bech64
bech64

Reputation: 129

INSERT multiple records in MySQL with one PHP form

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.

Upvotes: 1

Views: 22711

Answers (3)

Perry
Perry

Reputation: 11710

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++;
}

?>

Upvotes: 4

bech64
bech64

Reputation: 129

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>

Upvotes: 0

christopher
christopher

Reputation: 27356

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.

Upvotes: 0

Related Questions