0

好的,所以我需要在页面加载时更新两个表。这是代码:

<?php include'connects.php';
 //Test if it is a shared client
if (!empty($_SERVER['HTTP_CLIENT_IP'])){
 $ip=$_SERVER['HTTP_CLIENT_IP'];
//Is it a proxy address
}elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$ip=$_SERVER['REMOTE_ADDR'];
}

$ip = ip2long($ip);

mysqli_query($con,"INSERT INTO visits (ip_adress) VALUES ('$ip') ON DUPLICATE KEY                                                                                 
UPDATE         visit = visit + 1");

mysqli_query($con,"UPDATE ip_visits SET total_visits = total_visits + 1");

mysqli_close($con);
?>

现在我有第二个表需要执行与此顶层表相同的功能:

 mysqli_query($con,"INSERT INTO indVisits (ip_adress) VALUES ('$ip') ON DUPLICATE KEY  
 UPDATE visits = visits + 1");

 mysqli_query($con,"UPDATE totalVisits SET visit = visit + 1");
4

2 回答 2

1

您可以使用 mysqli 多查询:http: //php.net/manual/en/mysqli.multi-query.php

如果您需要在两个不同的表中存储相同的元组,您还应该考虑重新考虑数据库的设计。

于 2013-08-30T20:09:18.360 回答
0

你可以做这样的事情

mysqli_query($con,"INSERT INTO visits (ip_adress) VALUES ('$ip') ON DUPLICATE KEY                                                                                
UPDATE         visit = visit + 1; 
UPDATE ip_visits SET total_visits = total_visits + 1");

查询字符串中的分号将结束一个查询并表示另一个查询的开始。您不必拨打 2 次单独的电话,而只需拨打 1 次。

于 2013-08-30T20:06:39.987 回答