I_NEED_HELP Posted April 17, 2005 Report Share Posted April 17, 2005 Im trying to find the answer to this, i should know the answer but i cant seem to find it anywhere but that may be me having a dumb moment. Here is the problem I have created a script to modify fields in a database if form fields have been entered. Eg i have four fields username, password, name, and and id number called hemisnumber. If a field has been filled in i want it to update the database but if nothing has been put in i want it to skip changing it so i dont end up changingfields to blank space. This is the section of script that changes the database: CODE if ($_POST[hemisNumberNew]) { $update = "UPDATE users SET hemisNumber = '$_POST[hemisNumberNew]' WHERE hemisNumber = '$_POST[hemisNumberOld]'"; $hemisResult = @mysql_query($update,$connection) or die (mysql_error()); } if ($_POST[password]) { $update = "UPDATE users SET password = '$_POST[password]' WHERE hemisNumber = '$_POST[hemisNumberOld]'"; $passwordResult = @mysql_query($update,$connection) or die (mysql_error()); } if ($_POST[name]) { $update = "UPDATE users SET name = '$_POST[name]' WHERE hemisNumber = '$_POST[hemisNumberOld]'"; $nameResult = @mysql_query($update,$connection) or die (mysql_error()); } if ($_POST[courseCode]) { $update = "UPDATE users SET courseCode = '$_POST[courseCode]' WHERE hemisNumber = '$_POST[hemisNumberOld]'"; $courseCodeResult = @mysql_query($update,$connection) or die (mysql_error()); } However the problem with this is that if i fill in more than one field it will only change the first field and not any of the others. So how do i get it to run each statement if there is a value in that field. If you want to see what i mean the page is at http://www.housedarus.co.uk/ECE/admin/users.php login using userName admin password = password. Quote Link to comment Share on other sites More sharing options...
paul Posted April 17, 2005 Report Share Posted April 17, 2005 $update = "UPDATE users set"; if($_POST[name]) { $update .= "name = '$_POST[name]' WHERE hemisNumber = '$_POST[hemisNumberOld]'"; } if($_POST) { ..... build your SQL statement dynamically like the above example Quote Link to comment Share on other sites More sharing options...
SoulSe Posted April 18, 2005 Report Share Posted April 18, 2005 Paul the PHP-demon strikes again! :P Quote Link to comment Share on other sites More sharing options...
scoopy Posted April 18, 2005 Report Share Posted April 18, 2005 Quote Link to comment Share on other sites More sharing options...
I_NEED_HELP Posted April 18, 2005 Author Report Share Posted April 18, 2005 thanks for your help i found the problem the other day but forgot to post it. Reward to paul, the only guy on any forum i posted on who obviously realised my flawed logic in this code. I solved it by performing the hemis update last although i have to say you suggestion is a better solution. Ill try to implement it unfortunatly im fast running out of time on this uni project. Quote Link to comment Share on other sites More sharing options...
paul Posted April 18, 2005 Report Share Posted April 18, 2005 Quote Link to comment Share on other sites More sharing options...
tyme Posted April 20, 2005 Report Share Posted April 20, 2005 should this be moved to coding? Quote Link to comment Share on other sites More sharing options...
spinynorman Posted April 20, 2005 Report Share Posted April 20, 2005 should this be moved to coding? Moved. Are you looking for a job, Mark? :P Quote Link to comment Share on other sites More sharing options...
tyme Posted April 20, 2005 Report Share Posted April 20, 2005 no, just trying to keep you guys in line up there ;) 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.
Note: Your post will require moderator approval before it will be visible.