PHP形式不提交没有任何错误

时间:2019-07-09 20:49:10

标签: php mysql

我正在使此脚本具有表单操作方法,该方法应该在$ script_name变量中引用当前文件,并将数据引用到数据库,但它不起作用

我其余的代码都在工作,我尝试了e_all,而var_dump($ _ POST)是array(0){}

<h3>Leave a comment</h3>
<form action="<?php echo $SCRIPT_NAME . '?id='. $validentry ; ?>" method="post">
<table> <tr>
  <td>Your name</td>
  <td><input type="text" name="name"></td>
</tr>
<tr>
  <td>Comments</td>
  <td><textarea name="comment" rows="10" cols="50"></textarea></td>
</tr> <tr>
<td></td>
  <td><input type="submit" name="submit"  value="Add comment"></td>
</tr>
</table>
</form>


if(isset($_POST['submit']) && !empty($_POST['submit'])) {
 $con = mysqli_connect("localhost", "root", "root", "blog");
  $con->select_db("blog");
  $sql = "INSERT INTO comments(blog_id, dateposted,
 name, comment) VALUES(" .
    $validentry . ", NOW(), '" . $_POST['name']
 . "', '" . $_POST['comments'] . "');";
  $con->query($sql);
  header("Location: http://" . $HTTP_HOST
 . $SCRIPT_NAME . "?id=" . $validentry);
}
else {
  echo "not posted any results into comment box";
}

它应该提交表单并在同一页面上显示数据

2 个答案:

答案 0 :(得分:0)

按照http://192.168.1.100/studentnameid.txt中提供的格式,使用准备好的语句尝试以下操作,将您的原始代码清理干净。

注意:blog_id = ''-我没有在表单提交的查询字符串中使用$validentry(删除了操作目标并将其设置为"#")。一旦您完成了基本表单提交的工作(存储在数据库表中),然后调试如何将$validentry用作blog_id

<h3>Leave a comment</h3>
<form action="#" method="post">
    <table>
        <tr>
            <td>Your name</td>
            <td><input type="text" name="name"></td>
        </tr>
        <tr>
            <td>Comments</td>
            <td><textarea name="comment" rows="10" cols="50"></textarea></td>
        </tr>
        <tr>
            <td></td>
            <td><input type="submit" name="submit" value="Add comment"></td>
        </tr>
    </table>
</form>


<?php
if(!empty($_POST['submit'])) {
    $conn = mysqli_connect("localhost", "root", "root", "blog");

    /* check connection */
    if (!$conn) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }

    $stmt = mysqli_prepare($conn, "INSERT INTO comments (blog_id, dateposted, name, comment) VALUES (?, ?, ?, ?)");
    mysqli_stmt_bind_param($stmt, 'ssss', $blog_id, $dateposted, $name, $comment);

    $blog_id = '';
    $dateposted = date("Y-m-d H:i:s"); // the MySQL DATETIME format
    $name = $_POST['name'];
    $comment = $_POST['comment'];

    /* execute prepared statement */
    mysqli_stmt_execute($stmt);

    printf("%d Row inserted.\n", mysqli_stmt_affected_rows($stmt));

    /* close statement and connection */
    mysqli_stmt_close($stmt);

// redirect...

}

如果需要检查在PhpMyAdmin中设置的数据库/表,请在左侧面板中选择数据库,然后选择要查看的表。点击标签“结构”以调查表的列名称,类型等。

一个例子:带有表“ chat”及其属性的“ chat”数据库的屏幕快照(显示在“结构”选项卡下)。 the docs - bind params

答案 1 :(得分:-1)

尝试将查询修改为这样:

if(isset($_POST['submit'])) {
    var_dump($_POST);
    die();
    //$con = mysqli_connect("localhost", "root", "root", "blog");
    if($con){
       //$con->select_db("blog");
      // $sql = "INSERT INTO comments(blog_id, dateposted,
       //name, comment) VALUES(" .
     // $validentry . ", NOW(), '" . $_POST['name']
    // . "', '" . $_POST['comments'] . "');";
      //mysqli_query($con,$sql);
      //mysqli_close($con);
} else {
    echo "Connection error";
}  

}