mouli Posted June 18, 2006 Share Posted June 18, 2006 HiI 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 thanksmouli Quote Link to comment Share on other sites More sharing options...
poirot Posted June 19, 2006 Share Posted June 19, 2006 Make PHP check whether the variables are null; if they are, don't do the query. Quote Link to comment Share on other sites More sharing options...
mouli Posted June 19, 2006 Author Share Posted June 19, 2006 [!--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 Quote Link to comment Share on other sites More sharing options...
.josh Posted June 19, 2006 Share Posted June 19, 2006 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 idmysql_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 Link to comment Share on other sites More sharing options...
mouli Posted June 19, 2006 Author Share Posted June 19, 2006 [!--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 idmysql_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 :) Quote Link to comment Share on other sites More sharing options...
mouli Posted June 28, 2006 Author Share Posted June 28, 2006 Yup it worked a treat. I only had to remove the $submit variable and it worked beautifully. Many thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.