我是PDO的新手。我如何进行UPDATE SQL命令? 给出的错误消息 - 无法运行查询:SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中有错误;查看与您的MySQL服务器版本对应的手册,以便在'(电子邮件,给定名,姓氏',第1行附近使用正确的语法
<?php
ob_start();
require("config.php");
if(!empty($_POST))
{
// Check if the username is already taken
$query = "
SELECT
1
FROM users
WHERE
email = :email
";
$query_params = array( ':email' => $_POST['email'] );
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex){ die("Failed to run query: " . $ex->getMessage()); }
$row = $stmt->fetch();
if($row){ die("This email is already in use"); }
// Check if the mobile number is already taken
$query = "
SELECT
1
FROM users
WHERE
mobilenumber = :mobilenumber
";
$query_params = array( ':mobilenumber' => $_POST['mobilenumber'] );
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex){ die("Failed to run query: " . $ex->getMessage()); }
$row = $stmt->fetch();
if($row){ die("This Mobile Number is already in use"); }
//Add row to database
$query = "
UPDATE users (
email,
givenname,
surname,
gender,
dateofbirth,
nric,
nationality,
race,
address,
postalcode,
mobilenumber,
profession
) SET (
:email,
:givenname,
:surname,
:gender,
:dateofbirth,
:nric,
:nationality,
:race,
:address,
:postalcode,
:mobilenumber,
:profession
WHERE (
'id' = $id)
)
";
$query_params = array(
':email' => $_POST['email'],
':givenname' => ucwords($_POST['givenname']),
':surname' => ucwords($_POST['surname']),
':gender' => $_POST['gender'],
':dateofbirth' => $_POST['dateofbirth'],
':nric' => strtoupper($_POST['nric']),
':nationality' => $_POST['nationality'],
':race' => ucwords($_POST['race']),
':address' => ucwords($_POST['address']),
':postalcode' => $_POST['postalcode'],
':mobilenumber' => $_POST['mobilenumber'],
':profession' => ucwords($_POST['profession'])
);
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex){ die("Failed to run query: " . $ex->getMessage()); }
header("Location: myaccount.php");
die("Redirecting to myaccount.php");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<link rel="shortcut icon" href="../../assets/ico/favicon.png">
<title>Homepage</title>
<!-- Bootstrap core CSS -->
<link href="css/bootstrap.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="../../assets/js/html5shiv.js"></script>
<script src="../../assets/js/respond.min.js"></script>
<![endif]-->
<style>
#tablenotcenter {
border-collapse: collapse;
border-spacing: 0;
width: 100%;
text-align:left;
}
</style>
</head>
<body>
<?php
if (empty($_SESSION['user']))
{
include_once("header.php");
}
else {
include_once("header2.php");
}?>
<div class="page-header">
<div class="container">
<h1> My Account </h1>
<hr class="colorgraph">
<?php $username123 = htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8'); ?>
<?php
include ("DBScripts/DB.php");
$conn = dbConnect();
if (!$conn)
die("Couldn't connect to MySQL");
$result = mysql_query("SELECT * from users WHERE username='$username123'");
$row = mysql_fetch_assoc($result);
$id= $row["id"];
$username= $row["username"];
$email = $row["email"];
$givenname= $row["givenname"];
$surname = $row["surname"];
$gender = $row["gender"];
$dateofbirth= $row["dateofbirth"];
$nric = $row["nric"];
$nationality = $row["nationality"];
$race = $row["race"];
$address = $row["address"];
$postalcode = $row["postalcode"];
$mobilenumber = $row["mobilenumber"];
$profession = $row["profession"];
$membersince = $row["membersince"];
$lastlogin = $row["lastlogin"];
?>
<form method="post">
<table class="table table-hover" id="tablenotcenter" border="0">
<tr >
<td><div class="pull-left"><h3 class="text-primary"> Personal Information </h3></div></td>
<td> </td>
<td colspan="2"> </td>
</tr>
<tr>
<td><strong>Given Name:</strong></td>
<td> <input name="givenname" type="text" maxlength="20" value="<?echo $givenname?>" style="width: 250px; height: 20px" pattern="[A-Za-z]{1,20}" required/></td>
<td><strong>Race</strong></td>
<td><input name="race" type="text" maxlength="20" value="<? echo $race ?>" style="width: 250px; height: 20px" pattern="[A-Za-z]{1,20}" required/></td>
</tr>
<tr>
<td><strong>Surname:</strong></td>
<td><input name="surname" type="text" maxlength="20" value="<? echo $surname ?>" style="width: 250px; height: 20px" pattern="[A-Za-z]{1,20}" required/></td>
<td><strong>Address</strong></td>
<td><input name="address" type="text" maxlength="50" value="<? echo $address ?>" style="width: 250px; height: 20px" required/></td>
</tr>
<tr>
<td><strong>Gender:</strong></td>
<td> <?
if ($gender =="M")
{ ?>
<div class="radio-inline">
<label>
<input type="radio" name="gender" id="male" value="male" checked required>
Male
</label>
</div>
<div class="radio-inline">
<label>
<input type="radio" name="gender" id="female" value="Female" required>
Female
</label>
</div>
<? } else {?>
<div class="radio-inline">
<label>
<input type="radio" name="gender" id="male" value="Male" required>
Male
</label>
</div>
<div class="radio-inline">
<label>
<input type="radio" name="gender" id="female" value="Female" checked required>
Female
</label>
</div>
<? }?>
</td>
<td><strong>Postal Code</strong></td>
<td><input name="postalcode" type="text" maxlength="20" value="<? echo $postalcode ?>" style="width: 250px; height: 20px" pattern="[0-9]{6}" required/></td>
</tr>
<tr>
<td><strong>Date Of Birth:</strong></td>
<td> <input type="date" name="dateofbirth" value="<? echo $dateofbirth?>" max="1997-12-31" required> </td>
<td><strong>Mobile Number</strong></td>
<td><input name="mobilenumber" type="text" maxlength="20" value="<? echo $mobilenumber ?>" style="width: 250px; height: 20px" pattern="[0-9]{8}" required/></td>
</tr>
<tr>
<td><strong>Nationality:</strong></td>
<td> <input name="nationality" type="text" maxlength="20" value="<? echo $nationality ?>" style="width: 250px; height: 20px" required/> </td>
<td><strong>Profession:</strong></td>
<td><input name="profession" type="text" maxlength="20" value="<? echo $profession ?>" style="width: 250px; height: 20px" pattern="[A-Za-z]{1,20}" required/></td>
</tr>
<tr>
<td colspan="4"><div class="pull-left"><h3 class="text-primary"> Account Setting </h3></div></td>
</tr>
<tr>
<td><strong>ID</strong></td>
<td><? echo $id?></td>
<td><strong>Email:</strong></td>
<td><input name="email" type="text" maxlength="20" value="<? echo $email ?>" style="width: 250px; height: 20px" pattern="[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}" required/></td>
</tr>
<tr>
<td><strong>Password:</strong></td>
<td><a href= changepassword.php> <span class="label label-primary">Change Password</span>
</a></td>
<td><strong>Memeber Since</strong></td>
<td><? echo $membersince ?></td>
<table border="0">
<tr>
<td><input name="id" type="hidden" maxlength="20" value="<? echo $id?>" style="width: 250px; height: 20px" required/></td>
<td> </td>
<td><div class="pull-right"><a href="myaccountedit.php"> <button name="submit" type="submit" class="btn btn-success">Save</button></a> </div></td>
</tr>
</table>
</form>
</div>
</div>
<?php include_once("footer.php");?>
</body>
<? ob_flush()?>
</html>
答案 0 :(得分:0)
您的UPDATE在语法上是不正确的。你应该这样做:
UPDATE users
SET email = :email,
givenname = :givenname,
surname = :surname,
gender = :gender,
dateofbirth = :dateofbirth,
nric = :nric,
nationality = :nationality,
race = :race,
address = :address,
postalcode = :postalcode,
mobilenumber = :mobilenumber,
profession = :profession
WHERE id = $ID
答案 1 :(得分:0)
这是更新的错误语法 - 更新是
update `table` set field = value, another_field = another_value
你所拥有的是插入
的语法