我希望使用php将mutiple checkboxes
中的值插入到mysql数据库中,但代码不起作用。计划是将复选框值发送为json string
,并使用php在separate row
上插入每个值。
勾选复选框并提交后,我有empty
表。以下是我的代码。
<?php
if (isset($_POST)){
include("config.php");
$name = $conn->real_escape_string($_POST['name']);
$email = $conn->real_escape_string($_POST['email_address']);
$id = $conn->real_escape_string($_POST['code']);
$category = $_POST['speciality']; //this is the json string..example [{"a": "1", "b" : "2"}]
$data = json_decode($category,true);
$pass = $conn->real_escape_string($_POST['password']);
$hashed_password = password_hash($pass,PASSWORD_DEFAULT);
if(($id === "hello") || ($id === "hiii")){
foreach($data as $fields){
foreach($fields as $key => $val){
$query1 = "INSERT INTO category(email,cat_id) VALUES('$email', '$val')";
$result1 = $conn->query($query1);
}
}
if ( $result1) {
$query = "INSERT INTO friend(name,email,pass) VALUES('$name', '$email', '$hashed_password')";
$result = $conn->query($query);
if ( $result){
echo "success";
}
else {
echo "fail";
}
}
}
else{
echo "id_mismatch";
}
}
?>