这个PHP代码可能有什么问题?

时间:2016-04-21 13:26:00

标签: php html mysql

此代码在浏览器中打开时显示“未按下回复按钮!”首先加载页面,输入文本并按下Post按钮后,文本不会插入到数据库中: 这是代码:

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

这个PHP代码可能有什么问题? 数据库详细信息:

  1. 数据库名称:论坛
  2. 表名:消息
  3. 列 -

    1. id(外键引用不同表中的topicID)
    2. 时间(默认设置为CURRENT_TIMESTAMP)
    3. 文本
    4. PS:我是php的新手。所以非常感谢帮助

3 个答案:

答案 0 :(得分:2)

我认为错误就在这一行:

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

应该是

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

相反,因为你需要参数$ text而不是$ tag。

我也会设置这一行

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

这一行之后:

$id1=$id;

答案 1 :(得分:1)

我认为您的ID将是一个文本字符串而不是整数,因此bind_param()函数将失败。尝试将id转换为整数。

Savon::SOAPFault: /app/vendor/bundle/ruby/2.2.0/gems/nori-2.6.0/lib/nori.rb:72:in `find_value': undefined method `each' for nil:NilClass (NoMethodError)
from /app/vendor/bundle/ruby/2.2.0/gems/nori-2.6.0/lib/nori.rb:38:in `find'
from /app/vendor/bundle/ruby/2.2.0/gems/savon-2.11.1/lib/savon/soap_fault.rb:24:in `to_s'
from /app/vendor/ruby-2.2.3/lib/ruby/2.2.0/irb.rb:498:in `write'
from /app/vendor/ruby-2.2.3/lib/ruby/2.2.0/irb.rb:498:in `print'
from /app/vendor/ruby-2.2.3/lib/ruby/2.2.0/irb.rb:498:in `block (2 levels) in eval_input'
from /app/vendor/ruby-2.2.3/lib/ruby/2.2.0/irb.rb:623:in `signal_status'
from /app/vendor/ruby-2.2.3/lib/ruby/2.2.0/irb.rb:486:in `block in eval_input'
from /app/vendor/ruby-2.2.3/lib/ruby/2.2.0/irb/ruby-lex.rb:245:in `block (2 levels) in each_top_level_statement'
from /app/vendor/ruby-2.2.3/lib/ruby/2.2.0/irb/ruby-lex.rb:231:in `loop'
from /app/vendor/ruby-2.2.3/lib/ruby/2.2.0/irb/ruby-lex.rb:231:in `block in each_top_level_statement'
from /app/vendor/ruby-2.2.3/lib/ruby/2.2.0/irb/ruby-lex.rb:230:in `catch'
from /app/vendor/ruby-2.2.3/lib/ruby/2.2.0/irb/ruby-lex.rb:230:in `each_top_level_statement'
from /app/vendor/ruby-2.2.3/lib/ruby/2.2.0/irb.rb:485:in `eval_input'
from /app/vendor/ruby-2.2.3/lib/ruby/2.2.0/irb.rb:395:in `block in start'
from /app/vendor/ruby-2.2.3/lib/ruby/2.2.0/irb.rb:394:in `catch'
from /app/vendor/ruby-2.2.3/lib/ruby/2.2.0/irb.rb:394:in `start'
from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/commands/console.rb:110:in `start'
from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/commands/console.rb:9:in `start'
from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:68:in `console'
from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/commands.rb:17:in `<top (required)>'
from /app/bin/rails:4:in `require'
from /app/bin/rails:4:in `<main>'

答案 2 :(得分:1)

我的猜测是你的重定向命令(header("Location: forum.php");),它位于错误的地方