dongmubi4375 2016-04-21 13:25
浏览 33
已采纳

这个PHP代码可能有什么问题? [重复]

This code when opened in the browser, displays "Reply button not pressed!" at first when the page loads, and After text is entered and Post button is pressed, The text isn't inserted into the database: Here's the code:

<html>
<head><title>Some Title</title></head>
<body>
<div class="bottom">
<form action="#reply" method='post'>
    <input type="submit" class="button" value="Post">
</form>
</div>
<a id="reply" href="#" class="popup"></a>
<div class="popup">
<div class="title"><h3>Reply to this Topic</h3></div>
<div class="itopic">
    <p>What would you think about this?</p>
    <form name="pform" method="post" action="">

     <tr>
        <td><textarea name="text" placeholder="Enter your thoughtful response here!" cols="50" rows="20"></textarea></td>
     </tr>
     <br>
     <tr>
        <input type="submit" name="reply" class="tbutton" value="Reply"/>
     </tr>
    </form>
</div>


    <a class="close x" href="#">x</a>

</div>
<?php
$id=$_GET['id']; //id fetched from the URL
if(isset($_POST['reply']) && !empty($_POST['reply']))
{
    $conn=new mysqli('localhost','root','','forum') or die(mysql_error());
    if(!strlen(trim($_POST['text']))>0)
    {
        echo "Reply!";
    }
    else
    {
        $stmt= $conn->prepare("INSERT into messages(id,text) VALUES(?,?)");
        $stmt->bind_param('is',$id1,$text);
        if(isset($_POST['text'])){ $tag = $_POST['text']; } 
        $id1=$id;
        $stmt->execute();
        $stmt->close();
        $conn->close();
    }   
    header("Location: forum.php");
    }
    else{
        echo"Reply button Not pressed!";
}       
?>
</body>
</html>

What might be wrong in this php code? Database details:

  1. Database Name: forum
  2. Table name: messages

columns-

  1. id (foreign key referring to topicID in a different table)
  2. time (Default being set to CURRENT_TIMESTAMP)
  3. text

PS: I'm new to php. So help is really appreciated

</div>
  • 写回答

3条回答 默认 最新

  • dqr91899 2016-04-21 13:33
    关注

    I believe the error is in this line:

    if(isset($_POST['text'])){ $tag = $_POST['text']; }
    

    it should be

    if(isset($_POST['text'])){ $text = $_POST['text']; }
    

    instead, since you require the parameter $text and not $tag.

    I would also set this line

    $stmt->bind_param('is',$id1,$text);
    

    after this line:

    $id1=$id;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。