Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 具有多个_Php_Arrays_Foreach - Fatal编程技术网

Php 具有多个

Php 具有多个,php,arrays,foreach,Php,Arrays,Foreach,我的html是这样的 <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><

我的html是这样的

<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>
所以,我的问题是,我必须在for each中构建一个sql查询,它应该是

$sql="INSERT into table (value1,value2) VALUES (input_1,input_2)"
如何解决这个问题,我不认为每个人都能处理这样的事情:

foreach($array as $var) {
echo $var;
}
foreach($array1 as $var1, $array2 as $var2){ 
....

您可以在foreach中使用
$key
,但这要求两个数组中都存在该键。您可以使用
isset()
轻松检查这一点


您可以使用
for
循环来实现:

<?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>';
}

首先,查看您的html:

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

HTML
tpye中,应该是
类型
。试试下面这样

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>

在PHP中(test.PHP)

$post=$\u post;
$count=0;
对于($i=0;$i
键的确切值是多少?我不明白:/如果您愿意,可以将$key更改为$cats!不,但它会一个接一个地获取数组中的所有键。测试打印$key out,您将看到:)
<td><input type="text" name="input_1[]"/></td>
<?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]));
    }
}
<?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();
 }
<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>
$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++;
}