[英]Fatal error: Uncaught exception 'PDOException' thrown in C:\xampp\htdocs\register.php on line 19
I have received:我收到了:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;致命错误:未捕获的异常 'PDOException' 带有消息 'SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误; check the manual that corresponds to your MariaDB server version for the right syntax to use near '5538ac14bb2ca7514f9f4d8826f3c45e'')' at line 1' in C:\\xampp\\htdocs\\register.php:19 Stack trace: #0 C:\\xampp\\htdocs\\register.php(19): PDO->exec('insert into use...') #1 {main} thrown in C:\\xampp\\htdocs\\register.php on line 19.检查与您的 MariaDB 服务器版本相对应的手册,以获取在 C:\\xampp\\htdocs\\register.php:19 中的第 1 行的 '5538ac14bb2ca7514f9f4d8826f3c45e'')' 附近使用的正确语法:#0 C:\\xampp\\ htdocs\\register.php(19): PDO->exec('insert into use...') #1 {main} 在第 19 行扔到 C:\\xampp\\htdocs\\register.php 中。
How can this be solved, like what should be done?这怎么解决,比如应该怎么做?
<?php
session_start();
// If the form has been submitted
if (isset($_POST['submitted'])){
// Create a database connection
$db = new PDO("mysql:dbname=johnsoa7_db;host=localhost", "root", "");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Get and sanitise the inputs, we don't need to do
// this with the password as we hash it anyway
$safe_forename = $db->quote($_POST['forename']);
$safe_lastname = $db->quote($_POST['lastname']);
$safe_email = $db->quote($_POST['email']);
$hashed_password = $db->quote(md5($_POST['password']));
// Insert the entry into the database
$query = "insert into users values (default, $safe_forename, $safe_lastname, $safe_email, '$hashed_password')";
$db->exec($query);
// Get the ID
$id = $db->lastInsertId();
// Output success or the errors
echo "Congratulations! You are now registered. Your ID is: $id";
}
?>
You have an error in this line:你在这一行有一个错误:
$query = "insert into users values (default, $safe_forename, $safe_lastname, $safe_email,'$hashed_password')";
default
should be quoted if it is string.如果它是字符串,则应引用default
。
If it is a variable, you missed $
.如果它是一个变量,你就错过了$
。
Please see the comment by @ceejayoz:请参阅@ceejayoz 的评论:
As he said you don't need quoted around $hashed_password...正如他所说,您不需要在 $hashed_password 周围引用...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.