Jump to content

Archived

This topic is now archived and is closed to further replies.

mouli

database update form

Recommended Posts

Hi
I have a form that uploads images to a mysql database and it works great. If it detects a duplicate entry based on the unique key it then uses an UPDATE query to update the record. This works as well except that you must enter values in all the form fields or it obviously updates those field with null values.
I'd like to be able to only update fields for which the posted variable from the form is not null so that it leaves fields unchanged where the posted variable is null.
Many thanks

mouli

Share this post


Link to post
Share on other sites
Make PHP check whether the variables are null; if they are, don't do the query.

Share this post


Link to post
Share on other sites
[!--quoteo(post=385498:date=Jun 19 2006, 02:01 PM:name=poirot)--][div class=\'quotetop\']QUOTE(poirot @ Jun 19 2006, 02:01 PM) [snapback]385498[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Make PHP check whether the variables are null; if they are, don't do the query.
[/quote]
Thanks for that. What I cant figure out is how I use php to construct the query so it only updates fields for which the posted variable is not null. There are about 10 fields in the form, the user simply enters values in those fields that need updating, leaving the rest empty, so the query has to only update fields that are not null in the posted variables. Its the dynamic construction of the query that has got me stumped.

Many thanks for your help.

mouli

Share this post


Link to post
Share on other sites
there's the easy way and then there's the hard way:

the easy way: if your form variable names match your column names, you can run a loop to build your query like so:

simplified:
[code]
$sql = "update tablename set ";
foreach($_POST as $key => $val) {
   if (isset($val) && trim($val) !== '') {
      $sql.= $key . "='" . $val . "',";
   }
}
$sql = substr_replace($sql,"",-1); //get rid of the last comma
$sql.= " where id = '$id'"; //assuming you are updating based on id
mysql_query($sql);
[/code]

the hard way: doing the exact same thing except for with an if statement for each and every value, named individually.

Share this post


Link to post
Share on other sites
[!--quoteo(post=385514:date=Jun 19 2006, 03:55 PM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ Jun 19 2006, 03:55 PM) [snapback]385514[/snapback][/div][div class=\'quotemain\'][!--quotec--]
there's the easy way and then there's the hard way:

the easy way: if your form variable names match your column names, you can run a loop to build your query like so:

simplified:
[code]
$sql = "update tablename set ";
foreach($_POST as $key => $val) {
   if (isset($val) && trim($val) !== '') {
      $sql.= $key . "='" . $val . "',";
   }
}
$sql = substr_replace($sql,"",-1); //get rid of the last comma
$sql.= " where id = '$id'"; //assuming you are updating based on id
mysql_query($sql);
[/code]

the hard way: doing the exact same thing except for with an if statement for each and every value, named individually.
[/quote]

Mmmm, this looks good. Variables match columns so I'll give it a go and get back to you.
Many many thanks :)

Share this post


Link to post
Share on other sites
Yup it worked a treat.
I only had to remove the $submit variable and it worked beautifully. Many thanks

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.