我一直在尝试将这些复选框数据插入到mysql中但是徒劳无功,是否有人可以查看我的代码?
drop.php:这部分工作正常
<?php
session_start();
include 'checkbox.php';
?>
<!DOCTYPE html>
<html>
<head>
<title> PHP FORM </title>
</head>
<body bgcolor = "green">
<h3> INSERT VALUE FROM CHECKBOXES </h3>
<form action = "checkbox.php" method ="post">
<input type = "checkbox" name= "chk1[]" value ="JEff"> JEff <br/>
<input type = "checkbox" name= "chk1[]" value ="Taylor"> Taylor <br/>
<input type = "checkbox" name= "chk1[]" value ="Christ"> Christ <br/>
<input type = "checkbox" name= "chk1[]" value ="Andrew"> Andrew <br/>
<br>
<input type ="submit" name="Submit" value ="Submit">
</form>
</body>
</html>
checkbox.php:我认为问题来自这个页面,但是什么?
<?php
session_start();
include 'db.php';
$checkbox1 = $_POST['chk1'];
if($_POST["Submit"] == "Submit"){
for ($i = 0; $i <count($checkbox1);$i++){
if(!empty($checkbox1)){
$query = "INSERT INTO employees(name) VALUES ('".$checkbox1[$i]."')";
mysqli_query($query) or die (mysqli_error());
}
}
echo "NAMES are inserted";
}
?>
和db.php很好用
<?php
$conn = mysqli_connect('localhost', 'root', 'root', 'ABC');
if (!$conn) {
die("Connection failed: ".mysqli_connect_error());
}
答案 0 :(得分:0)
您需要在mysqli_query()函数中添加一个链接标识符。 以下将诀窍
mysqli_query($conn,$query) or die (mysqli_error());