我在使用这个sql查询html / php时遇到了麻烦

时间:2016-03-18 08:05:34

标签: php html mysql

您好我是网页设计的新手,所以如果这看起来像废话,请原谅!

好的,我在这里要做的是为每个帖子设置评论。我认为这样做的一个好方法是运行循环并指定$save_id[]来存储帖子ID和分配 $r <input type='text' name = *'".$r."'* style = 'width:478px; margin: 0 auto; margin-top:-9px;'id='butt_box' placeholder='Respond?' title = 'Leave a response.' />似乎工作正常并返回帖子ID。当我提交评论$_POST['$s']时,它没有注册,数据库没有更新,所以我想我的问题是,任何人都可以看到我做错了什么或特别是为什么if(isset($_POST['$s']))失败了?

enter code here $result =  mysql_query("SELECT * FROM user_posts WHERE user ='$name'  ORDER BY time_date DESC LIMIT 100");
        $save_id =array();
        while($row = mysql_fetch_assoc($result))
        {
            $save_id[] = $row["id"];
            $r = $row["id"];
            echo '<div id = "post">';
            echo '<img src = "'.$ico.'" style="margin: 0px" width = "48" height = "48" align = "left"/><h4>';echo $name.'</h4><h5>';echo $row["time_date"].'</h5><br>';
            echo $row["post"]. '</div>';
            echo "<form method='post' action=''>
            <input type='text' name = '".$r."' style = 'width:478px; margin: 0 auto; margin-top:-9px;'id='butt_box' placeholder='Respond?' title = 'Leave a response.'  />

            <input type='submit' value='Post' class='tfbutton' style =' float:right;'>
            </form>";
        }


        if(!$result)
        {
            echo mysql_error(); 
        }
        foreach($save_id as $s)
        {

        if(isset($_POST['$s']))
        {
           echo $_POST['$s'];
            $mypost = isset($_POST['$s']) ?     
            $_POST['$s'] : '';

            if(isset($mypost) && strlen($mypost) > 0)
            {      
                $text = addLinks($mypost);
                $text = CheckEmotes($text);

                $p = $text;
                $emote = "none";
                $dt = date('Y-m-d H:i:s');

                $sql = "INSERT INTO  `user_post_comments`(poster,post_id,responder,response,post_date)
                VALUES('$name','$s','$name',$text',$dt')";
                $rg = mysql_query($sql);

                if(!$rg)
                {
                    echo mysql_error(); 
                }
            }
            //header("Refresh:5; url=refresh.php");
        }          
    }

2 个答案:

答案 0 :(得分:1)

$_POST['$s']应为$_POST[$s]$s不带引号)

答案 1 :(得分:1)

  

$ _ POST [&#39; $ s&#39;]应该是$_POST[$s](没有引号的$ s)

这是事实。这背后的原因是因为您的$ _POST将您的变量($ s)视为文本。由于引用它认为它不是一个变量。删除它应该可以解决问题。

更新,这条线看起来有点奇怪:

 mysql_query("SELECT * FROM user_posts WHERE user ='$name'  ORDER BY time_date DESC LIMIT 100");

我希望在删除引号时修复您的问题,但如果不是这种情况,则建议您更改查询:

$selectAllFromUser = "SELECT * FROM user_posts WHERE user ='".$name."' ORDER BY time_date DESC LIMIT 100";
mysql_query($selectAllFromUser);

您的查询可能也会将$name变量视为文本。 (虽然我不知道你的版本的具体细节等。)

(编辑;已经很长时间了,但我认为这应该让你去。)

enter code here 
$selectAllFromUser= "SELECT * FROM user_posts WHERE user ='".$name."'  ORDER BY time_date DESC LIMIT 100";
$result =  mysql_query($selectAllFromUser);
        $save_id =array();
        while($row = mysql_fetch_assoc($result))
        {
//not working with arrays often, but shouldnt it be required to specify a // position? 
            $save_id[] = $row["id"];
            $r = $row["id"];
            echo '<div id = "post">';
            echo '<img src = "'.$ico.'" style="margin: 0px" width = "48" height = "48" align = "left"/><h4>{$row["id"]}</h4><h5>{ $row["time_date"]}</h5><br>{$row["post"]}</div>';
            echo "<form method='post' action=''>
            <input type='text' name = '".$r."' style = 'width:478px; margin: 0 auto; margin-top:-9px;'id='butt_box' placeholder='Respond?' title = 'Leave a response.'  />

            <input type='submit' value='Post' class='tfbutton' style =' float:right;'>
            </form>";
        }


        if(!$result)
        {
            echo mysql_error(); 
        }
        foreach($save_id as $s)
        {

        if(isset($_POST[$s]))
        {
           echo $_POST[$s];
            $mypost = isset($_POST[$s]) ?     
            $_POST[$s] : '';

            if(isset($mypost) && strlen($mypost) > 0)
            {      
                $text = addLinks($mypost);
                $text = CheckEmotes($text);

                $p = $text;
                $emote = "none";
                $dt = date('Y-m-d H:i:s');
//again using the $s without setting a position, is this valid?
                $sql = "INSERT INTO  `user_post_comments`(poster,post_id,responder,response,post_date)
                VALUES('{$name}','{$s}','{$name}','{$text}','{$dt}')";
                $rg = mysql_query($sql);

                if(!$rg)
                {
                    echo mysql_error(); 
                }
            }
            //header("Refresh:5; url=refresh.php");
        }          
    }