php - insert multiple checkbox value using jquery and ajax but i only insert one record
问题描述
I have this table in my database,
Now, in the raw_selected_rubric(Level) column that is where I will insert multiple values based on how many checkbox have been check. For example,
Based on the picture I have selected the level 3 and level 4 and when I click submit only the level 3 value is inserted on my database.
Input field,
<input class="rubricChkbox" type="checkbox" value="3" />
Jquery and Ajax,
var rubricChkbox = [];
$('.rubricChkbox').each(function(){
if($(this).is(":checked"))
{
rubricChkbox.push($(this).val());
}
});
rubricChkbox = rubricChkbox.toString();
$.ajax({
url: "Queries/save.php",
type: "POST",
data: {
"rubricChkbox":rubricChkbox
},
success: function(yey){
console.log(yey);
alert(yey);
}
});
Queries/save.php,
if(isset($_POST['rubricChkbox'])) {
$rubric_value = $_POST['rubricChkbox'];
$sql_raw = "INSERT INTO rubric_selected (raw_selected_rubric, Saved, ID_cmat, ID_users)
VALUES ('$rubric_value', '1')";
$success = mysqli_query($conn, $sql_raw);
}
What's wrong in my code? I'm sorry I'm still learning jquery and ajax. Thank you for the help.
What I want to happen is that if I selected Level 3 and 4 it both data will be inserted like this,
解决方案
您需要有多个这样的插入语句,例如:
if(isset($_POST['rubricChkbox'])) {
$rubric_value = $_POST['rubricChkbox'];
foreach($rubric_value as $value){
$sql_raw = "INSERT INTO rubric_selected (raw_selected_rubric, Saved, ID_cmat, ID_users)
VALUES ('$value', '1')";
$success = mysqli_query($conn, $sql_raw);
}
}
推荐阅读
- python - 拟合曲线时如何修复“TypeError:f() 需要 3 个位置参数时给出 4 个错误”
- html - 当元素包含长文本时,如何强制伪“后”元素的宽度?
- javascript - 如何遍历对象数组并过滤掉同一小时内的项目
- ios - 在某些操作系统版本中未调用 WKHTTPCookieStore getAllCookies 完成处理程序
- c# - 如何在 WPF 中的窗口之间导航?(不是浏览器)
- sql - 如何从子查询中获取两个值用于sql的where子句
- algorithm - 图灵机元素区别问题
- javascript - 为什么单击提交时我的表单未验证
- java - 当 'Categories 等于 null 时跳过循环的第一个循环
- excel - 部分匹配索引匹配多个标准