我制作了这段代码
<?php
mysql_connect ("localhost","root","root");
mysql_select_db ("new");
$newusername=$_POST ['newusername'];
$newpassword=$_POST ['newpassword'];
$submit=$_POST ['submit'];
if($submit) {
$newaccount= "INSERT INTO users (name,password) VALUES ("$newusername","$newpassword")";
$result=mysql_query($newaccount);
if ($result) {
print "account has been created"."<meta http-equiv="refresh" content="5;login.php">";
}
else {
echo " The account is already exist";
}
}
?>
但它表示第8行的错误是&#34;插入&#34;线
答案 0 :(得分:1)
双引号存在问题。您可以添加连接:
$newaccount = "INSERT INTO users (name,password) VALUES ('" . $newusername . "','" . $newpassword . "')";
P.S。不要使用mysql_ *函数(不推荐使用mysql扩展),而是使用mysqli_ *。
答案 1 :(得分:0)
您使用了错误的引用:
$newaccount= "INSERT INTO users (name,password) VALUES ('$newusername','$newpassword')";
答案 2 :(得分:0)
对引号进行排序并使用mysql_real_escape_string来清理输入(并防止轻松的SQL注入攻击): -
<?php
mysql_connect("localhost","root","root");
mysql_select_db ("new");
$newusername = mysql_real_escape_string($_POST['newusername']);
$newpassword = mysql_real_escape_string($_POST['newpassword']);
$submit = $_POST['submit'];
if($submit)
{
$newaccount = "INSERT INTO users (name, password) VALUES ('$newusername', '$newpassword')";
$result = mysql_query($newaccount);
if ($result)
{
print "account has been created"."<meta http-equiv='refresh' content='5;login.php'>";
}
else
{
echo " The account is already exist";
}
}
?>
答案 3 :(得分:-1)
$newaccount= "INSERT INTO users (name,password) VALUES ("$newusername","$newpassword")";
以强>
$newaccount= "INSERT INTO users (name,password) VALUES ('".$newusername."','".$newpassword."')";
OR TO
$newaccount= "INSERT INTO users (name,password) VALUES ('$newusername','$newpassword')";
答案 4 :(得分:-1)
<?php
mysql_connect ("localhost","root","root");
mysql_select_db ("new");
$newusername=$_POST ['newusername'];
$newpassword=$_POST ['newpassword'];
$submit=$_POST ['submit'];
if($submit) {
$newaccount= "INSERT INTO users (name,password) VALUES ('".$newusername."', '".$newpassword."')";
$result=mysql_query($newaccount);
if ($result) {
print "account has been created"."<meta http-equiv="refresh" content="5;login.php">";
}
else {
echo " The account is already exist";
}
}
?>