sgtBear
sgtBear

Reputation: 61

Loop over two arrays synchronously

my html is like this

<table>
<tr>
<td><input type="text" name="input_1[]</td>
<td><input tpye="text" name="input_2[]</td>
</tr>
<tr>
<td><input type="text" name="input_1[]</td>
<td><input tpye="text" name="input_2[]</td>
</tr>
</table>

on submit, the values get posted to the next site.

i know how to handle one array[], like this:

foreach($array as $var) {
echo $var;
}

so, my problem is, i have to build an sql-Query inside the for each, it should be like

$sql="INSERT into table (value1,value2) VALUES (input_1,input_2)"

how can this be solved, i dont think for each can handle something like this:

foreach($array1 as $var1, $array2 as $var2){ 
....

Upvotes: 1

Views: 94

Answers (4)

fortune
fortune

Reputation: 3372

You can use for loop to make it happen:

<?php

$a = $_POST['input_1'];
$b = $_POST['input_2'];

for($i=0; $i<count($a); $i++) {

    $sql="INSERT into table (value1,value2) VALUES ('{$a[$i]}','{$b[$i]}')";
    echo $sql .'<br>';
}

Upvotes: 1

MH2K9
MH2K9

Reputation: 12039

In your HTML tpye should be type. Try like below

In HTML

<form action="test.php" method="post">
<table>
    <tr>
        <td><input type="text" name="input_1[]"> </td>
        <td><input type="text" name="input_2[]"></td>
    </tr>
    <tr>
        <td><input type="text" name="input_1[]"></td>
        <td><input type="text" name="input_2[]"></td>
    </tr>
    <tr>
        <td></td>
        <td><input type="submit"></td>
    </tr>
</table>
</form>

In PHP (test.php)

$post = $_POST; 
$count = 0;
for($i = 0; $i < count($post['input_1']); $i++){
    $inp1 = $post['input_1'][$count];
    $inp2 = $post['input_2'][$count];
    $sql = "INSERT into table (value1,value2) VALUES ('$inp1', '$inp2')";
    $count++;
}

Upvotes: 1

Bluedayz
Bluedayz

Reputation: 599

First, take a look at your html:

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

should be

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

First solution, if you use PDO to connect to your database:

<?php
if($_SERVER['REQUEST_METHOD'] === 'POST') { //if something posted to server
    $array = array($_POST['input_1'], $_POST['input_2']);
    $stmt  = $db->prepare("INSERT into table (value1, value2) VALUES (?, ?)");
    foreach($array as $key) {
        $stmt->execute(array($key[0], $key[1]));
    }
}

Here the second solution if you use MySQLi:

<?php
if($_SERVER['REQUEST_METHOD'] === 'POST') {
     $array = array($_POST['input_1'], $_POST['input_2']);
     $stmt  = $db->prepare("INSERT into table (value1, value2) VALUES (?, ?)");
     foreach($array as $key) {
         $stmt->bind_param('ss', $key[0], $key[1]);
         $stmt->execute();
     }
     $stmt->close();
 }

I am using prepared statements here because 1. it is faster and 2. it reduces the risk of SQL injection by a lot. Keep in mind that we take user input here and this is never safe!

Upvotes: 0

Karl
Karl

Reputation: 833

You can use the $key in the foreach, but this requires that the key exists in both arrays. You can easily check that with isset().

$arr1 = array(...);
$arr2 = array(...);

foreach($arr1 as $key => $value) {
    echo $arr1[$key];
    echo $arr2[$key];
}

Upvotes: 1

Related Questions