如果选中复选框,则会将多行插入到表中

时间:2015-05-12 05:01:46

标签: php mysql

如果选中复选框,我试图将多行插入数据库表。但是在我尝试插入的代码中,基于复选框选择插入新行。但没有数据传递。我需要一些关于以下代码的建议来修改:

<?php
  $db=mysql_connect("localhost","root","");
  mysql_select_db("kkk",$db);
  $qry="select * from  pi";
  $result=mysql_query($qry);
?>

<form action="check.php"   method="post">
  <table>
    <tr>
      <th>A</th>
      <th>B</th>
      <th>C</th>
   </tr>

<?php
while($row=mysql_fetch_array($result))
{
  echo "<tr><td><input type=checkbox name=name[] value='".$row['id']."'>".$row['PI_NO']."</td><td>".$row['CUSTOMER_NAME']."</td><td>".$row['PI_ADDRESS']."</td></tr>";
}
?>

<input type="submit" value="save" id="submit">

<?php
  $db=mysql_connect("localhost","root","");
  mysql_select_db("kkk",$db);

  $name=$_POST['name'];
  foreach($_POST['name'] as $x)
  {
    $qry="INSERT INTO pi (PI_NO, CUSTOMER_NAME, PI_ADDRESS)VALUES ('$PI_NO','$CUSTOMER_NAME','$PI_ADDRESS')";
    mysql_query($qry);
  }     
?>

2 个答案:

答案 0 :(得分:1)

说明:

  • 您忘记使用一个勾号(checkbox)绑定'的名称
  • 您在查询中使用了尚未定义和分配值
  • 的变量
  • 您只传递了name的值,但未包含Pi AddressCustomer name。我将使用<input type="hidden">隐藏输入传递它们。
  • 我会更改您检查传递的表单的方式,方法是循环使用for()if()
  • 进行检查
  • 在您的查询中使用mysql_real_escape_string()之前使用mysqli prepared statement来阻止某些SQL injections。但是,如果您考虑使用mysql_*而不是弃用的isset(),那就更好了。
  • 您的帖子是单个文件吗?如果是,则必须使用<form>附上查询,以防止在加载页面时出错。
  • 您没有关闭<?php while($row=mysql_fetch_array($result)) { ?> <tr> <td> <input type="checkbox" name="name[]" value="<?php echo $row['id']; ?>"> <?php echo $row["PI_NO"]; ?> <!-- HERE IS THE START OF YOUR TWO HIDDEN INPUT --> <input type="hidden" name="piaddress[]" value="<?php echo $row["PI_ADDRESS"]; ?>"> <input type="hidden" name="customer[]" value="<?php echo $row["CUSTOMER_NAME"]; ?>"> </td> <td><?php echo $row['CUSTOMER_NAME']; ?></td> <td><?php echo $row['PI_ADDRESS']; ?></td> </tr> <?php } /* END OF WHILE LOOP */ ?> <input type="submit" value="save" id="submit"> </form> <!-- YOU DID NOT CLOSE YOUR FORM IN YOUR POST -->

这是你纠正的while循环:

<?php

  $db=mysql_connect("localhost","root","");
  mysql_select_db("kkk",$db);

  $counter = count($_POST["name"]); /* COUNT THE PASSED ON NAME */

  for($x=0; $x<=$counter; $x++){

    if(!empty($_POST["name"][$x])){
      $PI_NO = mysql_real_escape_string($_POST["name"][$x]);
      $CUSTOMER_NAME = mysql_real_escape_string($_POST["customer"][$x]);
      $PI_ADDRESS = mysql_real_escape_string($_POST["piaddress"][$x]);

      $qry="INSERT INTO pi (PI_NO, CUSTOMER_NAME, PI_ADDRESS) VALUES ('$PI_NO','$CUSTOMER_NAME','$PI_ADDRESS')";
      mysql_query($qry);

    } /* END OF CHECKING THE CHECKBOX IF SELECTED */

  } /* END OF FOR LOOP */

?>

您的查询:

match1(.*)match2

答案 1 :(得分:0)

很多小问题。还有一些大的。

as $x){ .. $x未被使用,因此我假设您只是循环查看复选框的数量。

这些没有值:'$PI_NO','$CUSTOMER_NAME','$PI_ADDRESS'

缺少</form>

未使用:$name=$_POST['name'];

<?php
echo '<form action="check.php"   method="post"><table><tr><th>A</th><th>B</th><th>C</th></tr>';

$db=mysql_connect("localhost","root","");
mysql_select_db("kkk",$db);
$sql = "select `id`,`PI_NO`, `CUSTOMER_NAME` ,`PI_ADDRESS` from  `pi`";
$result=mysql_query($sql);
while($row=mysql_fetch_array($result)){
  echo "<tr><td><input type=\"checkbox\" name=\"name[]\" value=/"$row[0]/"'>$row[1]</td><td>$row[2]</td><td>$row[3]</td></tr>";
}

echo '<input type="submit" value="save" id="submit"></form>';
foreach($_POST['name'] as $x){
  $sql="INSERT INTO pi (`PI_NO`, `CUSTOMER_NAME`, `PI_ADDRESS`)VALUES ('$PI_NO','$CUSTOMER_NAME','$PI_ADDRESS')";
  mysql_query($sql);
}
?>