Page 1 of 1

Problem with Update

Posted: Sun Nov 02, 2008 8:16 pm
by zed420
Hi ALL
Can anyone help me on this query please, all I'm trying to do is Update three tables in a same query.

Code: Select all

if($_POST['submit']) {
        foreach($_POST as $job_id) { 
        mysql_query("UPDATE job_tb,blockbook,blockbook2 SET 
        job_tb.cust_address = 'Cancel' 
        blockbook.cust_address = 'Cancel'
        blockbook2.cust_address1 = 'Cancel'
        WHERE job_tb.job_id = '$job_id'
        AND blockbook.job_id = '$job_id' 
        AND blockbook2.job_id = '$job_id'"); 
        if (mysql_affected_rows() > 0) { // execute query 
        print "<font color=red size=2> No. = $job_id has been Updated</font><p>"; 
        } 
    }
}
 
Thanks Zed

Re: Problem with Update

Posted: Sun Nov 02, 2008 8:37 pm
by requinix
1. You didn't say what the problem was.
2. Your query is missing some commas.
3. It's also insecure because it's using stuff from $_POST without validating/sanitizing it.

Re: Problem with Update

Posted: Mon Nov 03, 2008 12:19 am
by bmoyles0117
Please do keep in mind that $_POST right off the bat already contains extra field values than what you are searching for. You should refine your output and as stated sanitize the data. After every SET insertion you need a comma.

Re: Problem with Update

Posted: Mon Nov 03, 2008 3:44 am
by zed420
Sorry I should've made it clear, it basiclly is not updating I have added the commas now it's not giving me any errors message but at the same time its NOT Updating.

Thank you for your reply
Zed

Re: Problem with Update

Posted: Mon Nov 03, 2008 8:27 am
by aceconcepts
You could try using mysql_error() to determine where the error lies.

Re: Problem with Update

Posted: Mon Nov 03, 2008 10:22 am
by theboyholty
I prefer to checkeach step of the way by inserting the following line after if($_POST['submit']) {:

echo 'Check Data: ' . $job_id;

That way, you are ensuring that your form is actually passing some information. If that shows the data you were expecting then its a problem with your query.

By the way, if it was me, id split the update query into three separate queries, one for each table. Its less messy.

Re: Problem with Update

Posted: Mon Nov 03, 2008 12:22 pm
by zed420
Its funny you've said that about spliting the queries theboyholty, that's exactly what I have just done and it seems to be ok now all tables are updating fine. I'm fairly sure I could've done this in one query but its just wasting so time. Thanks for repying

Zed