0

I'm trying to update two tables at the same time. The action completes successfully.However, it updates all records in the the table instead of the ones specified. I have tried the suggestions here, but to no avail. Any ideas what the issue might be? My code looks like below.

$sql = "UPDATE $receiverTable, $currUserTable 
SET $currUserTable.originator = '$curr_username', 
$receiverTable.originator = '$curr_username', 
$currUserTable.status = '$currValue', 
$receiverTable.status = '$currValue' 
WHERE ($receiverTable.username = '$curr_username') 
OR ($currUserTable.username = '$curr_username')";
7
  • 1
    Tag appropriate database name. Commented Aug 12, 2019 at 9:53
  • @mkRabbani, I am not sure what you mean. Thanks Commented Aug 12, 2019 at 9:57
  • 1
    Which database engine are you using Commented Aug 12, 2019 at 9:58
  • I mean MSSQL or MySQL or Oracle .... Commented Aug 12, 2019 at 10:00
  • Thats a weird requirement, why not wrap two updates inside a single transaction ? Commented Aug 12, 2019 at 10:01

2 Answers 2

2

You have no condition on your JOIN (you really shouldn't be writing JOINs with commas any more) and so every row gets JOINed to every other row, which means that they all have a JOIN to a row in which one of the WHERE conditions is true, hence they all get updated. Rewrite your JOIN with the appropriate condition and the problem will go away. Something like:

UPDATE $receiverTable
JOIN $currUserTable ON $currUserTable.somecolumn = $receiverTable.somecolumn
SET $currUserTable.originator = '$curr_username', 
$receiverTable.originator = '$curr_username', 
$currUserTable.status = '$currValue', 
$receiverTable.status = '$currValue' 
WHERE ($receiverTable.username = '$curr_username') 
OR ($currUserTable.username = '$curr_username')

If there is no way that the tables can be JOINed, you will need to write the UPDATE as two separate queries.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, that did the trick. I was trying to do it without the JOIN!
2

Instead of doing this with a single update use a transaction to wrap two update commands, something like this.

BEGIN TRANSACTIONS;

UPDATE TABLE1
SET Col1 = 'SomeValue'
WHERE Cond1 = 'SomeCond';

UPDATE TABLE2
SET Col2 = 'SomeValue'
WHERE Cond2 = 'SomeCond';

COMMIT;

UPDATE

Following this I believe with phpi it would look like:

mysqli_autocommit($dbConnection, false);

$query1 = " UPDATE $receiverTable set originator = '$curr_username', 
status = '$currValue' WHERE username = '$curr_username' "
$query2 = " UPDATE $currUserTable set originator = '$curr_username', 
status = '$currValue' WHERE username = '$curr_username' "

mysqli_query($dbConnection, $query1); 
mysqli_query($dbConnection, $query2);

mysqli_commit($dbConnection);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.