看下面的代码,它真的很有用。
如果记录不存在,它会插入affiliate
表。
如果它已插入affiliate
表中,则它将在phone
表中添加新记录。但是,它需要更新第3个查询中phone_id
表(新近期记录)中的affiliate
字段 - 有没有办法减少这种情况?
$SQLInsert = "INSERT INTO affiliate (affiliate_id, affiliate_phone_id, stock) ";
$SQLInsert .= "VALUES ('1', '$affiliatePhoneID', '$stock') ";
$SQLInsert .= "ON DUPLICATE KEY UPDATE stock = '$stock'";
mysql_query($SQLInsert);
if (mysql_insert_id()) {
$SQLInsert = "INSERT INTO phone (name) VALUE('$phoneName')";
$q = mysql_query($SQLInsert);
// If a record has been inserted into affiliate table and then phone table
// then it need to update phone_id field in affiliate table.
$phone_id = mysql_insert_id();
$SQLUpdate = "UPDATE affiliate set phone_id = $phone_id WHERE affiliate_id = 1 AND affiliate_phone_id = '$affiliatePhoneID'";
mysql_query($SQLUpdate) or die(mysql_error());
}
答案 0 :(得分:0)
也许你可以更改数据库sheme,以便电话表将forainkey保存到affilaite表。所以你不需要两次更新附表。
如果这不是您想要的,您可以尝试先更新电话表, 然后是附表。