编辑:抱歉取消的是数据库中的tinyint类型 我有一个带有1个复选框的表单 - 我希望它在检查时更新我的数据库以显示课程被取消。默认情况下,我的数据库中的值为0,但是当我尝试更新课程以取消它时,值永远不会更改为1.请帮忙!
这是我的editForm代码段:
<p><label class="field" for "Credits">Credits:</label> <input type="text" name="cred" size="40" value="<?php echo "$row[Credits]"?>" class="rep"></p>
<p><label class="field" for "Cancelled">Cancelled:</label> (Tick to cancel) <input type="checkbox" name="canc" size="40" value="Yes" class="rep"></p>
<input type="submit" name="submit_value" value="Update"></p>
这是我的updateCourse php页面:
$id = $_POST["id"];
$title = $_POST["title"];
$cat = $_POST["cat"];
$loc = $_POST["loc"];
$county = $_POST["county"];
$time = $_POST["time"];
$date = $_POST["date"];
$cred = $_POST["cred"];
$fac = $_POST["fac"];
$canc = $_POST["canc"];
if(isset($_POST['submit_value'])) {
if(isset($_POST['canc'])) {
$canc = isset($_POST['canc']) ? '1' : '0';
$query1 = "UPDATE Course
SET Title = '$title',
Category = '$cat',
Location = '$loc',
County = '$county',
Course_Time = '$time',
Course_Date = '$date',
Credits = '$cred',
Facilitator = '$fac'
Cancelled = '$canc'
WHERE CourseID = '$id' ";
$result = $mysqli_db->query($query1);
} else {
$query2 = "UPDATE Course
SET Title = '$title',
Category = '$cat',
Location = '$loc',
County = '$county',
Course_Time = '$time',
Course_Date = '$date',
Credits = '$cred',
Facilitator = '$fac'
WHERE CourseID = '$id' ";
$query_result = $mysqli_db->query($query2);
我的query2正在运行,但query1从不更改我的数据库中的已取消值
答案 0 :(得分:1)
您在查询中缺少此行中字段之间的逗号:
Facilitator = '$fac'
之后应该有一个逗号:
$query1 = "UPDATE Course
SET Title = '$title',
Category = '$cat',
Location = '$loc',
County = '$county',
Course_Time = '$time',
Course_Date = '$date',
Credits = '$cred',
Facilitator = '$fac',
Cancelled = '$canc'
WHERE CourseID = '$id' ";
这会触发MySQL错误。您可以使用mysql_error()
来调试这些问题。回显或打印查询通常也有助于发现错误。
答案 1 :(得分:1)
首先,简单代码行太多了。您正在重复$canc
的发起。如果你用0和1分配变量,为什么heck 2会查询? 1更简单,更易读。此外,您可以更轻松地进行调试。其次,您需要了解PDO语句或过滤,因为您的脚本非常容易受到攻击。例如,您的脚本可以缩短为:
$canc = isset($_POST['canc']) ? '1' : '0';
if(isset($_POST['submit_value'])) {
$query = "
UPDATE Course
SET Title = '$title',
Category = '$cat',
Location = '$loc',
County = '$county',
Course_Time = '$time',
Course_Date = '$date',
Credits = '$cred',
Facilitator = '$fac',
Cancelled = '$canc'
WHERE CourseID = '$id'";
$query_result = $mysqli_db->query($query);
}