0

我正在创建一个评论表单,它将询问用户他的姓名、电子邮件并使用 php 和 mysqli 发表评论以连接到数据库和 jquery 不刷新但问题是:我无法将数据插入数据库但是当我“回显”保存从用户输入的数据的变量时,它可以在不输入数据库的情况下正常工作,我不知道错误是什么。

谁能帮我?

ps: 我不是要求为我编写代码,但我需要知道如何解决它。

submit_to_db.php

<?php
  $conn = new mysqli('localhost', 'root', 'root', 'my_db');
  if(isset($_POST['submit'])){
  $name =isset ($_POST['name']);
  $email = $_POST['email'];
  $comments = $_POST['comments'];

  echo"<pre>";
  print_r($_POST);
  echo"</pre>";

  $query = "INSERT into comments('email', 'comments') VALUES(?, ?)";
  echo $query;
  $stmt = $conn->stmt_init();
  if($stmt->prepare($query)){

     $stmt->bind_param('ss', $email, $comments);
     $stmt->execute();
     //var_dump($stmt);

  }

  if($stmt){

  echo "thank you .we will be in touch soon <br />";
 // echo $_POST['name'];
  //echo $_POST['email'];
  //echo $_POST['comments'];
  var_dump($stmt);
  }
  else{
   echo "there was an error. try again later.";
   }  

}
else
   echo"it is a big error";
?>

索引.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>feedback page</title>
<script type = "text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<link rel ="stylesheet" href = "css/default.css" />

<script type = "text/javascript">

$(function(){

   $('#submit').click(function(){
     $('#container').append('<img src = "img/loading.gif" alt="Currently loading" id = "loading" />');

         var name = $('#name').val();
         var email = $('#email').val();
         var comments = $('#comments').val();


         $.ajax({

            url: 'submit_to_db.php',
            type: 'POST',
            data: 'name =' + name + '&email=' + email + '&comments=' + comments,

            success: function(result){
                 $('#response').remove();
                 $('#container').append('<p id = "response">' + result + '</p>');
                 $('#loading').fadeOut(500, function(){
                     $(this).remove();
                 });

            }

         });         

        return false;

   });


});

</script>




</head>

<body>
   <form action = "submit_to_db.php" method = "post">
   <div id = "container">
      <label for = "name">Name</label>
      <input type = "text" name = "name" id = "name" />

      <label for = "email">Email address</label>
      <input type = "text" name = "email" id = "email" />

      <label for = "comments">Comments</label>
      <textarea rows = "5"cols = "35" name = "comments" id = "comments"></textarea>
      <br />

      <input type = "submit" name = "submit" id = "submit" value = "send feedBack" />
    </div>
   </form>




</body>
</html>
4

2 回答 2

1

错误警报!

如果你这样做:

$query = "INSERT into comments(email, comments) VALUES($email, $comments)";

然后这样做:

 $stmt->bind_param('sss', $email, $comments);

您可能会收到一条错误消息。您需要在查询字符串中有绑定占位符,如下所示:

$query = "INSERT into comments(email, comments) VALUES(?,?)";

并且由于您只使用两个字段,因此您的绑定语句应为:

 $stmt->bind_param('ss', $email, $comments);

调试策略

 // Print out your post variable and look at it carefully...
 echo "<pre>";
 print_r($_POST);
 echo "</pre>";

 if(isset($_POST['submit'])){
     //$name =isset ($_POST['name']);
     $email = $_POST['email'];
     $comments = $_POST['comments'];

请仔细查看名称字段,我昨天发现了一个导致问题的额外/不可见字符。

此外,作为确保插入工作的测试:

 if(isset($_POST['submit'])){
     $name = "Stack Overflow";
     $email = "stack@overflow.com";
     $comments = "Lorem ipsum and so on...";

您需要找出问题是在客户端(表单和 POST 变量)还是在服务器端(MySQL 的东西)。

我的代码版本
这是我在服务器上工作的内容。请查看评论。 剪切和粘贴时要小心,以免引入任何无关字符。

<?php 
    // Echo your POST variable so you can see what the
    // data looks like from your submission form
    echo "<pre>";
    print_r($_POST);
    echo "</pre>";

  $conn = new mysqli('localhost', 'username', 'password', 'database');
  $query = "INSERT into comments(name, email, comments) VALUES(?, ?, ?)";

  // Initially, do a simple insert using static values to make sure 
  // the MySQL statements are working
  // When this works, you can comment these out and uncomment 
  // the statements using the POST array
  $name = "Some Name";
  $email = "some@name.com";
  $comments = "some commentary text...";

  // At this point, you will validate your input, but do this later...
  // After you tested the MySQL, try the form data
  //$name = $_POST['name'];
  //$email = $_POST['email'];
  //$comments = $_POST['comments'];

  $stmt = $conn->stmt_init();
  if($stmt->prepare($query)){
     $stmt->bind_param('sss',$name, $email, $comments);
     $stmt->execute();
  }

  // This tells you if the insert worked...
  printf("%d Row inserted.<br>", $stmt->affected_rows);

  if($stmt->affected_rows){
  echo "thank you .we will be in touch soon";
  }
  else {
   echo "there was an error. try again later.";
  }  
?>
于 2013-03-14T14:28:48.137 回答
-2

请将您的查询更改如下:

$query = "INSERT into comments(`email`, `comments`) VALUES('$email', '$comments')";

希望这对你有帮助

于 2013-03-14T14:03:06.980 回答