【问题标题】:Use cookie to get id, perform inner join on matching fields使用cookie获取id,对匹配的字段进行inner join
【发布时间】:2013-02-27 07:36:37
【问题描述】:

这是我的第一篇文章,但我发现这个论坛非常有用!我希望你能帮助我。

我的难题是:我让用户登录然后互相评分。用户登录后,我希望他们能够看到他们所做的评分(这是我开始工作的——我可以通过表单生成的唯一 ID 选择的评论),并且还可以看到他们的评分摘要已收到。这就是它似乎变得棘手的地方。我尝试了内部连接,但没有产生任何结果。

现在我的 html 上方有这部分

<?php 
    include "connect.php";
    if(isset($_COOKIE['ID_my_site'])) 
    { 
        $username = $_COOKIE['ID_my_site']; 
        $pass = $_COOKIE['Key_my_site']; 
        while($info = mysql_fetch_array( $check ))   
        { 
            //if the cookie has the wrong password, they are taken to the login page 
            if ($pass != $info['password']) 
            {
                header(""); 
            } 
            //otherwise they are shown the admin area    
            else 
            {
                echo ""; 
                echo ""; 
            } 
        } 
    } 
     else 
    //if the cookie does not exist, they are taken to the login screen 
    {            
        header(""); 
    } 
include "settings.php";
?>

这部分在我的html之后

<?php
    include('connect.php');
    $result = mysql_query("SELECT r.user, r.rating1, r.rating2, r.rating3, u.username
                             FROM reviews r INNER JOIN users u ON r.user=u.username
                            WHERE r.user='$userid' ORDER BY r.user DESC") 
                or die(mysql_error()); 

        echo "<table border='1' cellpadding='10'>";
        echo "<tr>
                  <th></th>
                  <th>View Comments</th>
                  <th>Rating 1</th>
                  <th>Rating 2</th>
                  <th>Rating 3</th>
              </tr>";

        while($row = mysql_fetch_array( $result )) {
               echo "<tr>";
               echo '<td><a href="showit.php?id=' . $row['id'] . '">View/Print</a></td>';
               echo '<td>' . $row['rating1'] . '</td>';
               echo '<td>' . $row['rating2'] . '</td>';
               echo '<td>' . $row['rating3'] . '</td>';
               echo "</tr>"; 
        } 
        echo "</table>";
?> 

不幸的是,我根本没有得到任何结果,尽管我在 sql 表中看到了这个人的大约 20 个评分。

它还会抛出“警告:mysql_fetch_array():提供的参数不是第 19 行的 reviews.php 中的有效 MySQL 结果资源”错误。

那里可能有一个愚蠢的错误,但我越来越盲目和沮丧。

感谢您的帮助!

【问题讨论】:

    标签: php sql cookies inner-join


    【解决方案1】:

    如果这是第 19 行:

    while($row = mysql_fetch_array( $result )) {
                   echo "<tr>";
                   echo '<td><a href="showit.php?id=' . $row['id'] . '">View/Print</a></td>';
                   echo '<td>' . $row['rating1'] . '</td>';
                   echo '<td>' . $row['rating2'] . '</td>';
                   echo '<td>' . $row['rating3'] . '</td>';
                   echo "</tr>"; 
            } 
    

    您应该使用数组中值的位置,例如 1,2,3 .. 等等,而不是 ratings1 ,ratings2 .. 等等。

    【讨论】:

    • 谢谢约翰尼!我还在研究这个,看看我能不能弄清楚。 :)
    • 我最终放弃了加入并从感谢页面的 cookie 更新评级表,然后只从评论表中调用迭代。谢谢你的回复,约翰尼。我称你为正确的回答者。
    猜你喜欢
    • 2016-02-05
    • 1970-01-01
    • 1970-01-01
    • 2011-04-20
    • 2014-03-13
    • 1970-01-01
    • 2013-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多