Home >Backend Development >PHP Tutorial >After mysql UPDATE, use mysql_affected_rows to determine whether it is successful. If not, INSERT will result. Something went wrong.

After mysql UPDATE, use mysql_affected_rows to determine whether it is successful. If not, INSERT will result. Something went wrong.

WBOY
WBOYOriginal
2016-07-06 13:53:512018browse

After mysql UPDATE, judge whether it is successful through mysql_affected_rows. If not, then INSERT. The result is a problem. The number of table rows has doubled. The code is as follows, and the solution is

<code>//数据写入数据库
   function save_db($currencyname_en,$currencyname_cn,$buyingrate,$sellingrate,$middlerate)
   {
        //更新
        $query_update = ' UPDATE '. TABLENAME .' SET '
                       .' buyingrate=' .$buyingrate .','
                       .' sellingrate=' .$sellingrate .','
                       .' middlerate=' .$middlerate .','
                       .' lastupdatetime="' .date('Y-m-d H:i:s',time()) .'"'
                       .' WHERE currencyname_en="' .$currencyname_en .'"';
        mysql_query($query_update);

        if(mysql_affected_rows()!=1)//更新不成功,尝试插入        
        {
            $query_insert = 'INSERT INTO ' . TABLENAME . '(currencyname_en, currencyname_cn, buyingrate, sellingrate, middlerate, lastupdatetime) VALUES (                   
                                            "'.$currencyname_en.'",
                                            "'.$currencyname_cn.'",
                                            "'.$buyingrate.'",
                                            "'.$sellingrate.'",
                                            "'.$middlerate.'",
                                            "'.date('Y-m-d H:i:s',time()).'"
            )';
            mysql_query($query_insert);
        }

        if(mysql_affected_rows()!=1)
        {
            $err .= "<br>插入或者更新".$currencyname_en."-".$key."currencyname_cn";
        }

   }</code>

Reply content:

After mysql UPDATE, judge whether it is successful through mysql_affected_rows. If not, then INSERT. The result is a problem. The number of table rows has doubled. The code is as follows, and the solution is

<code>//数据写入数据库
   function save_db($currencyname_en,$currencyname_cn,$buyingrate,$sellingrate,$middlerate)
   {
        //更新
        $query_update = ' UPDATE '. TABLENAME .' SET '
                       .' buyingrate=' .$buyingrate .','
                       .' sellingrate=' .$sellingrate .','
                       .' middlerate=' .$middlerate .','
                       .' lastupdatetime="' .date('Y-m-d H:i:s',time()) .'"'
                       .' WHERE currencyname_en="' .$currencyname_en .'"';
        mysql_query($query_update);

        if(mysql_affected_rows()!=1)//更新不成功,尝试插入        
        {
            $query_insert = 'INSERT INTO ' . TABLENAME . '(currencyname_en, currencyname_cn, buyingrate, sellingrate, middlerate, lastupdatetime) VALUES (                   
                                            "'.$currencyname_en.'",
                                            "'.$currencyname_cn.'",
                                            "'.$buyingrate.'",
                                            "'.$sellingrate.'",
                                            "'.$middlerate.'",
                                            "'.date('Y-m-d H:i:s',time()).'"
            )';
            mysql_query($query_insert);
        }

        if(mysql_affected_rows()!=1)
        {
            $err .= "<br>插入或者更新".$currencyname_en."-".$key."currencyname_cn";
        }

   }</code>

<code class="SQL">INSERT INTO table
(...)
VALUES
(...)
ON DUPLICATE KEY UPDATE field1 = ?, field2 = ?, ...</code>

Would this be easier?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn