选择正确的记录行

时间:2015-03-27 21:05:50

标签: php mysqli

我找不到问题。你能帮我解决变量$rows['book_id']的错误吗?它没有回应任何东西。我是否制作了一些$_SESSION变量或我该怎么做。我试图找到一个没有成功的解决方案。

    <?php
    $query=$conn->query("SELECT DISTINCT book_name, writer, book_id, countpc FROM `book` b
    JOIN `read` r
    ON (b.book_id = r.book)
    JOIN count_public_comment p ON
    (p.book=r.book)
    WHERE(b.book_id>'95')") or die ($conn->error);

    echo "<table id='table1'
    <br>
    <tr>
    <th>Bookname</th>
    <th>Writer</th>
    <th>Count of comments</th>
    </tr>";

    while($row = mysqli_fetch_array($query)){
    echo "<form action=test2.php method=post>";       
    echo "<tr>";
    echo "<td>" . $row['book_name'] . "</td>";
    echo "<td>" . $row['writer'] . "</td>";
    echo "<td>" . $row['countpc'] . "</td>";
    echo "<td>" . "<input type=hidden name=hidden value=" . $rows['book_id'] . "> </td>";
    echo "<td>" . "<input type=submit name=comment value=comments" . " </td>";
    echo "</tr>";
    echo "</form>";
    }

if(isset($_POST['comment'])){
$sql=$conn->query("SELECT public_comment, stars FROM `read` WHERE `read`.book = '{$rows['book_id']}'" ) or die ($conn->error);
}
echo "<table id='table1'>
<br>
<tr>
<th>Public_comments</th>
<th>stars</th>
</tr>";

while($row=mysqli_fetch_array($sql)){
echo "<tr>";
echo "<td>" . $row['public_comment'] . "</td>";
echo "<td>" . $row['stars'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>

2 个答案:

答案 0 :(得分:0)

如果你在谈论这部分

if(isset($_POST['comment'])){
   $sql=$conn->query("SELECT public_comment, stars FROM `read` WHERE `read`.book = '{$rows['book_id']}'" ) or die ($conn->error);
}

然后问题是$rows['book_id']不存在。它应该是$row['book_id']。您还需要将与评论相关的所有内容移动到while循环中,因为它们依赖于book_id。像这样:

while($row = mysqli_fetch_array($query)){
    echo "<form action=test2.php method=post>";       
    echo "<tr>";
    echo "<td>" . $row['book_name'] . "</td>";
    echo "<td>" . $row['writer'] . "</td>";
    echo "<td>" . $row['countpc'] . "</td>";
    echo "<td>" . "<input type=hidden name=hidden value=" . $row['book_id'] . "> </td>";
    echo "<td>" . "<input type=submit name=comment value=comments" . " </td>";
    echo "</tr>";
    echo "</form>";

    if(isset($_POST['comment'])){
        $sql=$conn->query("SELECT public_comment, stars FROM `read` WHERE `read`.book = {$row['book_id']}" ) or die ($conn->error);
    }
    echo "<table id='table1'>
    <br>
    <tr>
    <th>Public_comments</th>
    <th>stars</th>
    </tr>";

    while($row=mysqli_fetch_array($sql)){
        echo "<tr>";
        echo "<td>" . $row['public_comment'] . "</td>";
        echo "<td>" . $row['stars'] . "</td>";
        echo "</tr>";
    }
    echo "</table>";
}

答案 1 :(得分:0)

这是不回应的行吗?

 echo "<td>" . "<input type=hidden name=hidden value=" . $rows['book_id'] . "> </td>";

如果是这样,这应该有用

 echo "<td>" . "<input type=hidden name=hidden value=\"" . $rows['book_id'] . "\"> </td>";