duanji2014 2012-11-14 13:00
浏览 22
已采纳

评论框问题; 数据未在网站上呈现

i am fairly new to php and have recently encountered a problem.

I have created a comment box on my website using PHP and MySQL and it works fine but there is one major issue. The comment is only rendered on the website/stored in the database when it fits into one row of the html comment box. Whenever i enter longer text it simply won't save anything.

Here is the code i used:

<?php
mysql_connect("localhost","x","x");
mysql_select_db("fendersh_cdreviews");
$name=$_POST['name'];
$comment=$_POST['comment'];
$submit=$_POST['submit'];
$dbLink = mysql_connect("localhost", "x", "x");
    mysql_query("SET character_set_client=utf8", $dbLink);
    mysql_query("SET character_set_connection=utf8", $dbLink);
if($submit)
{
if($name&&$comment)
{
$insert=mysql_query("INSERT INTO cdreviews (name,comment) VALUES ('$name','$comment') ");
}
else
{
echo "please fill out all fields";
}
}
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<form action="cds.php" method="POST">
<table style="margin-left:15px; background-color:none; border-top: 1px dotted black; border-bottom: 1px dotted black; padding-bottom:5px;">
<tr><td><p style="margin-bottom:-20px;">Name:</p> <br><input type="text" name="name"/></tr>
<tr><td colspan="2"><p style="margin-bottom:-5px;">Comment:</p> </td></tr>
<tr><td colspan="5"><textarea name="comment" rows="3" cols="50"></textarea>
<tr><td colspan="2"><input type="submit" name="submit" value="Comment"></td></tr>
</table>
</form>
<?php
$dbLink = mysql_connect("localhost", "x", "x");
    mysql_query("SET character_set_results=utf8", $dbLink);
    mb_language('uni');
    mb_internal_encoding('UTF-8');
$getquery=mysql_query("SELECT * FROM cdreviews ORDER BY id DESC");
while($rows=mysql_fetch_assoc($getquery))
{
$id=$rows['id'];
$name=$rows['name'];
$comment=$rows['comment'];
echo "<p><b>$name</b>; $comment </p>"
;}
?>

I have replaced the user_name and password with an X for security reasons.

That's the code i'm using and it works fine.

It connects to the database alright; I have set my column parameter into TEXT.

Is there anything I should change there?

Hope someone can help me further with this.

Regards

Jack

  • 写回答

1条回答 默认 最新

  • doujianguang5506 2012-11-14 13:03
    关注

    You are connecting like three times. And selecting DB only once, selection that is forgotten when the following connect happens.
    Try using only one mysqli_connect() and only one mysqli_select_db() at the beginning of the code. Yes, use mysqli instead of mysql. mysql is deprecated (or about to, I think).

    Please take as example this... example, untested, but should work:

    <?php
    mysql_connect("localhost","x","x");
    mysql_select_db("fendersh_cdreviews");
    
    mb_language('uni');
    mb_internal_encoding('UTF-8');
    
    if (count($_POST))
    {
        $name    =& $_POST['name'];
        $comment =& $_POST['comment'];
        $submit  =& $_POST['submit'];
    
        if ($submit)
        {
            if (strlen($name)  &&  strlen($comment))
            {
                $name = mysql_real_escape_string($name);
                $comment = mysql_real_escape_string($comment);
                $insert = mysql_query("INSERT INTO cdreviews (name,comment) VALUES ('$name','$comment') ");
            }
                else
            {
                echo "please fill out all fields";
            }
        }
    }
    ?>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <form action="cds.php" method="POST">
    <table style="margin-left:15px; background-color:none; border-top: 1px dotted black; border-bottom: 1px dotted black; padding-bottom:5px;">
    <tr><td><p style="margin-bottom:-20px;">Name:</p> <br><input type="text" name="name"/></tr>
    <tr><td colspan="2"><p style="margin-bottom:-5px;">Comment:</p> </td></tr>
    <tr><td colspan="5"><textarea name="comment" rows="3" cols="50"></textarea>
    <tr><td colspan="2"><input type="submit" name="submit" value="Comment"></td></tr>
    </table>
    </form>
    <?php
        $getquery = mysql_query("SELECT * FROM cdreviews ORDER BY id DESC");
        while($rows = mysql_fetch_assoc($getquery))
        {
             $id = $rows['id'];
             $name = $rows['name'];
             $comment = $rows['comment'];
             echo "<p><b>$name</b>; $comment </p>";
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示