gpt4 book ai didi

php - 在 PHP 中调试 SQL 语法错误

转载 作者:行者123 更新时间:2023-11-29 05:19:46 25 4
gpt4 key购买 nike

我试图在我的注册和登录页面上使用 PDO,并开始在我的注册页面上工作,但出现语法错误,不知道为什么。

错误如下:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO users (id, name, username, password, lastname ,email) VALUES (?, ?,' at line 1

代码:

 if ($_SESSION["username"]){

header("Location: home.php");
}
else{
if(isset($_POST['firstname']) && isset($_POST['lastname']) && isset($_POST['username']) && isset($_POST['password']) && isset($_POST['cpassword']) && isset($_POST['email'])){
require('connect.php');

$dusername = mysql_real_escape_string($_POST["username"]);
$dfirstname = mysql_real_escape_string($_POST["firstname"]);
$dpassword = md5(mysql_real_escape_string($_POST["password"]));
$dlastname =mysql_real_escape_string($_POST["lastname"]);
$demail =mysql_real_escape_string($_POST["email"]);




$dbhost = "xxxxxxx";
$dbname= "xxxxxx";
$dbuser = "xxxx";
$mysql_password = "xxxxx";



try{


$db = new PDO('mysql:host='.$dbhost.';dbname='.$dbname.';charset=utf8',

''.$dbuser.'',

''.$mysql_password.'');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

}

catch(PDOException $e){
echo $e->getMessage();
die();


}

mysql_error();
//According to user's input
$register =mysql_query( "(INSERT INTO users (id, name, username, password, lastname,email) VALUES (?, ?, ?, ?, ?)")or die(mysql_error());
$register =$db->prepare($register);
$register->execute(array($dfirstname,$dusername,$dpassword,$dlastname,$demail));

// Check username and password match
if ($register) {
// Set username session variable
$_SESSION['username'] = $_POST['username'];
// Jump to secured page
header('Location: home.php');
exit;
}
else{

echo "<div class=\"error\">Error Signing Up... Make Sure all the require

fields are correctly formatted. </div>";
}
}
else{

}
}

更新了

  $register ="INSERT INTO users (id, name, username, password, lastname ,email) VALUES('',?, ?, ?, ?, ?)"or die(mysql_error());
$register =$db->prepare($register);
$register->execute(array('',$dfirstname,$dusername,$dpassword,$dlastname,$demail));

还是有错误

最佳答案

$register->execute(array($dfirstname,$dusername,$dpassword,$dlastname,$demail));

应该是

$register->execute(array('',$dfirstname,$dusername,$dpassword,$dlastname,$demail));

关于php - 在 PHP 中调试 SQL 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27540817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com