【问题标题】:Can not to store form data into database PHP (instead i got success message )不能将表单数据存储到数据库 PHP 中(而是我收到了成功消息)
【发布时间】:2017-01-26 06:55:08
【问题描述】:

大家好,请帮助我,我的代码有问题。我想将我的表单数据存储到数据库中,但我无法获得成功消息(感谢您的评论) 请帮帮我

我的表单代码是这样的

com_form.php

<form method='post'>
  Name: <input type='text' name='name' id='name' /><br />

  Email: <input type='text' name='email' id='email' /><br />

  Comment:<br />
  <textarea name='comment' id='comment'></textarea><br />

  <input type='hidden' name='articleid' id='articleid' value='<? echo $_GET["id"]; ?>' />

  <input type='submit' value='Submit' />
</form>
</form>

PHP 代码是这样的

ma​​nage_com.php

    <?
if( $_POST )
{
  $con=mysqli_connect("localhost","root","password","dbname");
  // Check connection
  if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

  $sql="SELECT name,email,comment FROM comments ";
  $retval=mysqli_query($con,$sql);


  $users_name = $_POST['name'];
  $users_email = $_POST['email'];
  $users_comment = $_POST['comment'];

  $users_name = mysqli_real_escape_string($con, $_POST['$users_name']);
  $users_email = mysqli_real_escape_string($con, $_POST['$users_email']);
  $users_comment = mysqli_real_escape_string($con, $_POST['$users_comment']);

  $articleid = $_GET['id'];
  if( ! is_numeric($articleid) )
    die('invalid article id');

  $query = "
  INSERT INTO `pricemom_comment`.`comments` (`id`, `name`, `email`,
        `comment`, `post_date`, `articleid`) VALUES (NULL, '$users_name',
        '$users_email', '$users_comment',
        CURRENT_TIMESTAMP, '$articleid');";

  mysqli_query($con,$query);

  echo "<h2>Thank you for your Comment!</h2>";

  mysqli_close($con);
}
?>

我想显示表单的页面 page1.php

<? include("manage_com.php"); ?>

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>page1 created by Afzal</title>
  </head>
  <body>

    <img src="nature.jpg" alt="beautifull nature image" style="width:700px; align:center;height:200px;">
    <h2>page1 created by Afzal</h2>
    <p style="width:700px;">order to display comments on a page, we first need to know what comments to show. When we setup our site we created two pages, and each page was assigned a unique id number. This ID number will be used to gather comments for that specific page. For example, when the user is on page 1, we'll select all of the comments in the database assigned to page "1".</p>

     <p style="width:700px;"> Now that we have our sample SQL query, we can use it to create the php code that will print all comments on a page. Below is the example code that we created. If you're not familiar with php, any line that begins with a // is a comment, and comments are used by developers to document their code. In our example, we have quite a few comments to help explain what the code is doing, but keep in mind that most scripts do not have as many comments.</p>

      <?php

include("com_form.php");
       ?>
  </body>

</html>

请解决我的问题。我哪里出错了

注意:我收到了成功消息(感谢您的评论)但评论未存储在我的数据库中

【问题讨论】:

  • mysqli_query($con,$query); 如果由于某种原因失败了怎么办?添加一些错误报告

标签: php database forms connection submit


【解决方案1】:

无论发生什么,您的 echo 语句都会显示成功。 执行查询并检查错误:

  if (!mysqli_query($con,$query))
  {
     echo("Error description: " . mysqli_error($con)); die;
  }

【讨论】:

    【解决方案2】:

    我认为问题与错误的 sql 请求有关。您尝试为 id 字段插入 NUll 值。

    【讨论】:

      猜你喜欢
      • 2020-02-15
      • 1970-01-01
      • 2021-07-02
      • 2021-04-26
      • 2016-09-15
      • 1970-01-01
      • 2019-08-31
      • 2016-01-21
      • 1970-01-01
      相关资源
      最近更新 更多