5

I have a problem when I want to insert multiple fields into one table.

Here's my form:

<h1>Add user</h1>
 <form method="post" action="index.php">

 <table>
    <thead>
        <th>Name</th>
        <th>Age</th>
    </thead>

    <tr>
        <td><input name="name[]" type="text" /></td>
        <td><input name="age[]" type="text" /></td>
    </tr>

    <tr>
        <td><input name="name[]" type="text" /></td>
        <td><input name="age[]" type="text" /></td>
    </tr>

    <tr>
        <td><input name="name[]" type="text" /></td>
        <td><input name="age[]" type="text" /></td>
    </tr>
</table>

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

And here's the submit code:

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

    foreach ($_POST as $val) {
        $name = $val['name'];
        $age = $val['age'];

        mysql_query("INSERT INTO users (name, age) VALUES ('$name', '$age')");
    } 
}

The query inserts into the database, but not the values that I've entered.

Can someone please help me?

2
  • 1
    SQL injection anyone? Commented Aug 9, 2013 at 22:27
  • yeah, I'll fix that later, just need to fix this right now Commented Aug 9, 2013 at 22:28

6 Answers 6

15

You are doing a foreach on $_POST rather than on the name/age arrays. You should be doing foreach on name or age array like this:

if (
   !empty($_POST['name']) && !empty($_POST['age']) &&
   is_array($_POST['name']) && is_array($_POST['age']) &&
   count($_POST['name']) === count($_POST['age'])
) {
    $name_array = $_POST['name'];
    $age_array = $_POST['age'];
    for ($i = 0; $i < count($name_array); $i++) {

        $name = mysql_real_escape_string($name_array[$i]);
        $age = mysql_real_escape_string($age_array[$i]);

        mysql_query("INSERT INTO users (name, age) VALUES ('$name', '$age')");
    } 
}

I would also note that you are currently susceptible to SQL injection so I added the step of escaping your strings for name/age.

I would also highly suggest simply making a single bulk insert into the DB instead of an insert of each record individually (I will leave that up to you to implement). This approach is almost always preferable from a performance standpoint.

Finally, you REALLY should not be using mysql_* functions as they are deprecated. Consider changing to mysqli or PDO.

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

1 Comment

Thanks. but i'm having issues with the solution.
5
if (isset($_POST['submit'])) {

$i = 0;
foreach ($_POST as $val) {
    $name = $_POST['name'][$i];
    $age = $_POST['age'][$i];

    mysql_query("INSERT INTO users (name, age) VALUES ('$name', '$age')");
    $i++;
  } 
}

This will solve your problem !

Comments

1
foreach($_POST['firstname'] as $key=>$value) {
    $firstname = $_POST['firstname'][$key];
    $lastname = $_POST['tipo'][$key];
    echo "Parte: $lastname";
    echo "<br>"; 
    echo "Tipo: $firstname";
    echo "<br>";
}

1 Comment

You should add explanation for your answer to help OP and other readers understand this solution. Code only answers are discouraged.
0

A little bit easier code which works for me well.

if (isset($_POST['submit'])) {
     $i = 0;
     for ((array) $_POST as $val) {
       $sql = "INSERT INTO users (name, age) VALUES (
                   '{$_POST["name"][$i]}','{$_POST["age"][$i]}'
               );
       mysql_query($sql);
       echo (!mysql_affetced_rows()) ? "Query Wrong" : "Query Okay";
       $i++;
     } 
    }

Comments

0

below is the fxample how to inset multi row at one time

$query_string = "INSERT INTO YOURTBL_NAME(column_1,column_2)VALUES";
    $data ="";
    for($i=0;$i<count($YOUR FILE ARRAY);$i++)
    {
        $data .='("'.$paramater_1[$i].'","'.$paramater_2.'"),';
    }
     $qry = substr($query_string.$data, 0, -1); 
     $result = mysql_query($qry);

Comments

0
$education_institute_array = $_POST['education_institute'];
        $education_qualification_array = $_POST['education_qualification'];
        $education_start_date_array = $_POST['education_start_date'];
        $education_end_date_array = $_POST['education_end_date'];
        $education_note_array = $_POST['education_note'];
        for ($i = 0; $i < count($education_institute_array); $i++) {
            $education_institute = mysql_real_escape_string($education_institute_array[$i]);
            $education_qualification = mysql_real_escape_string($education_qualification_array[$i]);
            $education_start_date = mysql_real_escape_string($education_start_date_array[$i]);
            $education_end_date = mysql_real_escape_string($education_end_date_array[$i]);
            $education_note = mysql_real_escape_string($education_note_array[$i]);
            $sql_education_insert = "INSERT INTO `education` (`user_id`, `education_institute`, `education_qualification`, `education_start_date`, `education_end_date`, `education_note`) VALUES ('$user_id', '$education_institute', '$education_qualification', '$education_start_date', '$education_end_date', '$education_note')";
            $sql_result_education = mysql_query($sql_education_insert);
        }

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.