论坛SQL语法错误

时间:2017-06-07 15:44:59

标签: php html sql database forum

您好我正在尝试建立一个论坛,但是当我点击论坛的主题时,我可以查看该页面。我收到以下错误:

您的SQL语法有错误;检查与您的MariaDB服务器版本对应的手册,以便在第1行的“7”附近使用正确的语法

以下是我正在尝试构建的view_topic.php页面中的代码:

    <?php
    session_start(); 

    ?>
    <!DOCTYPE html>
    <html>
    <head>
         <title>View Category</title>
         <link rel="stylesheet" type="text/css" href="style.css">
    </head>
    <body>
    <div id="wrapper">
           <h2>Timkipptutorials</h2>


        <?php
         if(!isset($_SESSION ['uid'])){
             echo "<form action='login_parse.php' method='post'>
              Username: <input type='text' name='username'>&nbsp;
              Password <input type='text' name='password'>&nbsp;
              <input type='submit' name='submit' value='Log In'>" ;
        }
         else{
           echo "<p>You are logged in as ".$_SESSION['username']." &bull; <a            href='logout_parse.php'> Logout</a>";
        }
           ?>

    <hr>
    <div id="content">
            <?php
                include_once("connect.php");

                $cid = $_GET['cid'];
                $tid = $_GET['tid'];
                $sql = "SELECT * FROM topics WHERE category_id='".$cid."' AND           id='".$tid."' LIMIT 1";
                    $res = mysql_query($sql) or die(mysql_error());
                if(mysql_num_rows($res) == 1){
                    echo "<table width='100%'>";
                    if(isset($_SESSION['uid'])){
                            echo "<tr><td colspan='2'><input type='submit'          value='Add Reply' onClick=\"window.location = 'post_reply.php?          cid=".$cid."$tid=".$tid."'\"/><hr>";
                    }else{
                        echo "<tr><td colspan='2'><p>Please log in to add a reply</p><hr></td></tr>";
                    }
                    while ($row = mysql_fetch_assoc($res)) {
                        $sql2 = "SELECT * FROM posts WHERE category_id = '".$cid."' AND topic_id '".$tid."'";
                        $res2 = mysql_query($sql2) or die(mysql_error());
                        while($row2 = mysql_fetch_assoc($res2)){
                            echo "<tr><td valign='top' style='border: 1px           solid #000000;'><div style='min-height: 125px;'>".$row['topic_title']."<br> by          ".$row2['post_creator']. " - ".$row2['post_date']."<hr>".$row2['post_content']."        </div></td><td width='200' valign='top' align='center' style='border: 1px solid         #000000;'>User Info Here</td></tr><tr><td colspan='2'><hr></td></tr>";
                        }
                        $old_views = $row['topic_views'];
                        $new_views = $old_views + 1;
                        $sql3 = "UPDATE topics SET topic_views='".$new_views."' WHERE category_id='".$cid."' AND id='".$tid."' LIMIT 1";
                        $res3 = mysql_query($sql3) or die(mysql_error());
                    }

                    echo "</table>";

                }else{
                    echo "<p>This topic does not exist.</p>";
                }
            ?> 

        </div>
    </div>
    </body>
    </html>

请帮助我。感谢

2 个答案:

答案 0 :(得分:0)

$ sql2 =“SELECT * FROM posts WHERE category_id ='”。$ cid。“'AND topic_id'”。$ tid。“'”;

在第二次比较时缺少=符号...如

$ sql2 =“SELECT * FROM posts WHERE category_id ='”。$ cid。“'AND topic_id ='”。$ tid。“'”;

答案 1 :(得分:0)

您的代码中存在拼写错误,导致错误。你的代码是

$sql2 = "SELECT * FROM posts WHERE category_id = '".$cid."' AND topic_id '".$tid."'";

最后你忘了添加一个“=”来检查topic_id与$ tid的相等性。它应该是

$sql2 = "SELECT * FROM posts WHERE category_id = '".$cid."' AND topic_id = '".$tid."'";