php - 我在使用此 sql 查询 html/php 时遇到问题

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

最佳答案

$_POST['$s'] should be $_POST[$s] ($s without the quotes)

这是真的。这背后的原因是因为您的 $_POST 将您的变量 ($s) 视为 text。由于引号,它认为它不是变量。删除它应该可以解决问题。

更新,这一行看起来有点奇怪:

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

我希望在删除引号后您的问题得到解决,但如果不是这种情况,id 建议像这样更改您的查询:

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

关于php - 我在使用此 sql 查询 html/php 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36079129/

相关文章:

php - 如何在 PHP 中反序列化 Perl Data::Dumper 输出

php - Codeigniter - 检测浏览器的最佳方法

php - 如何在打印查询结果时对 HTML 中的元素进行分组?

html - 组件打开后按钮的蓝色边框

MySQL 重复事件未执行

php - 随机插入数据库

javascript - 从 HTML 页面调用 javascript 文件时出现问题

jquery - 如何在不包含子元素的元素内的悬停文本上加下划线

mysql - 将查询结果中的 id 外键替换为用户名

mysql - mysql 中的供应商表