简体   繁体   中英

i have difficulties with the sql part

I posted a code below about my website. In this code i want to update rows in my database, if the user changed the name of the topic on the website's form. Everything is working except the sql part. I mean the part where:"LIMIT 1 OFFSET '$x'" this part of the sql code is not good for some reason, but i don't know why. I tested it in xampp phpmyadmin and it works but here something just wrong.

<?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");
    }
}
?>

Try updating as follows:(Hope your limit: 1 and offset: $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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM