2013-11-01 24 views
0

我想扔我的表nonbulkmdu並看看r10database,以查找是否有重複,如果有它會更新4個字段,如果它不是它會插入一個新的行。我不斷收到錯誤PHP的MySql更新其他插入錯誤「警告:mysql_result()」

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in on line 19-23.

我在做什麼錯?

<?php 
$username=""; 
$password=""; 
$database=""; 
$link = mysql_connect(localhost,$username,$password); 


mysql_select_db($database) or die("Unable to select database"); 

$query="SELECT * FROM nonbulkmdu"; 

if ($result=mysql_query($query, $link)) { 
    $num=mysql_numrows($result); 

    $i=0; 
    while ($i < $num) { 
     $address=strtoupper(mysql_result($result,$i,"address")); 
     $drops=mysql_result($result,$i,"drops"); 
     $city=mysql_result($result,$i,"city"); 
     $citycode=mysql_result($result,$i,"citycode"); 
     $feature_type=mysql_result($result,$i,"Feature_Type"); 

     $result = mysql_query("update r10_database 
           set drops=$drops, citycode=$citycode, city=$city, Feature_Type=$feature_type 
           where address=$address;"); 

     if (mysql_affected_rows()==0) { 
      $result = mysql_query("insert into r10_database (address, 
                  drops, 
                  city, 
                  citycode, 
                  Feature_Type) 
                values ($address, 
                  $drops, 
                  $city, 
                  $citycode, 
                  $Feature_Type);"); 
     } 
     $i++; 
    } 
} else { 
    echo mysql_error(); 
} 
mysql_close(); 
?> 
+2

歡迎使用stackoverflow,強制通知[**不要在新代碼中使用'mysql_ *'函數**](http://bit.ly/phpmsql)。他們不再被維護[並且被正式棄用](http://j.mp/XqV7Lp)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。如果你選擇PDO,[這裏是一個很好的教程](http://j.mp/PoWehJ)。 –

+0

您的代碼的格式已關閉。哪一行是錯誤? – jokeeffe

+0

請使用'{}'工具標記代碼,而不是用於引用純文本的''「''工具。 – Barmar

回答

1

你錯過了周圍的值引號中UPDATEINSERT電話:

$result = mysql_query("update r10_database 
          set drops='$drops', citycode='$citycode', city='$city', Feature_Type='$feature_type' 
          where address='$address';"); 

    if (mysql_affected_rows()==0) { 
     $result = mysql_query("insert into r10_database (address, 
                 drops, 
                 city, 
                 citycode, 
                 Feature_Type) 
               values ('$address', 
                 '$drops', 
                 '$city', 
                 '$citycode', 
                 '$Feature_Type');") 

順便說一句,如果address在表中有一個獨特的鍵,就可以同時做兩個查詢,使用INSERT ... ON DUPLICATE KEY UPDATE

+0

啊是的。當然。 +1是當之無愧的。 –