I'm kind of new to PDO, and I'm struggling about checking duplicate(s) in the database while I'm on editing form.
The current scenario is:
- The input field is a phone number
- Upon insert it is being checked if it already exist in the db
- Upon edit it is being checked again if the edit got a match in the db
- I can't just forbid the editing in the phone number once it is
added because what if in the future it needs to be change - After the user hit the edit button all data is being fetch and being echoed to its respective fields therefore the user will know that field has that kind of information already or that field is still empty
The problem occurs when the user is just editing other fields like "an update for a change in PIN", the phone number field got checked again and obviously it will return an error saying "it already exist".
How can i ignore that field entry if it's not changed?
here is the part of code for edit.php
if(isset($_POST['Edith_SIM'])) {
$id = $_POST['id'];
$s_num = $_POST['sim_num'];
$s_esn = $_POST['sim_esn'];
$s_net = $_POST['sim_net'];
$s_pn1 = $_POST['simpn1'];
$s_pn2 = $_POST['simpn2'];
$s_pk1 = $_POST['simpk1'];
$s_pk2 = $_POST['simpk2'];
$s_fea = $_POST['sim_feat'];
$s_typ = $_POST['sim_type'];
$s_acq = $_POST['d_acq'];
$s_afo = $_POST['acq_for'];
$s_ufr = $_POST['d_uf'];
$s_uto = $_POST['d_ut'];
$s_dev = $_POST['use_dev'];
$s_sta = $_POST['sim_stat'];
$s_not = $_POST['sim_notes'];
$chekDuplicate = $dbConn->prepare('SELECT * FROM tst_sims WHERE SIM_num= :sim_num');
$chekDuplicate->execute(array(':sim_num' => $s_num));
// checking empty fields
if(empty($s_num)) {
echo "<font color='red'>Must have SIM number my dudes</font><br/>";
echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";
} elseif($chekDuplicate->rowCount() > 0) {
echo "<font color='red'>Its existing my dudes</font><br/>";
echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";
} else {
$sim_passed_e = "UPDATE tst_sims SET
SIM_num = :sim_num,
SIM_network =:sim_net,
SIM_PIN1 =:simpn1,
SIM_PIN2 =:simpn2,
SIM_PUK1 =:simpk1,
SIM_PUK2 =:simpk2,
SIM_features =:sim_feat,
SIM_type =:sim_type,
Acquired =:d_acq,
Acquired_for =:acq_for,
Used_from =:d_uf,
Used_to =:d_ut,
Used_devices =:use_dev,
Status =:sim_stat,
Notes =:sim_notes
WHERE Ent_id =:id";
$que_edit = $dbConn->prepare($sim_passed_e);
$que_edit->bindparam(':id', $id);
$que_edit->bindparam(':sim_num', $s_num);
$que_edit->bindparam(':sim_esn', $s_esn);
$que_edit->bindparam(':sim_net', $s_net);
$que_edit->bindparam(':simpn1', $s_pn1);
$que_edit->bindparam(':simpn2', $s_pn2);
$que_edit->bindparam(':simpk1', $s_pk1);
$que_edit->bindparam(':simpk2', $s_pk2);
$que_edit->bindparam(':sim_feat', $s_fea);
$que_edit->bindparam(':sim_type', $s_typ);
$que_edit->bindparam(':d_acq', $s_acq);
$que_edit->bindparam(':acq_for', $s_afo);
$que_edit->bindparam(':d_uf', $s_ufr);
$que_edit->bindparam(':d_ut', $s_uto);
$que_edit->bindparam(':use_dev', $s_dev);
$que_edit->bindparam(':sim_stat', $s_sta);
$que_edit->bindparam(':sim_notes', $s_not);
$que_edit->execute();
header("location: index.php");
echo "<font color='green'>Data added successfully my dudes.";
echo "<br/><a href='index.php'>View Result</a>";
}
}
I hope this is not that confusing to all of you, I don't know what kind of approach I should make to this.
Thank you in advance
Aucun commentaire:
Enregistrer un commentaire