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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度