Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的PHP SQL语句有什么问题?_Php_Mysql - Fatal编程技术网

我的PHP SQL语句有什么问题?

我的PHP SQL语句有什么问题?,php,mysql,Php,Mysql,我的sql语句有问题。我不知道我做错了什么,但它一直添加到数据库中,而不是上传 $result = mysql_query("SELECT id FROM users where fbID=$userID"); if (mysql_num_rows($result) > 0) { mysql_query("UPDATE users SET firstName='$firstName' , lastName='$lastN

我的sql语句有问题。我不知道我做错了什么,但它一直添加到数据库中,而不是上传

$result = mysql_query("SELECT id FROM users where fbID=$userID");
if (mysql_num_rows($result) > 0) {
  mysql_query("UPDATE users 
               SET firstName='$firstName'
                 , lastName='$lastName'
                 , facebookURL='$link'
                 , birthday='$birthday'
                 , update='$today'
                 , accessToken='$accessToken'
                 , parentEmailOne='$parentEmailOne'
               , WHERE fbID='$userID'");
} else {
  mysql_query("INSERT INTO users 
                 (fbID, firstName, lastName, facebookURL, birthday
                 , updated, accessToken, parentEmailOne ) 
              VALUES ('$userId', '$firstName', '$lastName', '$link', '$birthday'
                 , '$today', '$accessToken', '$parentEmailOne')");
}

如果userID列是一个varchar,您应该在第一个查询中引用$userID变量

我发现在第一个查询中您使用$userID,而在INSERT中您使用$userID

您需要在第一个查询中引用,
fbID='$userID'

另外,在第二个SQL语句中,在
where
之前不需要这个


最后,您在第一次引用中使用了
userID
,在最后一次引用中使用了
userID
,您是说它是插入而不是更新的吗?换句话说,它无法找到您期望它找到的现有记录


我建议您不要自己执行“如果记录存在,则更新,否则插入”逻辑,而是查看MySQL。

更新是关键字,必须使用from分隔符。 第一个查询中的一个逗号是额外的

$result = mysql_query("SELECT `id` FROM `users` where `fbID`=$userID");
if (mysql_num_rows($result) > 0) {
mysql_query("UPDATE `users` SET `firstName`='$firstName', `lastName`='$lastName', `facebookURL`='$link', `birthday`='$birthday', `update`='$today', `accessToken`='$accessToken', `parentEmailOne`='$parentEmailOne' WHERE `fbID`='$userID'");
} else {
mysql_query("INSERT INTO `users` (`fbID`, `firstName`, `lastName`, `facebookURL`, `birthday`, `updated`, `accessToken`, `parentEmailOne` ) VALUES ('$userId', '$firstName', '$lastName', '$link', '$birthday', '$today', '$accessToken', '$parentEmailOne')");
}

这是标准代码

在您的第一个(我指的是更新)查询中有一个额外的逗号:


WHERE
子句之前的
UPDATE
语句中有一个额外的逗号:

parentEmailOne='$parentEmailOne', WHERE fbID='$userID'"
                              ^^^^
但是,您还应该确保您的变量
$userID
不是空的,并回显
mysql\u num\u rows()


另外,在
选择中使用变量
$userID
,但在
插入中使用的是
$userID
。请注意大小写的差异。

名称是否包含任何撇号


您需要确保使用

fbID列的类型是什么?您的意思是插入而不是更新吗?上传是不明确的,暂时将第一行更改为
mysql\u query(“从用户中选择id,其中fbID=$userID”)或die(mysql\u error())parentEmailOne='$parentEmailOne', WHERE fbID='$userID'"
                              ^^^^