简体   繁体   English

使用复选框将多行插入MySQL

[英]Inserting multiple rows into MySQL with checkbox

Below is my code. 下面是我的代码。 I have two MySQL database tables, one is "member" and another one is "participants". 我有两个MySQL数据库表,一个是“成员”,另一个是“参与者”。 I would like to display the data in table "member" to my local host page and I did it. 我想将表“ member”中的数据显示到我的本地主机页面上,并且做到了。 However, I cannot insert multiple rows of "member" data by using checkbox to my database table "participants". 但是,我无法通过使用复选框将多个“成员”数据行插入数据库表“参与者”。 I am stuck. 我被困住了。 Any help is much appreciated. 任何帮助深表感谢。

<?php
try {
    $con = new PDO("mysql:host=localhost;dbname=kgruum member", "root", "");
    $sql = $con->query("SELECT * FROM member");
    echo "<table class='info' align='center' border='1'>";
    echo "<tr><td width='10'></td>
        <td width='10'><b>ID</b></td>
        <td width='500'><b>Name</b></td>
        <td width='50'><b>Handicap</b></td><tr>";

    foreach($sql as $row) {
        $ID = $row["ID"];
        $Name = $row["Name"];
        $Handicap = $row["Handicap"];
        echo "<tr>
            <td><form method='POST' action='Add participant.php'><input type='checkbox' name='insert[]' value='$ID'></td>
            <td>$ID</td>
            <td>$Name</td>
            <td>$Handicap</td><tr>";
    }

    echo"</table><div align='center'><input type='image' value='submit' src='add selected button.png' alt='submit Button' onmouseover='this.src='pink add selected button.png'' onmouseout='this.src='add selected button.png'' name='add_btn' id='add_btn'></div><br></form>";


    if(isset($_POST['add_btn'])) {
        if(!empty($_POST['insert'])) {
            foreach($_POST['insert'] as $check) {
                $st=$con->prepare("INSERT INTO participants(ID,Name,Handicap) VALUES('$ID','$Name','$Handicap')");
                $insert->bindParam('ID',$ID);
                $insert->bindParam('Name',$Name);
                $insert->bindParam('Handicap',$Handicap);
                $st->execute();
                }
           echo "<script type='text/javascript'>
               alert('Successful Insert ! ');
               window.location.href = 'Add participant.php';
               </script>";
         } else {
           echo "<script type='text/javascript'>alert('You didn't choose which user you want to insert ! ')</script>";
      }
    }
} catch(PDOException $e) {
    echo "error".$e->getMessage();
}
?>

You are not storing the $Name and $Handicap values in the insert[] array, you only storing the $ID value. 您没有将$ Name和$ Handicap值存储在insert []数组中,仅存储了$ ID值。

To resolve, do something like this: 要解决,请执行以下操作:

<input type='checkbox' name='insert[]' value='$ID|$Name|$Handicap'>

Then: 然后:

foreach($_POST['insert'] as $check) {
  $values = explode('|', $check);
  $ID = $values[0];
  $Name = $values[1];
  $Handicap = $values[2];
  //The rest of your SQL code goes here as you have it...
  $check = ''; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM