我有sql部分的困难

时间:2015-04-07 10:01:18

标签: php mysql web

我在下面发布了一个关于我网站的代码。在此代码中,如果用户在网站的表单中更改了主题的名称,我想更新数据库中的行。一切都在工作,除了sql部分。我指的是:" LIMIT 1 OFFSET' $ x'"这部分sql代码由于某种原因不好,但我不知道为什么。我在xampp phpmyadmin中测试了它并且它可以工作但是这里有些错误。

<?php
$sql = "SELECT topicname, username, created, COUNT(commentid)
FROM user, topic, comment
WHERE topic.topicid = comment.whichtopic
AND user.userid = topic.owner
AND user.username = '" . $_SESSION['user_name '] . "'
GROUP BY topicname ";
$lekerdezes = mysql_query($sql);
$num_rows = mysql_num_rows($lekerdezes); ?>
<?php
if (isset($_POST['delete']))
{
    if (!empty($_POST['forumnev']))
    {
        for ($x = 0; $x < $num_rows; $x++)
        {
            foreach ($_POST['forumnev'] as $selected)
            {
                $seged = mysql_query("SELECT created FROM topic WHERE 
                                        created IN (SELECT created FROM user, topic, comment WHERE topic.topicid = comment.whichtopic 
                                        AND user.userid = topic.owner AND user.username = '" . $_SESSION['user_name '] . "'
                                        GROUP BY topicname ORDER BY created)
                                        LIMIT 1 OFFSET '$x'");
                if (!$seged)
                {
                    echo mysql_error();
                }
                $seged2 = mysql_fetch_array($seged);
                $seged2 = $seged2[0];
                if (!$seged2)
                {
                    echo mysql_error();
                }
                $sql = mysql_query("UPDATE topic SET topicname = '$selected' WHERE created = '$seged2'");

            }
        }
        header("Location: topicedit.php");
    }
}
?>

1 个答案:

答案 0 :(得分:0)

尝试更新如下:(希望你的限制:1和偏移:$ x)

$seged = mysql_query("SELECT created FROM topic WHERE created IN (SELECT created  
                                FROM user,topic,comment
                                WHERE topic.topicid = comment.whichtopic
                                AND user.userid = topic.owner
                                AND user.username = '". $_SESSION['user_name'] ."'
                                GROUP BY topicname
                                ORDER BY created) 
                                LIMIT $x, 1");