将复选框中的值插入mysql

时间:2016-11-07 16:58:53

标签: php html mysql checkbox mysqli

我一直在尝试将这些复选框数据插入到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());
}

1 个答案:

答案 0 :(得分:0)

您需要在mysqli_query()函数中添加一个链接标识符。 以下将诀窍

mysqli_query($conn,$query) or die (mysqli_error());