user1114409
user1114409

Reputation: 555

How to limit UPDATE when using PHP script

I have been trying to get right solution / answer for my issue, still not able to get around, kindly help !

This is the same MYSQL UPDATE, LIMIT issue I was trying to resolve. I thought maybe with PHP and MYSQL script it should be able work, not sure, pl check the below code. It is updating Rate field in Ratemaster table with the last value of Rates table. But there are 3 rows in both the tables where 3 different rates in Rates table.

I have 2 tables: Ratemaster and rates, in which a CUSTOMER can have 1 PRODUCT with different RATES. Because of this, there is a duplication of customer and product fields, only the rate field changes. Now Table Ratemaster has all the fields : id, cust_code, Product, Rate, user whereas Table Rates has only: id, cust_code, Rate, user. - user field is for checking session_user.

My php and mysql code below:

$con = mysql_connect("localhost","db","pwd");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("db", $con);

$user = $_SESSION['user'];

$query = "SELECT * FROM Rates";
$result = mysql_query($query)or die(mysql_error()); 

while ($line = mysql_fetch_assoc($result)) {
mysql_query("UPDATE Ratemaster, Rates SET Ratemaster.Rate = '".$line['Rate']."'  WHERE Ratemaster.user = '".$line['user']."'")or die(mysql_error());}

Upvotes: 0

Views: 614

Answers (1)

Siva Karuppiah
Siva Karuppiah

Reputation: 1013

I have add one more field in Ratemaster called 'rid' . It's should match with Rates table 'id' field. And one more thing, id always should be unique.

Ratemaster has : 3 records

(id:1, cust_code:1231, Product:Clay, Rate:0, user:user1,rid:1) 
(id:2, cust_code:1231, Product:Clay, Rate:0, user:user1,rid:2) 
(id:3, cust_code:1231, Product:Clay, Rate:0, user:user1,rid:3) 

Rates has 3recs :

(id:1, cust_code:1231, Rate:299, user:user1) 
(id:2, cust_code:1231, Rate:355, user:user1) 
(id:3, cust_code:1231, Rate:466, user:user1)

Code :

 $con = mysql_connect("localhost","db","pwd");
 if (!$con)
    {
       die('Could not connect: ' . mysql_error());
    }
mysql_select_db("db", $con);

$user = $_SESSION['user'];

mysql_query("UPDATE Ratemaster, Rates SET Ratemaster.Rate = Rates.Rate  WHERE Ratemaster.user = '".$line['user']."' AND Ratemaster.rid=Rates.id ")or die(mysql_error());

Upvotes: 1

Related Questions