0

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);

?>
4
  • @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. Commented Jul 9, 2013 at 17:09
  • also, do a print_r($returnedData) or print_r($_POST). Your array structure/data is not what you think it is. Commented Jul 9, 2013 at 17:10
  • and var_dump($stmt->execute). all of your db operations are simply assuming success. Commented Jul 9, 2013 at 17:12
  • Any code examples/explanations would be much appreciated. Thanks! Commented Jul 9, 2013 at 17:21

1 Answer 1

2

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();
}
Sign up to request clarification or add additional context in comments.

1 Comment

@Sean My fault (fixed) - Thanks, I really appreciate your time :)!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.