-1

i already google and follow some step in video tutorial, but it seem all of my data in checkbox can't send into database. Any idea? i'm totally new with PHP code. If you can help, thank you so much

<?php
$sqlservername = "127.0.0.1";
$sqlusername = "root";
$sqlpassword = "";
$sqldbname ="test";

$conn = new mysqli($sqlservername, $sqlusername, $sqlpassword, $sqldbname);

if($conn->connect_error){
    die("Connection fail");
}

session_start();



if(isset($_POST['li_submit'])){
    $_SESSION['li_username'] = $_POST['username'];
    $li_username = $_SESSION['li_username'];    
}   

if(isset($_POST['submit'])){

$id = $username = $password = $location = $description = $employeeNo = $telephoneNo = $designation =  $place = $area = $date = $time = $taken = $management = $action = $remarks = $surveyA = "";

if(isset($_POST['username'])){
    $id = uniqid($prefix='u_');
    $username = $_POST['username'];

}
if(isset($_POST['password'])){
    $password = $_POST['password'];
}
if(isset($_POST['location'])){
    $location = $_POST['location'];
}
if(isset($_POST['description'])){
    $description = $_POST['description'];
}

if(isset($_POST['employeeNo'])){
    $employeeNo = $_POST['employeeNo'];
}
if(isset($_POST['telephoneNo'])){
    $telephoneNo = $_POST['telephoneNo'];
}
if(isset($_POST['designation'])){
    $designation = $_POST['designation'];
}
if(isset($_POST['place'])){
    $place = $_POST['place'];
}
if(isset($_POST['area'])){
    $area = $_POST['area'];
}
if(isset($_POST['date'])){
    $date = $_POST['date'];
}
if(isset($_POST['time'])){
    $time = $_POST['time'];
}
if(isset($_POST['taken'])){
    $description = $_POST['taken'];
}
if(isset($_POST['management'])){
    $management = $_POST['management'];
}

if(isset($_POST['action'])){
    $action = $_POST['action'];
}
if(isset($_POST['remarks'])){
    $remarks = $_POST['remarks'];
}
if(isset($_POST['surveyA'])){
    $surveyA = $_POST['surveyA'];
    $A = implode (",",$surveyA);
}

if(isset($_FILES['profile_pic'])){
    $target_dir = "IMG_UPLOAD/";
    $target_file = basename($_FILES['profile_pic']["name"]);
    $file_type = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
    $target_path = $target_dir . uniqid($prefix='img.'). "." .$file_type;

    if(!move_uploaded_file($_FILES['profile_pic']['tmp_name'], $target_path)){
        $target_path = "";
    }


}   

$sql_insert = $conn->prepare("insert into user_info(id, username, password, location, description, image_path, employeeNo, telephoneNo, designation, place, area, date, time, taken, management, action, remarks, surveyA) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
$sql_insert->bind_param("sssssssssssssssss", $id, $username, $password, $location, $description, $target_path, $employeeNo, $telephoneNo,$designation, $place, $area, $date, $time, $taken, $management, $action, $remarks, $surveyA);
$sql_insert->execute();
$sql_insert->close();

}
$conn->close();




?>

This is my HTML code.

<input type="checkbox" name="surveyA[]" value="Ignoring Safety Signboard / Menginkari Tanda Keselamatan"><b>Ignoring Safety Signboard</b><br>Menginkari Tanda Keselamatan</br>
    <input type="checkbox" name="surveyA[]" value="Rule Violation / Melanggar Peraturan"><b>Rule Violation</b><br>Melanggar Peraturan</br>
    <input type="checkbox" name="surveyA[]" value="Wrong Tool / Equipment / Machinery / Peralatan / Alatan / Mesin Salah"><b>Wrong Tool / Equipment / Machinery</b><br>Peralatan / Alatan / Mesin Salah</br>
    <input type="checkbox" name="surveyA[]" value="Improper Material Handling / Kendalian Bahan Tidak Sempurna"><b>Improper Material Handling</b><br>Kendalian Bahan Tidak Sempurna</br>
    <input type="checkbox" name="surveyA[]" value="Use Faulty Tools / Equipment / Vehicle / Munggunakan Peralatan / alatan / Kenderaan Rosak"><b>Use Faulty Tools / Equipment / Vehicle</b><br>Munggunakan Peralatan / alatan / Kenderaan Rosak</br>
    <input type="checkbox" name="surveyA[]" value="Positive Finding/ Other. Please Specify"><b>Positive Finding/ Other. Please Specify</b>

i expected my data will send into database, so if you guys got any idea, please help me. Thank you very much

Mr Haikal
  • 13
  • 6

3 Answers3

0

You should be inserting $A, not $surveyA. $surveyA is an array containing the checkbox values, $A is the string resulting from implode().

Give it a default value here:

$id = $username = $password = $location = $description = $employeeNo = $telephoneNo = $designation =  $place = $area = $date = $time = $taken = $management = $action = $remarks = $A = "";

and then use it in the bind_param() call.

$sql_insert->bind_param("sssssssssssssssss", $id, $username, $password, $location, $description, $target_path, $employeeNo, $telephoneNo,$designation, $place, $area, $date, $time, $taken, $management, $action, $remarks, $A);
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • What declaration? – Barmar Sep 12 '19 at 04:34
  • I didn't look for any problems other than the checkboxes, which my answer shows the solution to. – Barmar Sep 12 '19 at 04:35
  • if(isset($_POST['surveyA'])){ $surveyA = $_POST['surveyA']; $A = implode (",",$surveyA); } this code is correct sir? because i got error. This is my error. Fatal error: Uncaught Error: Call to a member function bind_param() on bool in D:\xampp\htdocs\survey3.php:94 Stack trace: #0 {main} thrown in D:\xampp\htdocs\survey3.php on line 94 i save my checkbox table in database is "A", got error like that. @barmar – Mr Haikal Sep 12 '19 at 04:38
  • That error message means that `$conn->prepare()` is getting an error. See https://stackoverflow.com/questions/22662488/how-to-get-mysqli-error-in-different-environments – Barmar Sep 12 '19 at 04:42
0

Try this

<?php
$sqlservername = "127.0.0.1";
$sqlusername = "root";
$sqlpassword = "";
$sqldbname ="test";

$conn = new mysqli($sqlservername, $sqlusername, $sqlpassword, $sqldbname);

if($conn->connect_error){
die("Connection fail");
}

session_start();



if(isset($_POST['li_submit'])){
$_SESSION['li_username'] = $_POST['username'];
$li_username = $_SESSION['li_username']; 
} 

if(isset($_POST['submit'])){

$id = $username = $password = $location = $description = $employeeNo = $telephoneNo = $designation = $place = $area = $date = $time = $taken = $management = $action = $remarks = $surveyA = "";

if(isset($_POST['username'])){
$id = uniqid($prefix='u_');
$username = $_POST['username'];

}
if(isset($_POST['password'])){
$password = $_POST['password'];
}
if(isset($_POST['location'])){
$location = $_POST['location'];
}
if(isset($_POST['description'])){
$description = $_POST['description'];
}

if(isset($_POST['employeeNo'])){
$employeeNo = $_POST['employeeNo'];
}
if(isset($_POST['telephoneNo'])){
$telephoneNo = $_POST['telephoneNo'];
}
if(isset($_POST['designation'])){
$designation = $_POST['designation'];
}
if(isset($_POST['place'])){
$place = $_POST['place'];
}
if(isset($_POST['area'])){
$area = $_POST['area'];
}
if(isset($_POST['date'])){
$date = $_POST['date'];
}
if(isset($_POST['time'])){
$time = $_POST['time'];
}
if(isset($_POST['taken'])){
$description = $_POST['taken'];
}
if(isset($_POST['management'])){
$management = $_POST['management'];
}

if(isset($_POST['action'])){
$action = $_POST['action'];
}
if(isset($_POST['remarks'])){
$remarks = $_POST['remarks'];
}
if(isset($_POST['surveyA'])){
$surveyA = $_POST['surveyA'];
$A = implode (",",$surveyA);
}

if(isset($_FILES['profile_pic'])){
$target_dir = "IMG_UPLOAD/";
$target_file = basename($_FILES['profile_pic']["name"]);
$file_type = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
$target_path = $target_dir . uniqid($prefix='img.'). "." .$file_type;

if(!move_uploaded_file($_FILES['profile_pic']['tmp_name'], $target_path)){
$target_path = "";
}


} 

$sql_insert = $conn->prepare("insert into user_info(username, password, location, description, image_path, employeeNo, telephoneNo, designation, place, area, date, time, taken, management, action, remarks, surveyA) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
$sql_insert->bind_param("sssssssssssssssss",$username, $password, $location, $description, $target_path, $employeeNo, $telephoneNo,$designation, $place, $area, $date, $time, $taken, $management, $action, $remarks, $A);
$sql_insert->execute();
$sql_insert->close();

}
$conn->close();

You don't need to define id in the query because it is automatically incremented in the database. And also use $A instead of $surveyA. And suppose here is your checkbox : PHP
and you can get by using this "$_POST['lang']".

I hope this is helpful according to your requirement

Vintage Coders
  • 162
  • 1
  • 4
  • It doesn't look like `id` is an auto-increment column. He has `$id = uniqid($prefix='u_');` so it's a unique string. – Barmar Sep 12 '19 at 04:44
  • @vintagecoders sir, can you show the code that you say? about $A until "$_POST['lang']". i still can't get the idea based on suggestion that you give. – Mr Haikal Sep 12 '19 at 04:53
0

This is just an example of how to get checkbox value

suppose here is your checkbox :

<input type="checkbox" name='lang' value="PHP"> PHP <br/> 
 and you can get by using this "$_POST['lang']".
Vintage Coders
  • 162
  • 1
  • 4