Rodrigo Lessa
Rodrigo Lessa

Reputation: 59

updating two different tables in one query

Okay so i need to update both tables when the page loads.here is the code:

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

Now i have a second table that needs to do the same function as this top table:

 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");

Upvotes: 0

Views: 71

Answers (2)

Anders
Anders

Reputation: 499

You can use mysqli multiquery: http://php.net/manual/en/mysqli.multi-query.php

You should also consider rethinking the design of your database if you need to store identical tuples in two different tables.

Upvotes: 1

Oluwakayode Dagbo
Oluwakayode Dagbo

Reputation: 191

you can do something like this

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");

The semi-colon in the query string will end one query and signal the start for another. You would not have to make 2 separate calls, but just one.

Upvotes: 0

Related Questions