Php Mysql插入查询问题
我对此查询有问题,由于某种原因,它没有插入。有什么问题吗Php Mysql插入查询问题,php,html,mysql,Php,Html,Mysql,我对此查询有问题,由于某种原因,它没有插入。有什么问题吗 <?php if ( isset( $_POST['submit'] ) ) { $email = $_POST['email']; $name = $_POST['name']; $comment = $_POST['comment']; if($email && $name ) { i
<?php
if ( isset( $_POST['submit'] ) ) {
$email = $_POST['email'];
$name = $_POST['name'];
$comment = $_POST['comment'];
if($email && $name ) {
if($comment) {
$connect = mysql_connect("dragon.kent.ac.uk", "repo", "3tpyril");
mysql_select_db("repo");
$query = mysql_query("INSERT into comments(comment, name,email, newsitemId) VALUES ('$email', '$name','$comment',':newsitemId'=> $_POST[newsitemId])");
echo '<script type="text/javascript">alert("Your blog post has been added");</script>';
} else {
echo '<script type="text/javascript">alert("Please provide some details");</script>';
}
} else {
echo '<script type="text/javascript">alert("All fields required");</script>';
}
}
?>
是。你们试图使用一些类似于事先准备好的陈述,但完全错误的方式。这应该可以解决这个问题:
$query = mysql_query("INSERT into comments(comment, name,email, newsitemId) VALUES ('$email', '$name','$comment','" . mysql_real_escape_string($_POST['newsitemId']) . "')");
要使用prepared语句,您必须使用,其主要思想是将变量绑定到查询中,如下所示:
$query = 'INSERT INTO table (column) VALUES (:myValue);';
$binds = array(
'myValue' => $_POST['myValue']
);
$db->Execute($query, $binds);
(这只是说明它是如何工作的,而不是示例本身)
编辑:在将来,如果您仍在使用mysql_*函数,并且您的SQL查询看起来像被破坏了一样,请使用函数(获取错误代码)和(获取错误消息)。它将告诉您的查询有什么问题。试试这个
"INSERT into comments(comment, name,email, newsitemId) VALUES ('$email', '$name','$comment','{$_POST['newsitemId']}')"
$query = mysqli_query("INSERT into comments VALUES ('$email', '$name','$comment', '$_POST[newsitemId]')");
您也可以尝试使用这个有效的insert语句,但稍后在现实世界中实现代码时,出于安全原因,必须对其进行编辑
$con=mysqli_connect("hostname","username","password","databasename");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
mysqli_query($con,"INSERT INTO comments VALUES ('$email', '$name','$comment', '$_POST[newsitemId]' );
mysqli_close($con);
}
':newsitemId'=>$\u POST[newsitemId]
这是错误的。试试'newsitemId'=$\u POST[newsitemId]
。mysql_*函数也不推荐使用。请减少您的问题,使其涉及一种语言。不是四个。还要学习缩进代码。这里有一个语法错误:':newsitemId'=>$\u POST[newsitemId])
。如果您有任何错误,也可以使用mysql\u error()
进行检查。无论如何,您应该切换到MySQLi,因为这些功能已被弃用。看起来您是盲目地从internet复制/粘贴了一些代码,而没有花时间了解它的功能。请告诉我们您遇到了什么错误。