Main
Index
Search
Posts
Who's
Online
Log
In

Home: Products: CMS Builder:
Update a record based on matching field value in form

 

 


gkornbluth
Veteran

Dec 14, 2011, 12:05 PM

Post #1 of 5 (279 views)
Shortcut
Update a record based on matching field value in form Can't Post

Hi All,

Well, obviously I’m going about this wrong, because it doesn’t work... (And Ill probably feel pretty silly once I find out the solution)

I’m trying to update a record in the table customer_uploads only if the value of a variable ($activation_code) matches the value in the existing record’s activation_code field. I thought that the code below would do that, but instead it updates all the records.

Here’s what I have. (There are other variables and fields but I left them out for clarity.)

Hope someone is willing to help.

Thanks,

Jerry Kornbluth


Code
$activation_code=$_POST['activation_code']; 


// load records
list($customer_uploadsRecords, $customer_uploadsMetaData) = getRecords(array(
'tableName' => 'customer_uploads',
'where' => 'activation_code = "$activation_code"',
));

mysql_query("UPDATE `{$TABLE_PREFIX}customer_uploads` SET

email = '".$email."',
first_name = '".$first_name."',
last_name = '".$last_name1."',
street_address = '".$street_address."',

or die("MySQL Error updating Record:<br/>\n". htmlspecialchars(mysql_error()) . "\n");
$userNum = mysql_insert_id();
@saveUploadFromFilepath('customer_uploads', 'uploads', $userNum, '123456789' , $path_of_uploaded_file);

$errors='Thank You<br /><br />Your Order Has Been Successfully Placed.';
}

?>


Jason
Staff / Moderator


Dec 15, 2011, 10:37 AM

Post #2 of 5 (274 views)
Shortcut
Re: [gkornbluth] Update a record based on matching field value in form [In reply to] Can't Post

Hi Jerry,

In order to not have the query update all records, you need to add a WHERE clause to your update query.

For example, if you wanted to update ALL records with that given activation code, you can try this:


Code
 mysql_query("UPDATE `{$TABLE_PREFIX}customer_uploads` SET  
email = '".$email."',
first_name = '".$first_name."',
last_name = '".$last_name1."',
street_address = '".$street_address."'
WHERE activation_code = '".mysql_escape($activation_code)."'")

or die("MySQL Error updating Record:<br/>\n". htmlspecialchars(mysql_error()) . "\n");


Hope this helps
---------------------------------------------------
Jason Sauchuk - Programmer 
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/ 

(This post was edited by Jason on Dec 15, 2011, 10:38 AM)


gkornbluth
Veteran

Dec 15, 2011, 3:47 PM

Post #3 of 5 (270 views)
Shortcut
Re: [Jason] Update a record based on matching field value in form [In reply to] Can't Post

Hi Jason,

I thought I would have to put in a where clause but I kept getting syntax errors every time I tried to put one in.
I'm still getting a syntax error:

Code
MySQL Error updating Record: 
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE activation_code = 'T87345'' at line 19



Here's the actual code that's generating the error (with or without the load records call):

//load records
list($customer_uploadsRecords, $customer_uploadsMetaData) = getRecords(array(
'tableName' => 'customer_uploads',

));

mysql_query("UPDATE `{$TABLE_PREFIX}customer_uploads` SET

email = '".$email."',
first_name = '".$first_name."',
last_name = '".$last_name1."',
street_address = '".$street_address."',
city = '".$city."',
state = '".$state."',
zip = '".$zip."',

sales_id = '".$sales_id."',
createdDate = NOW(),
updatedDate = NOW(),
createdByUserNum = '0',
updatedByUserNum = '0',
WHERE activation_code = '".mysql_escape($activation_code)."'")
or die("MySQL Error updating Record:<br/>\n". htmlspecialchars(mysql_error()) . "\n");
$userNum = mysql_insert_id();
@saveUploadFromFilepath('customer_uploads', 'uploads', $userNum, '123456789' , $path_of_uploaded_file);
$errors='Thank You<br /><br />Your Portrait Order Has Been Successfully Placed.';
}

?>


ANd I've attached the complete file
The first CMS Builder reference book is now available on-line!
http://www.thecmsbcookbook.com
Attachments: ordernow3.php (11.7 KB)


Jason
Staff / Moderator


Dec 15, 2011, 3:51 PM

Post #4 of 5 (269 views)
Shortcut
Re: [gkornbluth] Update a record based on matching field value in form [In reply to] Can't Post

Hi Jerry,

Try removing the last comma in your field list, the one on this line:


Code
updatedByUserNum = '0',


That should take care of it. I don't know how many times I got caught with that one myself.
---------------------------------------------------
Jason Sauchuk - Programmer 
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/ 


gkornbluth
Veteran

Dec 15, 2011, 4:03 PM

Post #5 of 5 (268 views)
Shortcut
Re: [Jason] Update a record based on matching field value in form [In reply to] Can't Post

So easy for the Guru

So difficult for the rest of us mere mortals.

Thank you Guru!

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!
http://www.thecmsbcookbook.com