2013-02-22 23 views
1

我被困在php/mysql错误,我无法跟踪。 我的表user_like结构与样本数据错误在同时选择和更新在mysql

id user_id song_id like 
1 1  1  1 
2 1  2  0 

0表示厌恶和1表示等。 现在我查询表使用PHP像 http://myhost.net/server/services/songlike.php?uid=1&song_id=1 和我也得到了使用下面的代码片段中的结果。

$query="select * from atr_like where user_id='$uid' and song_id='$songid' limit 1 "; 
$rs = mysql_query($query); 
$row = mysql_fetch_assoc($rs); 
print_r($row); 
echo "<br>"; 
$like=$row['like']; 
echo $like; 

我正在成功获取此值。 我正在做的是,如果像不存在意味着null然后插入像表中。 否则我只是用更新的方式来表示它的价值,如果它是零则一,反之亦然。 在这里,我收到错误。 我的PHP代码是

 if(mysql_num_rows($rs) > 0 && $like!=null) 
     { 
    if($like==1) 
    { 
    // "user has liked it already so dislike it update like to 0 "; 
    $query1="update atr_like set like=0 where user_id='$uid' and song_id='$songid'"; 
    } 
    else 
    { 
    //"user has disliked it previously already so update like to 1 "; 
    $query1="update atr_like set like=1 where user_id='$uid' and song_id='$songid'"; 
    } 

     } 
     else 
     { 
     echo "first time so insert record . user is going to like"; 
     $query1="insert into atr_like (user_id,song_id,like) values ('$uid','$songid',1)"; 
     } 
$rs1 = mysql_query($query1)or die(mysql_error()); 
mysql_close($conn); 

错误消息:

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 'like=1 where user_id='1' and song_id='1375'' at line 1.

它是由于MySQL的问题,你不能更新任何记录,这是目前在先前的查询的SQL选择? 语法看起来不错,但仍然显示语法错误。

+0

请避免使用单 – 2013-02-22 06:30:20

+1

你正在使用MySQL保留名称作为列名,请表前缀名atr_like.like = 1 – Saqueib 2013-02-22 06:31:41

+0

真的感谢花费宝贵的时间。你让你的工作变得简单。 – 2013-02-22 07:24:45

回答

3

你正在使用MySQL保留关键字like

像子句用于指定模式在一列

什么喜欢的是使用SQL简单

expr LIKE pat [ESCAPE 'escape_char']

模式匹配正则表达式比较。返回1(TRUE)或0(FALSE)。如果expr或pat为NULL,则结果为NULL。

所以无论是使用反引号,因此会像列名都更将不使用保留的关键字作为列名

Please, don't use mysql_* functions in new code。他们不再维护and are officially deprecated。了解prepared statements代替,并用PDOMySQLi

恕我直言,只使用PDO


也避免使用单中是否有

$like=$row['like']; 
echo $like; 

我没有看到任何需要创建的变量$like你可以简单echo $row['like'];

+1

+1之前写给我.. – 2013-02-22 06:35:29

+0

感谢NullPointer。真的有用。当然,我会学习新的设计模式和编码风格。 – 2013-02-22 07:25:36

+0

@mayurbhagat你的欢迎声音不错,你可以快速开始形式http://www.youtube.com/feed/UChgECUNwLBQkgeDcb44pMVg ...我看了,这些都很好..你也可以使用聊天..视频的作者可以找到在聊天室..即使他是PHP聊天室的所有者之一 – 2013-02-22 07:28:11

2

变化从

$query1="update atr_like set like=0 where user_id='$uid' and song_id='$songid'"; 

查询到

$query1="update atr_like set `like`=0 where user_id='$uid' and song_id='$songid'"; 

like在MySQL中保留字。如果您将您的列命名为与mysql的保留字相同的名称,则必须使用反向列名称。

也更改此查询。

$query1="insert into atr_like (`user_id`,`song_id`,`like`) values ('$uid','$songid',1)"; 
+0

为什么downvote?解释。 – 2013-02-22 06:32:02

+0

我没有downvote,但也添加了'INSERT'语句在你的答案':D' – 2013-02-22 06:33:28

+3

+1我看到答案是正确的.SO,必须使用'down-vote'一个答案! – Vimalnath 2013-02-22 06:35:34

0

尝试

$query1 = "update atr_like set `like` = 0 where user_id = '".$uid."' and song_id = '".$songid."'"; 

用于插入

$query1 = "insert into atr_like (user_id,song_id,`like`) values ('".$uid."','".$songid."',1)";