MySQL:查询语法错误和2个语句

时间:2013-03-14 14:02:15

标签: php mysql syntax-error

我的剧本有问题:

<?php
include('includes/header.php');
include("includes/config.php");
$email = $_GET['email'];
$key = $_GET['key'];
$email = strip_tags($email); 
$email = trim($email);
$key = strip_tags($key); 
$key = trim($key);
$sql = mysql_query("SELECT * FROM confirm WHERE email = '$email' AND key = '$key' 
ORDER BY id ASC LIMIT 1")or die(mysql_error()); 
while($result = mysql_fetch_array($sql))
{ 
$key2 = $result['key']; 
$email2 = $result['email'];  
}
if($key == $key2 && $email == $email2){
$sql = mysql_query("UPDATE users SET user_confirm = 1 WHERE user_mail = '$email2'")or  
die(mysql_error());
$sql = mysql_query("DELETE FROM confirm WHERE email = '$email2'")or die(mysql_error());
echo "Your account have been activated, and is ready to use!";
}else{
echo 'You have entered wrong key or the key is invalid!';
}
include('includes/footer.php');
?>

我在'key =(keyvalue here)'

附近收到语法错误

任何人都知道我在这里做错了什么?

3 个答案:

答案 0 :(得分:1)

如果你的列名是“key”,那么问题KEY是MySQL 5.1中的旧操作符。您可以在列名称周围添加反引号以使其正常工作

`key` = 'some value'

答案 1 :(得分:1)

'key'是MySQL中的保留字。

将您的第一个查询更改为以下内容(在关键字周围查看正确的引号)

$sql = mysql_query("SELECT * FROM confirm WHERE email = '$email' AND `key` = '$key' ORDER BY id ASC LIMIT 1")or die(mysql_error()); 

更好的解决方案是不使用mysql保留字。因此,您可以重命名“确认”表中的“密钥”列。

这是一个MySQL保留字列表: http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

答案 2 :(得分:0)

将列名编辑为cnf_key,然后尝试替换beloow syntex

$sql = mysql_query("SELECT * FROM confirm WHERE email = '$email' AND `cnf_key`= '$key' 
ORDER BY id ASC LIMIT 0,1")or die(mysql_error());