【问题标题】:How to insert multiple inputs with same input name如何插入具有相同输入名称的多个输入
【发布时间】:2017-08-03 06:29:30
【问题描述】:

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

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

问题:我想将 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>

【问题讨论】:

  • 您的代码中有很多错误... 1) 每个输入字段都写下唯一的名称。例如 name="date[]" 使用了 3 次......这是错误的。你应该使用 date1[]、date2[] 或 date3[]
  • 修复了这个错误并更新代码..我会指导你
  • $sql === TRUE 永远不会是真的,$sql 是一个字符串。

标签: php mysql arrays mysqli foreach


【解决方案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;
    }
}

【讨论】:

  • OOP os 可选,而准备好的语句是强制。它使您的代码容易出错且不安全
【解决方案2】:

你的测试是错误的,$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 注入。

【讨论】:

    【解决方案3】:

    试试这个代码:
    注意:阅读 cmets

    <?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>
    

    【讨论】:

    • 感谢大家的帮助,OmarTuffaha 和@Hossam 的回答对我有用。我也能够使用比 3 更高的 emor evalues。来自 LP154 的答案对我不起作用,我不知道为什么。我收到错误“未知索引”。感谢批评者,我只从教程和代码 sn-ps 中学习。我的问题是我找到了很多关于 sql 和 php 的文件,但我不知道学习顺序,因此我只是想找到我需要的代码 sn-ps 并将它们放在一起。我也知道编写代码有 3 种不同的方法。我通常会选择 mysqli 的那个。
    • 如果你有很好的知识资源和一些实际的例子,我将不胜感激。我的问题也是我现在发现许多旧代码。有时将 mysql_connect 之类的命令重写为 mysqli_connect 就足够了,但大多数情况下它需要更好的理解才能真正修复或编写我自己的代码。提前谢谢,如果太多,请忽略。祝你有美好的一天。
    【解决方案4】:

    你好,很好的例子。我遇到了同样的问题,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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-05
      • 1970-01-01
      • 2017-07-11
      • 2022-07-13
      • 2014-11-28
      相关资源
      最近更新 更多