我无法将数据插入数据库
Posted
技术标签:
【中文标题】我无法将数据插入数据库【英文标题】:I can not make the data insert into the database 【发布时间】:2013-03-02 21:10:00 【问题描述】:我正在创建一个评论表单,它将询问用户他的姓名、电子邮件并使用 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";
?>
index.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" 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>
【问题讨论】:
你在代码中输入的 var_dump 的输出是什么? 我从第一个得到错误,如果这意味着来自 if (isset($_POST['submit']))... 但如果我删除第一个 if 语句我得到这个 谢谢。我们将尽快与您联系 object(mysqli_stmt)[2] public 'affected_rows' => null public 'insert_id' => null public 'num_rows' => null public 'param_count' => null public 'field_count' = > null public 'errno' => null public 'error' => null public 'error_list' => null public 'sqlstate' => null public 'id' => null 此代码与 OP 在另一个问题中的帖子相似/相同。我在我的服务器上安装了脚本,它们确实可以工作。 查看相关帖子:***.com/questions/15389888/php-mysql-insert-error 【参考方案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 的东西)。
我的代码版本 这是我在服务器上工作的内容。 请查看 cmets。 剪切和粘贴时要小心,以免引入任何无关字符。
<?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.";
?>
【讨论】:
我更新了我的问题,查看 submit_to_db.php 文件,系统向我打印出消息一个大错误,这意味着提交从未发布......这怎么可能?? 哦,不错。将 print_r 移到 if 语句之前,请参阅上面的更新代码。 我按照你说的做了,我得到了这个 echo Array ([name_] => test name [email] => test email [cmets] => test cmets) 这是一个很大的错误 您在我们的脚本中仍然存在与if($stmt)
相关的错误 - 我之前在您回复的其他问题中讨论过这个问题。请查看我以前的 cmets。
因为您使用的是 AJAX,所以 submit
名称不是 POST 数组的一部分...目前,请删除该 if
语句,因为它会妨碍您。您可以稍后添加验证步骤。【参考方案2】:
请将您的查询更改如下:
$query = "INSERT into comments(`email`, `comments`) VALUES('$email', '$comments')";
希望对你有帮助
【讨论】:
试试这个:在 PHP 脚本中回显查询并在成功函数中提醒result
,让我知道您的查询是否正确
这是警报结果中的几行谢谢。我们会尽快与您联系object(mysqli_stmt)[2] public 'affected_rows' => null public 'insert_id' => null public 'param_count' => null public 'num_rows' => null
以上是关于我无法将数据插入数据库的主要内容,如果未能解决你的问题,请参考以下文章