Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm having a heck of a time trying to figure out how to loop through my posted form data and inserting to mysql. My database table contains two columns "name" and "age"

My form:

<form action="form.php" method="post">

<input type="text" name="data[][name]" value=""/>
<input type="text" name="data[][age]" value=""/>
<input type="text" name="data[][name]" value=""/>
<input type="text" name="data[][age]" value=""/>
<input type="text" name="data[][name]" value=""/>
<input type="text" name="data[][age]" value=""/>
<input type="text" name="data[][name]" value=""/>
<input type="text" name="data[][age]" value=""/>
<input type="text" name="data[][name]" value=""/>
<input type="text" name="data[][age]" value=""/>
<input type="text" name="data[][name]" value=""/>
<input type="text" name="data[][age]" value=""/>

<input type="submit" value="submit" name="submit" />
</form>

PHP:

<?php
// Create Mysqli object
$db = new mysqli('localhost', 'root', 'root', 'database');

// Create statement object
$stmt = $db->stmt_init();


if (isset($_POST['submit'])) {

// Create a prepared statement
if($stmt->prepare("INSERT INTO contact (name, age) VALUES (?, ?)")) {

    // Bind your variables to replace the ?s
    $stmt->bind_param('si', $name, $age);


    $returnedData = $_POST['data'];

  foreach($returnedData as $data) {
        $name = $data['name'];
        $age = $data['age'];
        $stmt->execute();
  }


    // Close statement object
    $stmt->close();
}


}

// Close Connection
mysqli_close($link);

?>
share|improve this question
 
@dave: no, you only bind variables once. It's (almost) essentially doing param s = &$name internally, establishing a reference to the specified variable. change the variable's value, and next time you exec() the prepared statement, it gets that changed value. –  Marc B Jul 9 at 17:09
 
also, do a print_r($returnedData) or print_r($_POST). Your array structure/data is not what you think it is. –  Sean Jul 9 at 17:10
 
and var_dump($stmt->execute). all of your db operations are simply assuming success. –  Marc B Jul 9 at 17:12
 
Any code examples/explanations would be much appreciated. Thanks! –  user1040259 Jul 9 at 17:21

1 Answer

up vote 2 down vote accepted

Currently your form returns this array -

$_POST['data'] = array(
                       0 => array('name'=> 'string'),
                       1 => array('age'=> #),
                       2 => array('name'=> 'string'),
                       3 => array('age'=> #),
                       4 => array('name'=> 'string'),
                       5 => array('age'=> #),
                       6 => array('name'=> 'string'),
                       7 => array('age'=> #),
                       8 => array('name'=> 'string'),
                       9 => array('age'=> #),
                       10 => array('name'=> 'string'),
                       11 => array('age'=> #);

So you either want to redo your form to give the name/age the same key-

<form action="form.php" method="post">

<input type="text" name="data[0][name]" value=""/>
<input type="text" name="data[0][age]" value=""/>
<input type="text" name="data[1][name]" value=""/>
<input type="text" name="data[1][age]" value=""/>
<input type="text" name="data[2][name]" value=""/>
<input type="text" name="data[2][age]" value=""/>
<input type="text" name="data[3][name]" value=""/>
<input type="text" name="data[3][age]" value=""/>
<input type="text" name="data[4][name]" value=""/>
<input type="text" name="data[4][age]" value=""/>
<input type="text" name="data[5][name]" value=""/>
<input type="text" name="data[5][age]" value=""/>

<input type="submit" value="submit" name="submit" />
</form>

-OR-

change your foreach loop -

$returnedData = $_POST['data'];

for($i=0;$i<count($returnedData);$i+=2){
    $name = $returnedData[$i]['name'];
    $age = $returnedData[$i+1]['age'];
    $stmt->execute();
}
share|improve this answer
1  
You beat me to it! –  Joe Frambach Jul 9 at 17:35
 
I'm receiving an error.. Any thoughts? –  user1040259 Jul 9 at 17:41
 
What is the error? –  Sean Jul 9 at 17:42
 
@Sean My fault (fixed) - Thanks, I really appreciate your time :)! –  user1040259 Jul 9 at 17:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.