将多行插入数据库

时间:2016-02-08 19:38:33

标签: php mysql database mysqli insert

您好我正在处理将多行插入数据库的问题。它应该是一个包含复选框的书籍列表,因此您可以将它们添加到列表中。我从其他表中获取了一些值,它们应该插入到my_list表中。我认为插入有问题,因为值是正确的。但是当我按下按钮时没有任何反应。

$sql = "SELECT b.id, b.name, b.author, l.id FROM books b join history h on(k.id=h.id_book) join list l on(l.id=h.id_list) join class c on(c.id=l.id_class) join students s on(s.id_class=c.id) join my_list m on(m.id=m.id_student)  where c.id=(select id_class from students where id=".$_SESSION['login']." and b.id!=n.id_book)";
$result = mysqli_query($conn, $sql);  

?>
<form action="index.php?content=list_books" method="post">
<?php
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
   ?><input type="checkbox" name="check_list[]" value="<?php echo $row['id']?>"/><?php  echo  $row["name"]. "/" . $row["author"]. "<br>";
}
?>
<input type="submit" value="Add" name="add_book">
<?php
} else {
echo "0 books";
}

?>

</form>
<?php
if(isset($_POST['add_book'])) {
 foreach($_POST['check_list'] as $check) {
    $insert = "INSERT INTO my_list (id_book,id_student,id_list) VALUES (".$check.",".$_SESSION['login'].",".$row['l.id'].")";
  $qwe = mysqli_query($insert, $conn); 
   if(!$qwe) {
  echo mysql_error();
  }       
  }
  }

0 个答案:

没有答案