samedi 28 février 2015

Update SQL data via PHP/HTML form

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 to 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?


Aucun commentaire:

Enregistrer un commentaire