Jump to content

PHP question [solved]


Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
 Share

×
×
  • Create New...