doushouhe7072 2016-10-15 12:13
浏览 169
已采纳

如何在MySQL中插入多行而不使用预准备语句插入空行

I want to insert multiple rows with the single query using PDO prepared statement but I don't want to insert any blank row.

Actually, I have four row in my form you can see in HTML below and when I fill only one row it insert three blank row also but I don't to insert any blank row

 <form method="post">
    <table>
      <tr>
        <td><input type="text" name="col1"></td>
        <td><input type="text" name="col2"></td>
      </tr>
      <tr>
        <td><input type="text" name="col1"></td>
        <td><input type="text" name="col2"></td>
      </tr>
      <tr>
        <td><input type="text" name="col1"></td>
        <td><input type="text" name="col2"></td>
      </tr>
      <tr>
        <td><input type="text" name="col1"></td>
        <td><input type="text" name="col2"></td>
      </tr>
    </table>
  </form>

my code is like this

$stmt = $pdo->prepare('INSERT INTO foo VALUES(:col1, :col2)');
foreach($data as $item)
{
    $stmt->bindValue(':col1', $item[0]);
    $stmt->bindValue(':col2', $item[1]);
    $stmt->execute();
}

help me please...

  • 写回答

1条回答 默认 最新

  • doushen4719 2016-10-15 12:27
    关注

    As your col1 and col2 is multiple then you should use array: Must change database credential and change table name. Testing purpose i have use table name test.

    index.php

    <?php
        if(isset($_POST['submit'])){
            $dbhost = "localhost";  //Set your hostname
            $dbname = "dbname";     //set your dbname
            $dbusername = "root";   //set your db username
            $dbpassword = "";       //set your db password
            $link = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbusername,$dbpassword);
    
            for ($i=0; $i <count($_POST['col1']) ; $i++) {
                $rowNum = $i+1;
                if($_POST['col1'][$i]!='' && $_POST['col2'][$i]!=''){
                    $statement = $link->prepare("INSERT INTO test(col1, col2) VALUES(:col1, :col2)");
                    $res = $statement->execute(array(
                        "col1" => $_POST['col1'][$i],
                        "col2" => $_POST['col2'][$i],
                    ));
                    if($res)
                        echo "Row no: $rowNum Successfully inserted.<br>";
                    else
                        echo "Row no: $rowNum Failed to insert.<br>";
                }else
                    echo "Row no: $rowNum single or double field  empty.<br>";
            }
        }
    ?>
    
    <form method="post">
        <table>
          <tr>
            <td><input type="text" name="col1[]"></td>
            <td><input type="text" name="col2[]"></td>
          </tr>
          <tr>
            <td><input type="text" name="col1[]"></td>
            <td><input type="text" name="col2[]"></td>
          </tr>
          <tr>
            <td><input type="text" name="col1[]"></td>
            <td><input type="text" name="col2[]"></td>
          </tr>
          <tr>
            <td><input type="text" name="col1[]"></td>
            <td><input type="text" name="col2[]"></td>
          </tr>
          <tr>
            <td colspan="2"><input type="submit" name="submit" value="submit"></td>
          </tr>
        </table>
    </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?