0

I've got the following form which is a select with options, where the user selects an option such as email, address etc.. enter new data to update existing field which is done by selecting the field by matching it to a name.

    <form action="sUpdateResponse.php" method="post">

    <select id="sRetrieveUpdate" name="sRetrieveUpdate" onchange=";">
       <option value='' disabled selected style='display:none;'>Select Field To Update</option>
       <option value="address">Address</option>
       <option value="email">Email</option>
    </select><br>

    <div id="box-1"><input type="text" id="address" name="sUpdateAddress" placeholder="Enter New Address..." 
        onClick="$(this).removeClass('placeholderclass')" class="dateclass placeholderclass"></input></div>

    <div id="box-2"><input type="text" id="email" name="sUpdateEmail" placeholder="Enter New Email..." 
        onClick="$(this).removeClass('placeholderclass')" class="dateclass placeholderclass"></input></div>

            For the staff entry
            where the name matches...<br>
    <input type="text" placeholder="Enter Forename..." name="sUpdateFN">
    <input type="text" placeholder="Enter Surname..." name="sUpdateSN">
        <input type="submit" value="Update Record" name="sRetrieveUpdate">
        </form>  

The response then checks to see if address or email was selected and updates where the firstname and surname match the ones entered.

    if(isset($_POST['sUpdateEmail']) && isset($_POST['sUpdateFN']) && isset($_POST['sUpdateSN']))
            $query = "UPDATE staffData SET sEmail = '".$_POST['sUpdateEmail']."' WHERE sFN = '".$_POST['sUpdateFN']."' AND sSN = '".$_POST['sUpdateSN']."'";
            $result = mysql_query($query);


  if(isset($_POST['sUpdateAddress']) && isset($_POST['sUpdateFN']) && isset($_POST['sUpdateSN']))
        $query = "UPDATE staffData SET sAddress = '".$_POST['sUpdateAddress']."' WHERE sFN = '".$_POST['sUpdateFN']."' AND sSN = '".$_POST['sUpdateSN']."'";
        $result = mysql_query($query);

The only problem here is, when updating one field for some reason the other field goes empty, any idea where its going wrong?

2
  • 1
    That's not the only problem, you should be using mysqli and prepared statements. You have lots of security problems here. Commented Feb 28, 2015 at 16:43
  • @ArianFaurtosh this won't ever be used so security isnt an issue, just trying to get the concept working Commented Feb 28, 2015 at 16:43

1 Answer 1

1

You need to put brackets around your code blocks, or the second line will always be executed.

The problem here is that even if updateAddress is empty, the variable will be set still. So isset is a bad way to test if it is empty.

So you should add !empty() or != '' (to use empty() you have to have a PHP version higher than 5.4)

Here is an example:

if(isset($_POST['sUpdateEmail']) && $_POST['sUpdateEmail'] != '' && isset($_POST['sUpdateFN']) && isset($_POST['sUpdateSN'])) {
  $query = "UPDATE staffData SET sEmail = '".$_POST['sUpdateEmail']."' WHERE sFN = '".$_POST['sUpdateFN']."' AND sSN = '".$_POST['sUpdateSN']."'";
  $result = mysql_query($query);
}


if(isset($_POST['sUpdateAddress']) && $_POST['sUpdateAddress'] != '' && isset($_POST['sUpdateFN']) && isset($_POST['sUpdateSN'])){
  $query = "UPDATE staffData SET sAddress = '".$_POST['sUpdateAddress']."' WHERE sFN = '".$_POST['sUpdateFN']."' AND sSN = '".$_POST['sUpdateSN']."'";
  $result = mysql_query($query);
}
Sign up to request clarification or add additional context in comments.

9 Comments

This doesn't resolve the issue.. When updating email, address will delete itself and vice versa still.
yea i just figured that out aha, what would you suggest?
either do an if with !empty($_POST['sUpdateAddress']) if you have a new enough php version or you can do $_POST['sUpdateAddress']) != ''
What if its email that doesnt need to be updated though?
@ili look at my example I posted in my answer
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.