gpt4 book ai didi

php - 在 php 中添加记录时未将正确的数据插入数据库

转载 作者:行者123 更新时间:2023-11-29 06:33:22 25 4
gpt4 key购买 nike

尝试将记录插入数据库,但数据未正确插入数据库表中。

这是数据库表:

问题数据库表:

question_number  question_no  text  status
1 1 html 1
2 2 php 0

选择数据库表:

id   question_number   is_correct  text   status
1 1 1 abc 1
2 1 0 abc 1
3 1 0 abc 1
4 1 0 abc 1
5 2 0 abcdfd 0
6 2 1 abcdsf 0
7 2 0 abcdsf 0
8 2 0 abcsdfg 0

如果我尝试添加新记录,它会以这种格式插入数据库表中

问题表:

question_number  question_no  text  status
3 2 html 1

选择数据库表:

id   question_number   is_correct  text          status
9 2 1 abchdfgh 1
10 2 0 abfdhdfc 1
11 2 0 afdhgbc 1
12 2 0 adfhbc 1

这里是在选择表中插入问题编号为 2,但这里应该插入为 3。

这是代码:

<?php
if(isset($_POST['submit'])){
//Get post variables
$question_number = $_POST['question_no'];
$question_text = $_POST['question_text'];
$correct_choice = $_POST['correct_choice'];
//Choices array
$choices = array();
$choices[1] = $_POST['choice1'];
$choices[2] = $_POST['choice2'];
$choices[3] = $_POST['choice3'];
$choices[4] = $_POST['choice4'];
$choices[5] = $_POST['choice5'];

//Question query
$query = "INSERT INTO `questions`(question_no, text)
VALUES('$question_number','$question_text')";
//print_r($query);
//die();
//Run query
$insert_row = $mysqli->query($query) or die($mysqli->error.__LINE__);

//Validate insert
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')";

//Run query
$insert_row = $mysqli->query($query) or die($mysqli->error.__LINE__);

//Validate insert
if($insert_row){
continue;
} else {
die('Error : ('.$mysqli->errno . ') '. $mysqli->error);
}
}
}
$msg = 'Question has been added';
}
}

/*
* Get total questions
*/
$query = "SELECT * FROM `questions` WHERE status='1'";
//Get The Results
$questions = $mysqli->query($query) or die($mysqli->error.__LINE__);
$total = $questions->num_rows;
$next = $total+1;
?>
<form method="post" action="add.php">
<p>
<label>Question Number: </label>
<input type="number" value="<?php echo $next; ?>" name="question_no" />
</p>
<p>
<label>Question Text: </label>
<input type="text" name="question_text" required/>
</p>
<p>
<label>Choice #1: </label>
<input type="text" name="choice1" required/>
</p>
<p>
<label>Choice #2: </label>
<input type="text" name="choice2" required/>
</p>
<p>
<label>Choice #3: </label>
<input type="text" name="choice3" required/>
</p>
<p>
<label>Choice #4: </label>
<input type="text" name="choice4" required/>
</p>
<p>
<label>Choice #5: </label>
<input type="text" name="choice5" required/>
</p>
<p>
<label>Correct Choice Number: </label>
<input type="number" name="correct_choice" />
</p>
<p>
<input type="submit" name="submit" value="Submit" />
</p>
</form>

最佳答案

在您选择的查询中插入$question_number,但它应该是您在问题表中插入的最后一个插入ID,对于最后一个插入ID,请使用此$last_id = mysqli_insert_id($conn);

//Updated Choice query
$query = "INSERT INTO `choices` (question_number, is_correct, text)
VALUES ('$last_id','$is_correct','$value')";

希望对你有帮助

关于php - 在 php 中添加记录时未将正确的数据插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55160841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com