我正在尝试建立一个多选题考试门户。它工作正常,但是当我在add.php文件中添加一个问题时,它会被插入,但问题的选择没有插入数据库中
这是我的数据库:
选择表:
问题表:
这是我的add.php
代码<?php include 'includes/header.php'; ?>
<?php include 'config/config.php'; ?>
<?php include 'lib/Database.php'; ?>
<?php
$db = new Database();
if (isset($_POST['submit'])) {
//Grab Post Data
$question_number = $_POST['question_number'];
$question_text = $_POST['question_text'];
$correct_choice = $_POST['correct_choice'];
$choices = array();
$choices[1] = $_POST['choice1'];
$choices[2] = $_POST['choice2'];
$choices[3] = $_POST['choice3'];
$choices[4] = $_POST['choice4'];
//Insert question into database
$query = "INSERT INTO `questions`(question_number, text) VALUES('$question_number','$question_text')";
$insert_row = $db->insert($query);
//validate
if ($insert_row) {
foreach ($choices as $choice => $value) {
if ($value != '') {
if ($correct_choice == $choice) {
$is_correct = 1;
} else {
$is_correct = 0;
}
//Choice Query
$query = "INSERT INTO `choices`(question_number,is_correct,text) VALUES('$question_number','$is_correct',
'$value')";
//insert row
$insert_row = $db->insert($query);
if ($insert_row) {
continue;
} else {
die($mysqli->error);
}
}
}
}
}
$query1 = "SELECT * FROM `questions`";
$result = $db->select($query1);
$total = $result->num_rows;
$next = $total + 1;
?>
<form class="form-horizontal" action="signup.php" method="POST">
<fieldset>
<div id="legend">
<legend class="text-center">Add Questions</legend>
</div>
<div class="control-group">
<label class="control-label" for="username">Question Number</label>
<div class="controls">
<input name="question_number" value="<?php echo $next; ?>" placeholder="" class="form-control input-lg" type="number"/>
</div>
</div>
<div class="control-group">
<label class="control-label" for="text">Question Text</label>
<div class="controls">
<input name="text" placeholder="" class="form-control input-lg" type="text">
</div>
</div>
<div class="control-group">
<label class="control-label" for="choice1">#Choice 1</label>
<div class="controls">
<input name="choice1" placeholder="" class="form-control input-lg" type="text">
</div>
</div>
<div class="control-group">
<label class="control-label" for="username">#Choice 2</label>
<div class="controls">
<input id="choice2" name="choice2" placeholder="" class="form-control input-lg" type="text">
</div>
</div>
<div class="control-group">
<label class="control-label" for="username">#Choice 3</label>
<div class="controls">
<input id="choice3" name="choice3" placeholder="" class="form-control input-lg" type="text">
</div>
</div>
<div class="control-group">
<label class="control-label" for="username">#Choice 4</label>
<div class="controls">
<input id="username" name="choice4" placeholder="" class="form-control input-lg" type="text">
</div>
</div>
<div class="control-group">
<label class="control-label" for="username">Correct Choice Number</label>
<div class="controls">
<input id="username" name="correct_choice" placeholder="" class="form-control input-lg" type="number"/>
</div>
</div>
<input type="submit" name="submit" class="btn btn-block btn-primary" value="Submit" class="submit"/>
</fieldset>
</form>
<?php include 'includes/footer.php';?>
现在只有问题是在数据库中添加而不是选择。
答案 0 :(得分:0)
语法似乎是正确的,但您可以删除数字周围的简单引号。此外,最好一个请求插入所有选项。
document.getElementById("totalcost").value = (+(document.getElementById("pg").value) || 0) + (+(document.getElementById("totalcost").value) || 0);
你的变量$ value和$ question_number来自$ _POST变量,你必须使用预准备语句来保护你的查询免受SQL注入。