user1709294
user1709294

Reputation: 1805

Using Queries with Conditional Testing in PHP

I'm trying to make a script that updates a table in mySQL using php.

I have this so far

$q3 = mysql_query("Select 1 from USER_REP where CID = '$cid' AND uid = '$uid'") OR die;    
$num_q3 = mysql_num_rows($q3);
if($num_q3 == 0){
    mysql_query("UPDATE IDEA set ErrorCount = ErrorCount + 1 WHERE RID = '$rid' AND CID = '$cid'") OR die ("error"); 
}

I know that variables are being correctly associated since I can perform queries within other parts of the script. However I wanted to update a tuple in IDEA if $q3 doesn't return one tuple. So I think I'm having some issues with this conditional testing. Can someone help me with this?

Thanks.

Upvotes: 0

Views: 67

Answers (1)

John Woo
John Woo

Reputation: 263803

you can join both tables during update,

UPDATE  IDEA a
        LEFT JOIN USER_REP  b
            ON  a.cid = b.cid AND
                b.uid = 'uidHERE' AND
                b.cid = 'cidHERE'
SET     ErrorCount = ErrorCount + 1
WHERE   b.cid IS NULL AND
        a.rid = 'ridVALUE'

Upvotes: 1

Related Questions