我想在我的数据库中导入数据(来自form
),但我发现了这个错误:
Parse error: syntax error, unexpected ';' in /homepages/38/htdocs/index2.php on line 7
,脚本是
<?php
//Connecting to sql db.
$connect = mysqli_connect("","","","");
//Sending form data to sql db.
mysqli_query($connect,"INSERT INTO posts (email, pseudo)
VALUES ('$_POST[email]', '$_POST[pseudo]')";
?>
错误是什么? 谢谢
答案 0 :(得分:1)
$email=$_POST['email'];
$pseudo=$_POST['pseudo'];
mysqli_query($connect,"INSERT INTO `posts` (`email`, `pseudo`) VALUES ('$email', '$pseudo');");
答案 1 :(得分:1)
$_POST[]
变量。mysqli_query()
。(例如) - 输入属性需要像这样
<input type="email" name="email" value="" />
像这样你必须提供所有输入类型。
PHP代码
如果使用mysqli::real_escape_string
,最好使用它来避免SQL注入。
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
$email=mysqli_real_escape_string($con,$_POST['email']);
$pseudo=mysqli_real_escape_string($con,$_POST['pseudo']);
$stmt = "INSERT INTO posts (`email`, `pseudo`)VALUES('".$email."','".$pseudo."')";
$query = mysqli_query($con,$stmt);
if($query)
{
echo "Inserted Successfully";
}
else
{
// Handle Error over here.
}
?>
答案 2 :(得分:0)
你在POST中错过了引用。检查以下代码
<?php
//Connecting to sql db.
$connect = mysqli_connect("","","","");
$sql ="INSERT INTO posts (email, pseudo)VALUES('".$_POST['email']."','".$_POST['pseudo']."')";
//Sending form data to sql db.
mysqli_query($connect,$sql);
?>