sheen4him Posted August 9, 2007 Share Posted August 9, 2007 I am trying to use update to update data in the table, but if I don't have data in the field, I get an error. I have lots of optional fields, and would like an easy work around for this. Here is the code I have now, but I need to add this for roughly 20 forms and 20-50 fields each form. Any suggestions would be great! Thanks // first add required fields... $query = "UPDATE fcustomer SET TIN='$ssn', TIN1='$SSN1', TIN2='$SSN2', TIN3='$SSN3', FirstName='$FirstName', LastName='$LastName', Address='$Address', City='$City', State='$State', Zip1='$Zip1', Zip='$zip', Email='$Email' WHERE UserName = '$UserName' && Password = '$Password'"; $result = mysql_query($query); if (!$result) {echo "die"; die('Could not insert data in customer file.'.$mysql_error());} // add fields that may or may not exist $query = "UPDATE fcustomer SET Zip='$zip'"; // something has to go in if ($Zip2 !="") $query.=", Zip2='$Zip2'"; if ($Phone1 !="") $query.=", Phone1='$Phone1'"; if ($Phone2 !="") $query.=", Phone2='$Phone2'"; if ($Phone3 !="") $query.=", Phone3='$Phone3'"; if ($phone !="") $query.=", Phone='$phone'"; $query.=" WHERE UserName = '$UserName' && Password = '$Password'"; $result = mysql_query($query); if (!$result) {echo "die"; die('Could not update data in customer file.'.$mysql_error());} Link to comment https://forums.phpfreaks.com/topic/64008-update-query-with-blank-data/ Share on other sites More sharing options...
sheen4him Posted August 9, 2007 Author Share Posted August 9, 2007 Also, what if I need to change data from what was originally entered to a user cleared out field. Then the field is blank, but if I use update, I get an error. Any help please? Link to comment https://forums.phpfreaks.com/topic/64008-update-query-with-blank-data/#findComment-319067 Share on other sites More sharing options...
fenway Posted August 10, 2007 Share Posted August 10, 2007 You're probably missing a space somewhere... echo the final query and see, or post the mysql_error(). BTW, it's better to update via a UID than user/pass in plaintext all the time. As for 2nd question, I have no idea hwat you mean. Link to comment https://forums.phpfreaks.com/topic/64008-update-query-with-blank-data/#findComment-320464 Share on other sites More sharing options...
teng84 Posted August 10, 2007 Share Posted August 10, 2007 what finway is saying is that use a PK as the identifier of the update query Link to comment https://forums.phpfreaks.com/topic/64008-update-query-with-blank-data/#findComment-320753 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.