0

我试图扔掉我的表 nonbulkmdu 并查看 r10database 以查找是否有重复项,如果有,它将更新 4 个字段,如果没有,它将插入一个新行。我不断收到错误

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();
?>
4

1 回答 1

1

您在UPDATEandINSERT调用中缺少围绕值的引号:

    $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.

于 2013-11-01T23:59:35.710 回答