doucong4535 2011-02-11 02:23
浏览 51
已采纳

无法使用PHP脚本使用ON DUPLICATE KEY UPDATE

Ok I'm starting to spin out now. Too much trial and error has made me rather grumpy.

I am trying to do an INSERT and UPDATE if EXISTS using a loop. I have excluded the loop code from this example to simplify my problem.

Here is the query in my PHP script:

function insertrating($ratingid, $rating){
    $link = resdb::connect();
    $r = mysqli_query($link, "INSERT INTO propertyrating (id,name)" 
                            ."VALUES (\'$ratingid\',\'$rating\')
"
                ."ON DUPLICATE KEY UPDATE
"
                ."name = VALUES (name),
"
                ."description = VALUES (description)
"
                . "");
    if($r > 0){
        return true;
    }
 }
 $mydbclass = new $dbclass();
 $mydbclass->insertrating('3','3 Star');

Table is as follows: id int name varchar(100) description text

I do not want to add description at this stage. Hence no parameters for that column

I have produced the exact same query in phpMyAdmin and the MySQL console, both work. Thus im guessing its something to do with my syntax.

Also, if I get rid of the ON DUPLICATE KEY UPDATE function and parameters it works. However, obviously doesn't update duplicate rows.

Please ask or correct where I am wrong. Thanks.

EDIT: REQUESTED SQL

INSERT INTO 
 propertyrating 
 (
   id,
   name
 ) 
VALUES 
 (
   '3',
   '3 Star'
 ) 
ON DUPLICATE KEY UPDATE
name = VALUES (name),
description = VALUES (description)

AS REQUESTED UNIQUE * PK

Table is as follows:

id int UNIQUE AND PRIMARY KEY name varchar(100) description text

OK GUYS ERROR RETURNED:

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 'UPDATEname = name,description = description

as you can see there is no space between UPDATE and name (it reads, UPDATEname) i added a character space and now all is gravy.

Many thanks for getting me to mysqli_error($link)

not sure how to give answer to zerkms but u all led me in the right direction, first.

Code now stands as:

$r = mysqli_query($link, "INSERT INTO propertyrating (id,name)" 
                        ."VALUES (\'$ratingid\',\'$rating\')
"
            ."ON DUPLICATE KEY UPDATE
"
            ."name = VALUES (name),
"
            ."description = VALUES (description)
"
            . "") or die("Error: ".mysqli_error($link));
  • 写回答

2条回答 默认 最新

  • duanraotun1674 2011-02-11 03:19
    关注

    Put echo mysqli_error($link); right after mysqli_query to get the exact error happened (if any)

    But make sure you've removed this line after this debugging session.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。