php + mysql插入错误

标签 php jquery mysql forms

我正在创建一个简单的表单评论,允许用户发送评论并使用 php mysql jquery 将页面插入到数据库中,以便在页面中不刷新一切正常,但问题是: 当我写的时候我评论 mysql 数据库需要 4 行并在其中填充相同的数据任何人都可以帮助我解决错误吗??

my_db.sql

--
-- Database: `my_db`
--

-- --------------------------------------------------------

--
-- Table structure for table `comments`
--

CREATE TABLE IF NOT EXISTS `comments` (
  `id` tinyint(4) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `email` varchar(70) NOT NULL,
  `comments` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

--
-- Dumping data for table `comments`
--

INSERT INTO `comments` (`id`, `name`, `email`, `comments`) VALUES
(1, 'test', 'test@test.com', 'testcomments'),
(2, 'test', 'test@test.com', 'testcomments'),
(3, 'test', 'test@test.com', 'testcomments'),
(4, 'test', 'test@test.com', 'testcomments');

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

index.php//jquery的形式和功能所在

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



   </div>
</body>
</html>

submit_to_db.php

<?php
  $conn = new mysqli('localhost', 'root', 'root', 'my_db');
  $query = "INSERT into comments(name, email, comments) VALUES(?, ?, ?)";

  $stmt = $conn->stmt_init();
  if($stmt->prepare($query)){

     $stmt->bind_param('sss', $_POST['name'], $_POST['email'], $_POST['comments']);
     $stmt->execute();

  }

  if($stmt){

  echo "thank you .we will be in touch soon";
  }
  else{
   echo "there was an error. try again later.";
   }  


?>

最佳答案

我实际上拿了你的脚本,将它们加载到我的服务器上,创建了数据库表,并测试了脚本。

每次提交只插入一行。

您的脚本运行正常。

但是,您的测试程序存在缺陷。

我还检查了 Firebug 控制台,AJAX 调用按预期工作。

除非您有其他诊断,否则我认为我无法进一步帮助您。

错误警报
您的代码中存在一个错误,这将给您 future 几年的生活带来巨大的痛苦...

if($stmt){
   echo "thank you .we will be in touch soon";
}
else{
    echo "there was an error. try again later.";
}

这个 if 语句在执行查询后将始终评估为真(当然,除非 MySQL 未运行)。

您的目的是查看插入是否成功,为此,您需要检查:

if($stmt->affected_rows){...}

$stmt->affected_rows 失败时返回 -1,成功时返回 +1。

调试策略
(1) 在您的字段中添加一个带有时间戳的额外字段,以便您可以看到插入记录的时间。这将使您更深入地了解正在发生的事情。

(2) 清空您的表格并再次尝试您的代码。您可能认为您收到了多个插入内容,但也可能是您点击了 4 次提交按钮。

关于php + mysql插入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15389888/

相关文章:

javascript - bootstrap 下拉菜单打不开

javascript - 仅添加一次调整大小事件处理程序

php - Ubuntu PHP 存储库会过期吗?

php - 将 mcrypt_generic 转换为 openssl_crypt

javascript - 如果所有值为 0,则不应提交表单 如果从 3 对中选择一对,然后提交表单

mysql - 添加外键约束时出错

Mysql like 和 BOOLEAN MODE (FULLTEXT) 搜索

mysql - @V1在MySQL中是什么意思?

Windows 上的 PHP -> AS400 有简单的方法吗?

php - 如何从 fatal error 中获取调用堆栈?