SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法错误;
问题描述:
确切的错误:SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法错误;
SQLSTATE[42000]: Syntax error or access violation: 1064 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 '' at line 20
我完全新的SQL和PHP,这样的任务的一部分,我被要求编辑导师的代码插入到我的项目。但是,当我运行代码时,它会在第20行(这是<body>
标记打开的位置)出现SQL语法错误。有人能帮我指出错误吗?我真的很新的这个东西,我提前很抱歉,如果这被看作是“粗鲁”,或者如果我在浪费你的时间< 3.
<!DOCTYPE HTML>
<html>
<head>
<title>"Sign Up" Form:</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!-- Latest compiled and minified CSS -->
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
</head>
<body>
<div id="page-wrapper">
<div class="wrapper style1">
<section id="main" class="container">
<?php
//-------------------------------------------------
$firstNameVal = 'Insert First Name';
$lastNameVal = 'Insert Surname';
$usernameVal = 'Choose a Username';
$passwordVal = '-------';
$emaiAddresslVal = 'Insert email';
$addressVal = 'Insert Address';
$suburbVal = 'Insert Suburb';
$ageVal = '17';
//-------------------------------------------------
//database credentials
define('DBHOST','localhost');
define('DBUSER','root');
define('DBPASS','root');
define('DBNAME','Login');
//new database connection
$db = new PDO("mysql:host=".DBHOST.";port=3306;dbname=".DBNAME, DBUSER, DBPASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//if form has been submitted process it
if(isset($_POST['submit'])){
// ???
$_POST = array_map('stripslashes', $_POST);
//collect form data
extract($_POST);
//very basic validation
if($firstName ==''){
$error[] = 'Please enter a first name.';
}
if($lastName ==''){
$error[] = 'Please enter a last name.';
}
if($username ==''){
$error[] = "Please enter a username";
}
if($password ==''){
$error[] = 'Please enter a password';
}
if($emailAddress ==''){
$error[] = 'Please enter an email address.';
}
if($address ==''){
$error[] = "Please enter your address";
}
if($suburb ==''){
$error[] = 'Please enter your suburb.';
}
if($mobile ==''){
$error[] = 'Please enter your personal mobile number.';
}
if($age == ''){
$error[] = 'Please enter your age!';
}
if(!isset($error)){
try {
//insert into database
$query = $db->prepare('INSERT INTO user(
firstName,
lastName,
username,
password,
emailAddress,
address,
suburb,
age,
mobile)
VALUES (
:firstName,
:lastName,
:username,
:password,
:emailAddress,
:address,
:suburb,
:age,
:mobile,
');
$query->execute(array(
':firstName' => $firstName, // processes the values for each form field
':lastName' => $lastName, //
':username' => $username, //
':password' => $password, //
':emailAddress' => $emailAddress, //
':address' => $address, //
':suburb' => $suburb, //
':age' => $age, //
':mobile' => $mobile, //
));
//redirect to index page
header('Location: success.php');
exit;
// if error, it displays error
} catch(PDOException $e) {
echo $e->getMessage();
}
}
}
//check for any errors
if(isset($error)){
echo '<section id="content" class="box" style="background-color: #F4CDCD;">';
echo '<h3>Errors</h3>';
echo '<b><ul>';
foreach($error as $error){
echo '<li>'.$error.'</li>';
}
echo '</ul></b>';
echo '</section>';
}
?>
<form method="post">
<!-- Your Details -->
<section id="content" class="box">
<h3>Your Details</h3>
<div class="row uniform 60%">
<div class="6u 12u(narrower)">
<label for="firstName">First Name</label>
<input type="text" name="firstName" id="firstName" value="<?php if(isset($error)){ echo $_POST['firstName'];}?>" placeholder="First Name" />
</div>
<div class="6u 12u(narrower)">
<label for="lastName">Last Name</label>
<input type="text" name="lastName" id="lastName" value="<?php if(isset($error)){ echo $_POST['lastName'];}?>" placeholder="Last Name" />
</div>
</div>
<div class="row uniform 60%">
<div class="6u 12u(narrower)">
<label for="username">username</label>
<input type="text" name="username" id="username" value="<?php if(isset($error)){ echo $_POST['username'];}?>" placeholder="Userame" />
</div>
<div class="6u 12u(narrower)">
<label for="password">password</label>
<input type="password" name="password" id="password" value="<?php if(isset($error)){ echo $_POST['password'];}?>" placeholder="Password" />
</div>
</div>
<div class="row uniform 60%">
<div class="6u 12u(narrower)">
<label for="emailAddress">Email</label>
<input type="email" name="emailAddress" id="emailAddress" value="<?php if(isset($error)){ echo $_POST['emailAddress'];}?>" placeholder="[email protected]" />
</div>
<div class="6u 12u(narrower)">
<label for="mobile">Mobile</label>
<input type="text" name="mobile" id="mobile" value="<?php if(isset($error)){ echo $_POST['mobile'];}?>" placeholder="1234 567 890" />
</div>
<div class="6u 12u(narrower)">
<label for="age">Age</label>
<input type="text" name="age" id="age" value="<?php if(isset($error)){ echo $_POST['age'];}?>" placeholder="17" />
</div>
</div>
<div class="row uniform 60%">
<div class="6u 12u(narrower)">
<label for="address">Address</label>
<input type="text" name="address" id="address" value="<?php if(isset($error)){ echo $_POST['Address'];}?>" placeholder="21B Baker Street" />
</div>
<div class="6u 12u(narrower)">
<label for="suburb">Mobile</label>
<input type="text" name="suburb" id="suburb" value="<?php if(isset($error)){ echo $_POST['suburb'];}?>" placeholder="marylebone" />
</div>
</div>
</section>
<br>
<!-- Submit Button -->
<section id="content" class="box">
<div class="row uniform 60%">
<div class="12u">
<ul class="actions">
<li style="width: 100%; list-style: none; ">
<input type="submit" name="submit" value="Finish and Submit" style="width: 100%;" />
</li>
</ul>
</div>
</div>
</section>
</form>
</section>
</div>
</div>
<!-- Scripts -->
</body>
</html>
答
请删除从最后,
逗号SQL语句并重试。这意味着更改此:
//insert into database
$query = $db->prepare('INSERT INTO user(
firstName,
lastName,
username,
password,
emailAddress,
address,
suburb,
age,
mobile)
VALUES (
:firstName,
:lastName,
:username,
:password,
:emailAddress,
:address,
:suburb,
:age,
:mobile,
');
这样:
//insert into database
$query = $db->prepare('INSERT INTO user(
firstName,
lastName,
username,
password,
emailAddress,
address,
suburb,
age,
mobile)
VALUES (
:firstName,
:lastName,
:username,
:password,
:emailAddress,
:address,
:suburb,
:age,
:mobile
');
+1
谢谢!我还需要在移动后的逗号后加一个括号,并且它工作正常! –
':移动,'
弗雷德意味着 “额外”','。除了与PHP或javascript不同之外,mysql并不那么优雅。 – Xorifelse
但是,在downvote ..对于新用户的代码格式正确,并且需要解决它的所有内容都在那里。我个人只赞成像downvote按钮这样毫不费力的问题:“这个问题没有显示任何研究工作;它不清楚或不是很有用' – Xorifelse