php + mysql插入错误 [英] php + mysql insert error
问题描述
我正在创建一个简单的表单注释,允许用户发送评论和插入到数据库使用php mysql jquery在页面中没有刷新所有工作正常,但问题是:
,当我写我注释mysql数据库采取4行,并填写相同的数据在任何人都可以帮助我的错误??
$ b
my_db.sql
-
- 数据库:`my_db`
-
- ------- -------------------------------------------------
-
- 表'comment`的表结构
-
CREATE TABLE IF NOT EXISTS`comments`(
` id` tinyint(4)NOT NULL AUTO_INCREMENT,
`name` varchar(50)NOT NULL,
`email` varchar(70)NOT NULL,
`comment` text NOT NULL,
PRIMARY KEY(`id`)
)ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 5;
-
- 表格`评论'的转储数据
-
插入`comments`(`id`,`name` ,'email`,`comments`)值
(1,'test','test@test.com','testcomments'),
(2,'test','test @ test。 ('test','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 // ENhttp://www.w3.org/TR/ XHTML1 / DTD / XHTML1-transitional.dtd>
< html xmlns =http://www.w3.org/1999/xhtml>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8/>
< title>反馈页面< / title>
< script type =text / javascriptsrc =http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js\">< ;; script>
< link rel =stylesheethref =css / default.css/>
< script type =text / javascript>
$ b $(function(){
$('#submit')。click(function(){
$('#container')。append( '< img src =img / loading.gifalt =目前正在加载id =loading/>');
var name = $('#name')。val ();
var email = $('#email')。val();
var comment = $('#comments')。val();
$ .ajax({
url:'submit_to_db.php',
type:'POST',
data:'name ='+ name +'& email ='+ email +'& comment ='+评论,
成功:功能(结果){
$('#response')。remove();
$('#container')。append('< p id =response>'+ result +'< / p>');
$('#loading')。fadeOut(500,函数(){
$(this).remove();
});
}
});
返回false;
});
});
< / script>
< / head>
< body>
< form action =submit_to_db.phpmethod =post>
< div id =container>
< label for =name>名称< / label>
< input type =textname =nameid =name/>
< label for =email>电子邮件地址< / label>
< input type =textname =emailid =email/>
< label for =评论>评论< / label>
< br />
< input type =submitname =submitid =submitvalue =send feedBack/>
< / div>
< / form>
< / div>
< / body>
< / html>
submit_to_db.php
<?php
$ conn = new mysqli('localhost','root','root','my_db');
$ query =插入评论(名称,电子邮件,评论)VALUES(?,?,?);
$ stmt = $ conn-> stmt_init();
if($ stmt-> prepare($ query)){
$ stmt-> bind_param('sss',$ _POST ['name'],$ _POST ['email '],$ _POST ['comments']);
$ stmt-> execute();
if($ stmt){
echo谢谢,我们会尽快与您联系;
}
else {
echo出现错误,请稍后再试。;
}
?>
我真的把你的脚本加载到我的服务器上,创建了数据库表,并测试了脚本。
每次提交只插入一行。您的脚本正常工作。
但是,您的测试过程中存在一个缺陷。
我也检查了Firebug控制台,并且AJAX调用按预期工作。
除非您有其他诊断,我认为我无法进一步帮助您。
Bug警告
您的在未来几年里,你的生活将会带来巨大的悲痛......
if($ stmt){
回声谢谢你,我们会尽快联系;
}
else {
echo出现错误,请稍后再试。;
$ / code>
如果语句
将始终评估为真,因为查询被执行(除非当然,MySQL没有运行)。
你的目的是看看插入是否成功,为此,你需要检查:
if($ stmt-> affected_rows){...}
$ stmt-> affected_rows
在失败时将返回-1,或者在成功时返回+1。
调试策略
(1)在时间戳中添加一个额外的字段,以便您可以查看记录何时插入。这会让你更深入地了解进入的内容。
(2)清空你的表并重新尝试你的代码。你可能认为你得到了多个插入,但也可能是你点击了提交按钮4次。
i am creating a simple form comment that will allow user to send a comment and inserti into the database using php mysql jquery for no refreshing in the page all work fine but the problem is : that when i write i comment the mysql database take 4 lines and fill the same data in it can anyone help me with error??
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 // where the form and the function of 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.";
}
?>
I actually took your scripts, loaded them onto my server, created the database table, and tested the script.
Only one row is being inserted per submission.
Your script is working correctly.
However, there is a flaw in your testing procedure.
I also checked the Firebug console and the AJAX call is working as expected.
Unless you have other diagnostics, I don't think I can help you any further.
Bug Alert
There is a bug in your code which will cause you immense grief in your life in the years ahead...
if($stmt){
echo "thank you .we will be in touch soon";
}
else{
echo "there was an error. try again later.";
}
This if
statement will always evaluate to true since the query is executed (unless of course, MySQL is not running).
What you intended is to see if the insert was successful, and for that, you need to check:
if($stmt->affected_rows){...}
$stmt->affected_rows
will return -1 upon failure or +1 on success.
Debugging Strategies
(1) Add an extra field in your with a timestamp so that you can see when the records are inserted. This will give you more insight into what is going in.
(2) Empty your table and try your code again. You may think you are getting multiple inserts but it could also be that you hit the submit button 4 times.
这篇关于php + mysql插入错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!