If the value already exists in the array, an error is thrown in PHP

I have code in which it checks if a value exists in an array. Basically, what the program does is that it first stores all the values ​​in an array. Then it will be checked using the count(array_keys) function. There are three entrances. If a value occurs twice or thrice in these three inputs, it will throw an error. Now my problem is that if INPUT A AND INPUT B is the same, but INPUT C is different, it will add the database anyway BUT IF ENTRANCE A and C IS ONLY BUT INPUT B is different from what it doesn't (which correctly).

Here is my PHP code:

 <?php include 'config.php'; if(isset($_POST['update_actual_lupong'])){ $id = isset($_GET['id'])? $_GET['id'] : ""; $id_hearing = $_POST['hearing_lup']; $lupong = $_POST['act_lupong']; $actual = array(); foreach($lupong as $aaa) { $actual[] = $aaa; } if ((count(array_keys($actual, $aaa)) > 1)) { echo '<br><br>this array contains DUPLICATE <br><br>'; } else { foreach ($lupong as $lup) { $updateMemo = mysqli_query($conn, "INSERT INTO actuallupong(Hearing_idHearing, bar_pos2) VALUES ('$id_hearing', (SELECT idtable_position FROM table_position WHERE Person_idPerson = '$lup'));"); } echo "ADDED ggg"; } //header ('location: view_case_profile.php?id='.$id); mysqli_close($conn); } ?> 

HTML code (in modal):

 <div class="modal fade bs-example-modal-lg" id="modal_lupong" data-backdrop="static" data-keyboard="false" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" > <div class="modal-dialog modal-lg"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal"> <span aria-hidden="true">&times;</span> <span class="sr-only">Close</span> </button> <h4 class="modal-title" id="myModalLabel">Update Lupong</h4> </div> <form id="update_actual_lupong" class="form-horizontal form-label-left calender" name = "update_actual_lupong" enctype="multipart/form-data" method="post" role="form" novalidate> <div class="modal-body"> <div class="d item form-group"> <label class="col-sm-3 control-label">Hearing Number</label> <div class="col-sm-7"> <input type="number" class="form-control" id="hearing_lup" name="hearing_lup" readonly="readonly"/> </div> </div> <div class="f item form-group" id = "act1"> <label class="control-label col-md-3 col-sm-3 col-xs-12">Lupong 1 <span class="required">*</span></label> <div class="col-sm-7"> <input name="actlupong[]" id="search_keyword_id_act" class="search_keyword_act form-control col-md-7 col-xs-12" required="required" placeholder ="Search first name or last name... " type="text"> <input type="hidden" name="act_lupong[]" id="act_lup1"/> <div id="result3"></div> </div> </div> <div class="f item form-group" id = "act2"> <label class="control-label col-md-3 col-sm-3 col-xs-12">Lupong 2 <span class="required">*</span></label> <div class="col-sm-7"> <input name="actlupong[]" id="search_keyword_id_act1" class="search_keyword_act1 form-control col-md-7 col-xs-12" required="required" placeholder ="Search first name or last name... " type="text"> <input type="hidden" name="act_lupong[]" id="act_lup2"/> <div id="result4"></div> </div> </div> <div class="f item form-group" id = "act3"> <label class="control-label col-md-3 col-sm-3 col-xs-12">Lupong 3 <span class="required">*</span></label> <div class="col-sm-7"> <input name="actlupong[]" id="search_keyword_id_act2" class="search_keyword_act2 form-control col-md-7 col-xs-12" required="required" placeholder ="Search first name or last name... " type="text"> <input type="hidden" name="act_lupong[]" id="act_lup3"/> <div id="result5"></div> </div> </div> </div> <div class="modal-footer" style="margin:0;"> <button type="button" class="btn btn-default" data-dismiss="modal" style="margin-top: 4px;">Close</button> <button id="send" type="submit" class="btn btn-success" name="update_actual_lupong">Save Record</button> </div> </form> </div> </div> </div> 

Screenshot: enter image description here

What seems wrong to me in my code? Which part should I change? Your help would be greatly appreciated. Thanks.

+7
arrays php mysql bootstrap-modal
source share
2 answers

As you said: - There are three inputs. If in those 3 inputs, a value occurs twice or thrice, it will issue an error. There are three inputs. If in those 3 inputs, a value occurs twice or thrice, it will issue an error.

Your code requires bit modification: -

 <?php include 'config.php'; if(isset($_POST['update_actual_lupong'])){ $id = isset($_GET['id'])? $_GET['id'] : ""; $id_hearing = $_POST['hearing_lup']; $lupong = $_POST['act_lupong']; if (count(array_unique($lupong)) < count($lupong))) { // check that count of unique $lupong and original $lupong is equal or not if not then $lupong have duplicate values echo '<br><br>this array contains DUPLICATE <br><br>'; } else { foreach ($lupong as $lup) { $updateMemo = mysqli_query($conn, "INSERT INTO actuallupong(Hearing_idHearing, bar_pos2) VALUES ('$id_hearing', (SELECT idtable_position FROM table_position WHERE Person_idPerson = '$lup'));"); } echo "ADDED ggg"; } //header ('location: view_case_profile.php?id='.$id); mysqli_close($conn); } ?> 
+4
source share

You exit the loop with $ aaa as the value of c, so check this value only for duplication.

You should check for duplicates inside the loop and set the ie variable

 $dup = false; foreach($lupong as $aaa) { $actual[] = $aaa; if ((count(array_keys($actual, $aaa)) > 1)) { $dup = true; } } if ($dup) { echo '<br><br>this array contains DUPLICATE <br><br>'; } else { foreach ($lupong as $lup) { $updateMemo = mysqli_query($conn, "INSERT INTO actuallupong(Hearing_idHearing, bar_pos2) VALUES ('$id_hearing', (SELECT idtable_position FROM table_position WHERE Person_idPerson = '$lup'));"); } echo "ADDED ggg"; } 
+4
source share

All Articles