Problem with Update

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zed420
Forum Commoner
Posts: 32
Joined: Wed Jun 04, 2008 11:24 am

Problem with Update

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Problem with Update

Post 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.
bmoyles0117
Forum Newbie
Posts: 22
Joined: Sun Nov 02, 2008 1:46 am

Re: Problem with Update

Post 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.
zed420
Forum Commoner
Posts: 32
Joined: Wed Jun 04, 2008 11:24 am

Re: Problem with Update

Post 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
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Problem with Update

Post by aceconcepts »

You could try using mysql_error() to determine where the error lies.
theboyholty
Forum Newbie
Posts: 5
Joined: Mon Nov 03, 2008 5:35 am

Re: Problem with Update

Post 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.
zed420
Forum Commoner
Posts: 32
Joined: Wed Jun 04, 2008 11:24 am

Re: Problem with Update

Post 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
Post Reply