Forum Moderators: coopster

Message Too Old, No Replies

Insert multiple variables into a database quickly

I don't want to list them all individually

         

ed_edin

3:44 pm on Jun 3, 2004 (gmt 0)

10+ Year Member



I've just started using PHP and have written a small script to insert 10 variables into my database, e.g:

$query = "insert into items values
('".$q1."', '".$q2."', '".$q3."', '".$q4."', '".$q5."','".$q6."', '".$q7."', '".$q8."', '".$q9."', '".$q10."')";

The problem is, I will eventually need 300 questions so I don't want to list them all individually. How can change this snippet of code so that it says something along the lines of "please insert all the variables q1-q300 into the database"?

Ed

sned

4:57 pm on Jun 3, 2004 (gmt 0)

10+ Year Member



My guess is to use an array, and a for loop, or foreach loop.

Something like --

for($x = 0; $x < 300; $x++){
$item = $q[$x];
$query = "INSERT INTO items VALUES ($item)";
mysql_query($query);
}

of course, I'm not sure what your database table is like, and theres no error catching there either, hope that helps.

PHP Arrays - www.php.net/array

Birdman

5:02 pm on Jun 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about a foreach loop on the $_POST array.

foreach ($_POST as $key => $val){
if ($key{0} == "q") mysql_query("insert into...");
}

just finish off the insert and use the variable $val for the value.

The if ($key{0} == "q") part is there to only insert fields that start with "q". Otherwise all form field values will be inserted. Even "Submit".

Birdman

5:07 pm on Jun 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sned's idea will work too!

Just name all your fields like this:

<input type="text" name="q[]" />
<input type="text" name="q[]" />
<input type="text" name="q[]" />
...

ed_edin

1:59 pm on Jun 7, 2004 (gmt 0)

10+ Year Member



When I used this to try it out with ten questions

for($x = 0; $x < 10; $x++){
$item = $q[$x];
$query = "INSERT INTO items VALUES ($item)";
mysql_query($query);
}

It inserted ten sets of data into my table, as if ten people had answered the questions rather than one. Am I doing something wrong?

Could you explain what ($x = 0; $x < 10; $x++) means? Is it saying, "for all questions between zero and 10" or something else?

Many thanks

sned

4:35 pm on Jun 7, 2004 (gmt 0)

10+ Year Member



The ($x = 0; $x < 10; $x++) tells the compiler to start at 0, and loop through the code as long as x is less than 10. It increments x at the end of each cycle.

Does your database just have one row for each set of questions? (I guess that would mean your table would have to have ~300 columns.) If that is the case, perhaps something like this could work:

$query = "INSERT INTO items VALUES (";
for($x = 0; $x < 9; $x++){
$item = $q[$x];
$query .= $item . ",";
}
// I just put the last item in here, to fix commas
$item = $q[9];
$query .= $item . ")";
mysql_query($query);

This will generate one big insert rather than 10 short ones.

m_shroom

5:09 pm on Jun 7, 2004 (gmt 0)

10+ Year Member



An other way is;

$item =("'$q1'");
$x=2;
$y=$number_of_q + 1;
while ($x < $y)
{$item= ($item).(",'$q$x'"); $x++;}
$query = "INSERT INTO items VALUES ($item)";
mysql_query($query);

TIP; Speed your scripts up by minimzing hard disk access use your RAM

ed_edin

8:37 am on Jun 8, 2004 (gmt 0)

10+ Year Member



I have put in 300 rows, one for each question. I'll give the above a go.