Jump to content

database update form


mouli

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
Link to comment
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
Link to comment
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.
Link to comment
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 :)
Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.