无法从sql数据库中检索并设置值

时间:2016-01-03 16:46:06

标签: php mysql sql mysqli

我在mysql数据库中有这些表:

answer_det:

enter image description here

问题:

enter image description here

我想为属于特定项目的一个问题选择4个答案。每个问题都有一个id(qid),每个项目都有一个id(pid)。例如,我想选择属于pid = 2 qid = 3的每个答案。然后我想将检索到的值(答案)存储到输入文本字段中。这是我想从“answer_det”-table中检索的信息。

从“问题”表中,我想检索相同“pid”的问题链接和

<?php
				
if (!empty($error_msg)) {
    echo $error_msg;
}

$qid1 = $_POST['qid'];
                    
$retrieve ="SELECT answer FROM answer_det AS answer1 WHERE pid = '" . $_SESSION['pid'] . "' AND qid ='$qid1' AND aid'1'";
$retrieve .="SELECT answer FROM answer_det AS answer2 WHERE pid = '" . $_SESSION['pid'] . "' AND qid ='$qid1' AND aid'2'";
$retrieve .="SELECT answer FROM answer_det AS answer3 WHERE pid = '" . $_SESSION['pid'] . "' AND qid ='$qid1' AND aid'3'";
$retrieve .="SELECT answer FROM answer_det AS answer4 WHERE pid = '" . $_SESSION['pid'] . "' AND qid ='$qid1' AND aid'4'";

$retrieve .="SELECT question_link FROM question WHERE pid = '" . $_SESSION['pid'] . "' AND qid ='$qid1'";
               
$result = $mysqli->multi_query($retrieve);

if ($result->num_rows > 0) {
    while($row = $result->fetch_array()) {

        echo "
            <form action='insertdilemma.php' method='post'><br>

        <h2>Dilemma</h2>

        <div>Video link:<br><textarea rows='1' cols='40' name='videolink1'>". $row["question_link"]. "</textarea></div>
            
            Svar 1: <input type='text' name='ans1' value='". $row["answer1"]. "'>
            Svar 2: <input type='text' name='ans1' value='". $row["answer2"]. "'>
            Svar 3: <input type='text' name='ans1' value='". $row["answer3"]. "'>
            Svar 4: <input type='text' name='ans1' value='". $row["answer4"]. "'>

<input type='submit' name='submit' value='Create'>   
</form>
";
    }}
    
else {
    echo "Wrong info.";
}

mysqli_close($mysqli);
		
?>

我收到此错误: 注意:尝试在第77行的C:\ xampp \ htdocs \ wildfire \ editdilemma.php中获取非对象的属性

这是哪一行: if($ result-&gt; num_rows&gt; 0){

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您应该可以使用简单的查询来解决错误:

$retrieve ="SELECT aid, answer FROM answer_det WHERE pid = '" . $_SESSION['pid'] . "' AND qid ='$qid1'";

然后您将得到每个答案作为结果,因此您必须更改while循环以正确地以HTML格式输出结果。