mysql - If value already exists in array then issue an error in PHP -
i have code wherein checks if value exists in array. basically, program first stores values in array. checked using count(array_keys)
function. there 3 inputs. if in 3 inputs, value occurs twice or thrice, issue error. now, problem if input , input b same input c different, still add database, if input , c same input b different not add (which correct).
here 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 $aaa) { $actual[] = $aaa; } if ((count(array_keys($actual, $aaa)) > 1)) { echo '<br><br>this array contains duplicate <br><br>'; } else { foreach ($lupong $lup) { $updatememo = mysqli_query($conn, "insert actuallupong(hearing_idhearing, bar_pos2) values ('$id_hearing', (select idtable_position table_position person_idperson = '$lup'));"); } echo "added ggg"; } //header ('location: view_case_profile.php?id='.$id); mysqli_close($conn); } ?>
html code (it's 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">×</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>
what seems wrong in code? part should change? appreciated. thank you.
as said:- there 3 inputs. if in 3 inputs, value occurs twice or thrice, issue error.
a bit modification code needed:-
<?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 count of unique $lupong , original $lupong equal or not if not $lupong have duplicate values echo '<br><br>this array contains duplicate <br><br>'; } else { foreach ($lupong $lup) { $updatememo = mysqli_query($conn, "insert actuallupong(hearing_idhearing, bar_pos2) values ('$id_hearing', (select idtable_position table_position person_idperson = '$lup'));"); } echo "added ggg"; } //header ('location: view_case_profile.php?id='.$id); mysqli_close($conn); } ?>
Comments
Post a Comment