lundi 7 novembre 2016

insert value from checkbox into mysql

I have been trying to insert these checkboxes data into mysql but in vain, could anyone have a look at my code?

drop.php : this part works fine

<?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: I believe the problem comes from this page, but what ?

<?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";
}

?>

and the db.php which works great

<?php

$conn = mysqli_connect('localhost', 'root', 'root', 'ABC');

if (!$conn) {
  die("Connection failed: ".mysqli_connect_error());
}




Aucun commentaire:

Enregistrer un commentaire