如何插入具有相同输入名称的多个输入

时间:2017-03-13 09:30:25

标签: php mysql arrays mysqli foreach

假设我有表:7列的交易:id,日期,金额,细节,类型,目的,位置。我希望能够在单击提交按钮后输入3行。在我的confiq.php中,在变量$ query中设置了连接。

这是我在insertmultiple.php中的代码(稍后它将作为3行包含在我的overview.php表中)。

问题:我想从html表单中将3行数据插入到表中的新行中。你看我错在哪里吗?我有一种感觉它在哪里,我开始条件如果。 谢谢大家。

<?php
    include('config.php');

    $date = $_POST['date'];
    $detail = $_POST['detail'];
    $type = $_POST['type'];

    foreach( $date as $key => $d ) {
        $sql = "INSERT INTO transactions (date, detail, type)
        VALUES ('$date[$key]', '$detail[$key]', '$type[$key]')";

        if ( $sql === TRUE) {
            mysqli_query ($query,$sql);
        } else {
        echo "Error: " . $sql . "<br>" . $query->error;
        }
    }
?>

<form method="post" action="">
<tr>
    <input type="text" name="date[]" />
    <input type="text" name="detail[]" />
    <input type="text" name="type[]" />
</tr>
<tr>
    <input type="text" name="date[]" />
    <input type="text" name="detail[]" />
    <input type="text" name="type[]" />
</tr>
<tr>
    <input type="text" name="date[]" />
    <input type="text" name="detail[]" />
    <input type="text" name="type[]" />
</tr>
    <input type="submit" name="submit" id="submit_button" style="width: 50px">
</form>

4 个答案:

答案 0 :(得分:-1)

在( $ CONNECTION )中准备mysqli_connect后:

$CONNECTION = mysqli_connect("IP_ADDRESS", "MYSQL_USER", "MYSQL_PASS", "MYSQL_DBNAME");

然后:

foreach( $date as $key => $d ) {
    $sql = "INSERT INTO transactions (date, detail, type)
            VALUES ('$date[$key]', '$detail[$key]', '$type[$key]')";

    if ( !(mysqli_query($CONNECTION, $sql) === TRUE) ) {
        echo "Error: " . mysqli_error($CONNECTION) . "<br>" . $sql->error;
    }
}

答案 1 :(得分:-1)

你的测试是错误的,$ sql永远不会是真的,它是一个字符串。试试这个:

<?php
include('config.php');

$date = $_POST['date'];
$detail = $_POST['detail'];
$type = $_POST['type'];

foreach( $date as $key => $d ) {
  $sql = "INSERT INTO transactions (date, detail, type) VALUES ('$date[$key]', '$detail[$key]', '$type[$key]')";

  if (mysqli_query ($query,$sql) === FALSE) {
      echo "Error: " . $sql . "<br>" . $query->error;
  }
}
?>

<form method="post" action="">
<tr>
    <input type="text" name="date[]" />
    <input type="text" name="detail[]" />
    <input type="text" name="type[]" />
</tr>
<tr>
    <input type="text" name="date[]" />
    <input type="text" name="detail[]" />
    <input type="text" name="type[]" />
</tr>
<tr>
    <input type="text" name="date[]" />
    <input type="text" name="detail[]" />
    <input type="text" name="type[]" />
</tr>
    <input type="submit" name="submit" id="submit_button" style="width: 50px">
</form>

编辑:我假设config.php定义了$query var并打开了连接。话虽这么说,你应该使用PDO和预处理语句来避免SQL注入。

答案 2 :(得分:-1)

试试这段代码:
注意:阅读评论

<?php 
include('config.php');


//added an if statment to check if the user have pressed submit (<input type="submit"  **name="submit"** id="submit_button" style="width: 50px">)
// this will insure that the query wont start unless data has been sent
if (isset($_POST['submit'])) {

$date = $_POST['date'];
$detail = $_POST['detail'];
$type = $_POST['type'];

foreach ($date as $key => $d) {
    $sql = "INSERT INTO transactions (date, detail, type) VALUES ('$date[$key]', '$detail[$key]', '$type[$key]')";

//        if ($sql === TRUE) { //the $sql will not be true it will be the equal to to the string of the query
//            mysqli_query($query, $sql);
//        } else {
//            echo "Error: " . $sql . "<br>" . $query->error;
//        }

    if (!mysqli_query($query, $sql)) { //tries to perform the query, if it doesnt work prints the error
        echo "Error: " . $sql . "<br>" . $query->error;
    }
}
}
?>

<form method="post" action="">
<tr>
<input type="text" name="date[]" />
<input type="text" name="detail[]" />
<input type="text" name="type[]" />
</tr>
<tr>
<input type="text" name="date[]" />
<input type="text" name="detail[]" />
<input type="text" name="type[]" />
</tr>
<tr>
<input type="text" name="date[]" />
<input type="text" name="detail[]" />
<input type="text" name="type[]" />
</tr>
<input type="submit" name="submit" id="submit_button" style="width: 50px">
</form>

答案 3 :(得分:-1)

  

你好,很好的例子。我遇到了同样的问题,grant all privileges on *.* to root@localhost identified by 'password'grant all privileges on *.* to root@'%' identified by 'password'

之间存在差异

如果你在localhost上使用root,你可能也想运行grant all privileges on *.* to root@localhost identified by 'password'

对于您的HTML和PHP脚本

<?php
include 'db_config.php';
if (isset($_POST['date']) && isset($_POST['detail']) && isset($_POST['type'])) {
   
    $date = $_POST['date'];
    $detail = $_POST['detail'];
    $type = $_POST['type'];
    
    for ($i = 0; $i < count($date); $i++) {
        $sql = "INSERT INTO transcations (date, detail, type) " .
                " VALUES ('$date[$i]', '$detail[$i]', '$type[$i]')";

        //$conn is the name of your connection string
        $results = mysqli_query($conn, $sql);
        if (!$results) {
            echo "Error: " . $sql . "<br>" . $query->error;
        }
    }
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>


        <form method="post" action="">
            <table>
                <tr>
                    <td>
                        <input type="text" name="date[]" />
                    </td>
                    <td>
                        <input type="text" name="detail[]" />
                    </td>
                    <td>
                        <input type="text" name="type[]" />
                    </td>
                </tr>
                <tr><td>
                        <input type="text" name="date[]" />
                    </td>

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

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



                </tr>
                <tr>
                    <td>
                        <input type="submit" name="submit" id="submit_button" style="width: 50px">
                    </td>

                </tr>

            </table>



        </form>
    </body>
</html>

相关问题