1

INSERT multiple records in MySQL with one PHP form.

Simple Form

<form action="process.php" method="post">
<p><label>Beamline ID</label>
<input type="text" name="bline_id[][bline_id]" />
<label>Flow</label>
<input type="text" name="flow[][flow]" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[][bline_id]" />
<label>Flow</label>
<input type="text" name="flow[][flow]" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[][bline_id]" />
<label>Flow</label>
<input type="text" name="flow[][flow]" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[][bline_id]" />
<label>Flow</label>
<input type="text" name="flow[][flow]" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[][bline_id]" />
<label>Flow</label>
<input type="text" name="flow[][flow]" />
</p>
<input name="Submit" type="submit" />
</form>

//process.php

<?php
// connect to the database
include('connect-db.php');


$cnt = count($_POST['bline_id']);
$cnt2 = count($_POST['flow']);

if ($cnt > 0 && $cnt == $cnt2) {
    $insertArr = array();
    for ($i=0; $i<$cnt; $i++) {
        $insertArr[] = "('" . mysql_real_escape_string($_POST['bline_id'][$i]) . "', '" . mysql_real_escape_string($_POST['flow'][$i]) . "')";
}

 $query = "INSERT INTO bltest (bline_id, flow) VALUES " . implode(", ", $insertArr);
 mysql_query($query) or trigger_error("Insert failed: " . mysql_error());
}

echo("<pre>\n");
print_r($_POST);
echo("</pre>\n");




mysql_close($connection);
?> 

Array results

 Array
 (
 [bline_id] => Array
    (
        [0] => Array
            (
                [bline_id] => 1
            )

        [1] => Array
            (
                [bline_id] => 2
            )

        [2] => Array
            (
                [bline_id] => 3
            )

        [3] => Array
            (
                [bline_id] => 4
            )

        [4] => Array
            (
                [bline_id] => 5
            )

    )

[flow] => Array
    (
        [0] => Array
            (
                [flow] => 11
            )

        [1] => Array
            (
                [flow] => 22
            )

        [2] => Array
            (
                [flow] => 33
            )

        [3] => Array
            (
                [flow] => 44
            )

        [4] => Array
            (
                [flow] => 55
            )

    )

[Submit] => Submit Query
)

the INSERT result is of course 5 rows but no data inserted for $bline_id or $flow. But looking at the array, that is the correct data.

8
  • So you want to save the same data, 30 times in your database? Commented Feb 25, 2013 at 16:53
  • Same type of data. ie Same fields, different values. Commented Feb 25, 2013 at 16:55
  • Right. So the user loads up several values into an array with each button click, then clicks one button and your code will send them all to the database? Commented Feb 25, 2013 at 16:56
  • To make it more clear... each row would has an id "bline_id" that corresponds with an id from another table. Commented Feb 25, 2013 at 16:58
  • enter data in a form with the values for 30 records and submit Commented Feb 25, 2013 at 17:00

3 Answers 3

4

USE PDO or Mysqli instead, these extensions have the prepare option, so you need ony to pass the query once, and that use a while loop to change the data!

<?php

// pdo example

$sql = 'INSERT INTO `table` (field1, field2, field3) VALUES (:value1, :value2, :value3)';

// $dbh is pdo connection
$insertTable = $dbh->prepare($sql);

$countArray = count($array);
$i = 0;

while ($i < $countArray) {
   $insertTable->bindParam(':value1', $array[1][$i], PDO::PARAM_INT); // if value is int
   $insertTable->bindParam(':value2', $array[2][$i], PDO::PARAM_STR); // if value is str
   $insertTable->bindParam(':value3', $array[3][$i], PDO::PARAM_STR);
   $insertTable->execute();

   $i++;
}

?>
Sign up to request clarification or add additional context in comments.

2 Comments

@ perry that would be great, I have not been able to find an example that I could or tutorial that i could make heads or tails of...
you forgot the colons in this VALUES (:value1, value2, value3) and I had to fix that.
0

Okay. Given what you've told me, here's the solution I've thought up. I'm not going to give you code; it defeats the point of you writing it.

I would have the user type in some values into the two input text fields "bline_id" and "flow".

When they hit the submit button, those values are added to an array in the PHP code.

The array is then serialized into a string, and stored as a session cookie.

As each value is inputted, you unserialize the cookie so it is converted into an array, add the new value into the array, and repeat.

When the user hit's another button, "Store in Database". This option will unserialize the cookie, loop through each element in the array, and store each value in the database.

Also bare in mind that you have two values, bline_id and flow. These can be stored in two arrays and stored as two cookies. All it means is you're doing this process for two different arrays instead of one.

Finally, you should be using the PDO Object. I've linked you to it with the "store" link. It is the recommended method.

7 Comments

Ok Thank you! One more issue... An internet connection is not always available while this data is being entered will it still work?
It will be stored in a session cookie. When the browser is closed, the data is lost. Another option is to store the data in a persistent cookie. This cookie will last for as long as you want it to. Take a look at the setcookie() method that I linked you. It tells you how to make them. If this answers your question, be sure to mark it as the right answer :)
It will take some time before I know if this answers my question because I have to be able to put it all together! Thank you so much for your time :)
That's okay :) Good luck with it all, and feel free to ask any questions if you get stuck again!
after a ton of reading, trial and error, I am somewhat closer but stuck at the moment, and could use some more input!
|
0

OMG!!!! I finally got it! It was my stupid form!

corrected below-------

<form action="process.php" method="post">
<p><label>Beamline ID</label>
<input type="text" name="bline_id[]" value="<?php echo $bline_id; ?>" />
<label>Flow</label>
<input type="text" name="flow[]"  value="<?php echo $flow; ?>" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[]" value="<?php echo $bline_id; ?>" />
<label>Flow</label>
<input type="text" name="flow[]"  value="<?php echo $flow; ?>" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[]"  value="<?php echo $bline_id; ?>"/>
<label>Flow</label>
<input type="text" name="flow[]" value="<?php echo $flow; ?>" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[]" value="<?php echo $bline_id; ?>" />
<label>Flow</label>
<input type="text" name="flow[]" value="<?php echo $flow; ?>" />
</p>
<p><label>Beamline ID</label>
<input type="text" name="bline_id[]" value="<?php echo $bline_id; ?>" />
<label>Flow</label>
<input type="text" name="flow[]" value="<?php echo $flow; ?>" />
</p>
<input name="Submit" type="submit" />
</form>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.