我如何将我的复选框数据保存到基于我的代码的数据库中,知道吗?

时间:2019-09-12 04:23:14

标签: php

我已经Google并按照视频教程中的步骤进行操作,但是看来我复选框中的所有数据都无法发送到数据库中。任何想法?我对PHP代码完全陌生。如果可以的话,非常感谢

<?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();




?>

这是我的HTML代码。

<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>

我希望我的数据会发送到数据库中,所以如果您有任何想法,请帮助我。非常感谢

3 个答案:

答案 0 :(得分:0)

您应该插入$A,而不是$surveyA$surveyA是包含复选框值的数组,$Aimplode()产生的字符串。

在此处提供默认值:

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

,然后在bind_param()调用中使用它。

$sql_insert->bind_param("sssssssssssssssss", $id, $username, $password, $location, $description, $target_path, $employeeNo, $telephoneNo,$designation, $place, $area, $date, $time, $taken, $management, $action, $remarks, $A);

答案 1 :(得分:0)

尝试一下

<?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();

您不需要在查询中定义id,因为它会在数据库中自动递增。并且也使用$ A代替$ surveyA。 并假设这是您的复选框:  PHP
您可以使用此“ $ _POST ['lang']”来获得。

我希望这对您有帮助

答案 2 :(得分:0)

这只是如何获取复选框值的示例

假设这是您的复选框:

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