问题陈述:
前3列是“ id”,“ rollnum”,“ selectStatus”
通过登录期间创建的会话变量,我获得了学生的卷数。这样我就可以在特定行上运行更新查询。
问题:我如何根据用户检查的输入来更新这9个主题列。注意:我将这些选中的输入字段值存储在数组中。
代码
<form action="index.php" id="form-3" method="post">
<input class="form-check-input" name="year-3-checkbox[]" type="checkbox" value="ucs303">UCS303 Operating Systems
<input class="form-check-input" name="year-3-checkbox[]" type="checkbox" value="ucs406">UCS406 Data Structures and Algorithms
<input class="form-check-input" name="year-3-checkbox[]" type="checkbox" value="uec401">UEC401 Analog Communication Systems
<input class="form-check-input" name="year-3-checkbox[]" type="checkbox" value="uec612">UEC612 Digital System Design
<input class="form-check-input" name="year-3-checkbox[]" type="checkbox" value="uec307">UEC307 Electromagnetic Field Theory & Trans Lines
<input class="form-check-input" name="year-3-checkbox[]" type="checkbox" value="uec502">UEC502 Digital Signal Processing
<input class="form-check-input" name="year-3-checkbox[]" type="checkbox" value="uec510">UEC510 Computer Architecture
<button type="submit" name="year-3-submit">Submit Selection</button>
</form>
<?php
if(isset($_POST['year-3-submit'])){
if(!empty($_POST['year-3-checkbox'])){
$subjectCheckList = array();
$subjectCheckList = $_POST['year-3-checkbox'];
}
}
?>
这取决于用户选择了多少个复选框。
例如: 用户1已选择3复选框并提交了表单,我们具有表3的数组和UPDATE 3列。 用户1选择了6个复选框并提交了表单,我们有6个数组和表的UPDATE 6列。
答案 0 :(得分:1)
根据OP的注释,可以使代码通用,如下所示:
// Check if atleast one subject has been selected
$selectedSubjects = array_filter($subjectCheckList);
// If no subject selected
if (empty($selectedSubjects)) {
$sql = "UPDATE subjectmaster
SET substatus = 0
WHERE rollno = '" . mysqli_real_escape_string($rollnumber) . "'";
} else {
// Initialize the sql string
$sql = "UPDATE subjectmaster
SET substatus = 1 ";
$i = 1;
foreach ($subjectCheckList as $subject) {
$sql .= ", sub" . $i . " = '" . mysqli_real_escape_string($subject) . "' ";
}
$sql .= " WHERE rollno = '" . mysqli_real_escape_string($rollnumber) . "'";
}
另外,请注意使用mysqli_real_escape_string
。它有助于防止SQL注入。有关防止SQL注入的更好方法,您可以检查How can I prevent SQL injection in PHP?
答案 1 :(得分:0)
首先,不清楚每列的默认值是多少。 由于您的MySQL列是由数字(sub1,sub2等)设置的,因此您的表单应相应地用适当的值表示它们。例如:
<input class="form-check-input" name="year-3-checkbox[]" type="checkbox" value="1">
这样,您可以轻松循环并更新表(我假设子列为TINYINT(1)DEFAULT NULL):
<?php
if(isset($_POST['year-3-submit'])){
if(!empty($_POST['year-3-checkbox'])){
$subjectCheckList = array();
$query = "UPDATE table SET ";
foreach ($_POST['year-3-checkbox'] as $key => $value) {
$query .= " sub" . $value . " = 1, "
}
$query = substr($query, 0, -1);
}
}
?>
希望这会有所帮助 盖伊
答案 2 :(得分:0)
您还可以使用array_filter,array_combine和array_slice。
<?php
$subs = [':sub1',
':sub2',
':sub3',
':sub4',
':sub5',
':sub6',
':sub7',
':sub8',
':sub9'
];
// use $dataFromForm = array_filter($_POST['year-3-checkbox'])
$dataFromForm = ['11111',
'222222',
'3333333'];
$dbh = new PDO('mysql:host=localhost;dbname=test', 'root', '*******');
$sql = 'UPDATE test SET sub1 = :sub1, sub2 = :sub2, sub3 = :sub3, sub4 = :sub4, sub5 = :sub5, sub6 = :sub6, sub7 = :sub7, sub8 = :sub8, sub9 = :sub9';
$sth = $dbh->prepare($sql);
$sth->execute(array_combine(array_slice($subs, 0, count($dataFromForm), $dataFromForm)));