0

I have a problem while inserting multiple record in to the db

HTML

<input type="input" name="row[][name]"> 
<input type="input" name="row[][surname]">
<input type="input" name="row[][name]"> 
<input type="input" name="row[][surname]">

PHP

$returnedData = $_POST['row'];
$sql = array(); 
foreach( $returnedData as $row ) {
    $sql[] = '("'.mysql_real_escape_string($row['name']).'", '.mysql_real_escape_string($row['surname']).')';
}
mysql_query('INSERT INTO tableName (name, surname) VALUES '.implode(',', $sql));

But the error is It opens 4 rows while I mean it opens a new row for each input.

How can I insert two records ?

NEW INFO

when I write posted values by usingforeach I can see the values properly, the thing is I couldnt find the solution to insert in to my table

foreach($returnedData as $data) { echo '<pre>'; echo $data['name']; echo $data['surName']; }

zyrag
  • 353
  • 1
  • 3
  • 17
  • First take a look into `$returnedData`, then decide how to iterate over it. And if you do the same with `$sql` instead of directly running the query, you can faster find out. – hakre Jun 21 '14 at 13:19
  • the problem is not that you can foreach and array or not, but *how* you foreach it. So the problem is not inserting here, it's before the inserting. – hakre Jun 21 '14 at 13:24
  • I've left you an answer giving you a better look onto the variables and made you an online example that shows a bit better how it works. – hakre Jun 21 '14 at 13:33
  • just my opinion, i would be tempted to use names of 'name[]' and 'surname[]' rather than the multi-dimensional arrays that you are creating. To see them just do: var_dump($_POST). If you do want to keep them together in one entry then i think 'row[name][]' will be easier to use. – Ryan Vincent Jun 21 '14 at 13:36

2 Answers2

0

to insert a row for each instance of your post it should be:

foreach( $returnedData as $row ) {
    $sql[] = '("'.mysql_real_escape_string($row['name']).'", '.mysql_real_escape_string($row['surname']).')';
mysql_query('INSERT INTO tableName (name, surname) VALUES '.implode(',', $sql));
}

cause your way only inserts the last one.

I think that is what your trying to achieve

Matt
  • 1,081
  • 15
  • 27
0

For the HTML form as you outline it in your question, the $_POST['row'] contains the form-data in the following structure:

Array
(
    [0] => Array
        (
            [name] => Lara
        )

    [1] => Array
        (
            [surname] => Larasen
        )

    [2] => Array
        (
            [name] => Linda
        )

    [3] => Array
        (
            [surname] => Lindasen
        )

)

As you've shown in your foreach iteration, you pick each entry and you have error displaying disabled. So you don't get the message that most often the data you're looking for is missing and your iterating 4 times instead of two.

Most likely you wanted to create the following HTML form:

<input type="input" name="row[0][name]"> 
<input type="input" name="row[0][surname]">

<input type="input" name="row[1][name]"> 
<input type="input" name="row[1][surname]">

Which gives you the $_POST['row'] the form-data in this following, but different structure that should be easier for you to iterate:

Array
(
    [0] => Array
        (
            [name] => Lara
            [surname] => Larasen
        )

    [1] => Array
        (
            [name] => Linda
            [surname] => Lindasen
        )

)

Bottom line is: PHP needs a little help from you with the outer array-index so that name and surname are properly grouped.

You can try out both forms your own with this demo: http://codepad.viper-7.com/kxUsru


In case codepad viper looses the code, here is the source of the example:

<form method="post">
<input type="input" name="row[][name]"> 
<input type="input" name="row[][surname]">

<input type="input" name="row[][name]"> 
<input type="input" name="row[][surname]">

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


<form method="post">
<input type="input" name="row[0][name]"> 
<input type="input" name="row[0][surname]">

<input type="input" name="row[1][name]"> 
<input type="input" name="row[1][surname]">

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

<pre>
<?php
    echo htmlspecialchars(print_r($_POST['row'], TRUE));
?>
</pre>
hakre
  • 193,403
  • 52
  • 435
  • 836