简体   繁体   English

php / mysql更新表并在未找到匹配项的地方插入新记录

[英]php/mysql update a table and insert new record where no match found

I want to update a table based on the values from another table. 我想根据另一个表中的值更新一个表。 If no record exists, then insert as new record. 如果不存在任何记录,则作为新记录插入。 This code updates correctly but does not insert where records do no exist. 此代码正确更新,但不会在不存在记录的地方插入。

$results_google = mysql_query("upadate google set                
    Description = '".mysql_real_escape_string(trim(addslashes(strip_tags($row_first['product_description']))))."',
    Manufacturer = '".$row_first['product_vendor']."',
    Price = '".$row_first['product_price']."',                
    Title ='".$row_first['product_title']."'
    WHERE product_id = '".mysql_real_escape_string($row_first['product_id'])."';");
if(!$results_google) {      
    $google = "insert into `google`(`product_id`,`id`,`Description`,`Link`,`Manufacturer`,`Title`,
        `Price`,`Shipping_Weight`,`Image_Link`,`Brand`,`inv_id`)
        VALUES (
        '".mysql_real_escape_string($row_first['product_id'])."',
        '".mysql_real_escape_string($row_first['product_id'])."',
        '".mysql_real_escape_string(trim(strip_tags(addslashes($row_first['product_description']))))."',
        '".mysql_real_escape_string($row_first['product_link'])."',
        '".mysql_real_escape_string($row_first['product_vendor'])."',
        '".mysql_real_escape_string($row_first['product_title'])."',
        '".mysql_real_escape_string($row_first['product_price'])."',                             
        '".mysql_real_escape_string($row_first['weight'])."',
        '".mysql_real_escape_string($row_first['image_link'])."',
        '".mysql_real_escape_string($row_first['product_vendor'])."',
        '$inventory_id')";
    mysql_query ($google) or die("google".mysql_error());
}

If you are asking how to insert a row or update it if it already exists, you need to look into ON DUPLICATE KEY UPDATE . 如果您询问如何插入行或对其进行更新(如果已经存在),则需要查看ON DUPLICATE KEY UPDATE ON DUPLICATE KEY UPDATE will only check UNIQUE indexes or primary keys, so make sure the fields that will differ to cause an additional row are marked as unique (or you can do a multi-column unique index). ON DUPLICATE KEY UPDATE将仅检查UNIQUE索引或主键,因此请确保将引起额外行的不同字段标记为唯一(或者您可以执行多列唯一索引)。

Read the documentation for INSERT ... ON DUPLICATE KEY UPDATE . 阅读INSERT ... ON DUPLICATE KEY UPDATE文档

Although @Konerak's comment is really the right way to go, in combination with prepared statements and knowing your data (casting to ints for ints, using string functions for strings, etc.), I will answer the question as to why your code is not working as expected. 虽然@ Konerak的评论是真的走正道,与预处理语句组合和知道你的数据(强制转换为ints的整数,使用字符串函数的字符串,等等),我会回答这个问题,为什么你的代码是不是按预期工作。

You are using an update query with a WHERE statement. 您正在使用带有WHERE语句的更新查询。 If no matching row is found, no row is updated but the query does not return false because mysql_query only returns false on error and not finding a row to update is not an error. 如果找不到匹配的行,则不会更新任何行,但查询不会返回false,因为mysql_query仅在错误时返回false,而未找到要更新的行则不是错误。 So as long as your query is valid, your if statement will always be false. 因此,只要您的查询有效, if语句将始终为false。

Edit: As mentioned in the comment below, you can use mysql_affected_rows() to check if no row was updated: 编辑:如下面的注释中所述,您可以使用mysql_affected_rows()来检查是否没有行被更新:

if (mysql_affected_rows() == 0)
{
  // insert a record
}

but I would really recommend going the ON DUPLICATE KEY UPDATE and prepared statements way. 但我真的建议您使用ON DUPLICATE KEY UPDATEON DUPLICATE KEY UPDATE Statement方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM