如何从下拉列表PHP MySQL的选定选项中更新值:
因此,我尝试执行以下操作:单击提交按钮时,它将MySQL表中的set_default更新为1,这将成为测验,而默认测验将被其覆盖并设置其值到0。
<form method="POST" action="index.php">
<select name="data">
<?php echo $options;?>
</select>
<input type="submit" name="defaultQuiz" value="Set Quiz" >
</form>
<?php
if(isset($_POST['defaultQuiz'])&& $_POST['defaultQuiz'] != ""){
$defaultQ = $_POST['data'];
require_once("scripts/connect_db.php");
mysqli_query($db,"UPDATE quizes SET set_default=0 WHERE set_default=1")or
die(mysqli_error($db));
mysqli_query($db,"UPDATE quizes SET set_default=1 WHERE quiz_name='$defaultQ'")or die(mysqli_error($db));
$defaultQ_sql1 = mysqli_query($db,"SELECT id FROM quizes WHERE set_default=1")or die(mysqli_error($db));
$numDefaults = mysqli_num_rows($defaultQ_sql1);
if($numDefaults < 1 || $numDefaults > 1){
echo "Sorry, there was a problem setting /".$defaultQ."/ default. Please try again later.";
exit();
}else{
echo "Thanks! The quiz, /".$defaultQ."/ has now been set as default.";
exit();
}
}
但是,我总是收到错误消息“很抱歉,设置$ defaultQ.default ..时出现问题。 我已经尝试过多次更改代码,但是我确定问题出在
mysqli_query($db,"UPDATE quizes SET set_default=0 WHERE set_default=1")
or die(mysqli_error($db));
mysqli_query($db,"UPDATE quizes SET set_default=1 WHERE quiz_name='$defaultQ'")
or die(mysqli_error($db));
功能