Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If value already exists in array then issue an error in PHP

I have a code wherein it checks if a value already exists in the array. Basically, what the program does is that it first stores all the values in the array. Then it will be checked using count(array_keys) function. There are three inputs. If in those 3 inputs, a value occurs twice or thrice, it will issue an error. Now, my problem is that if INPUT A AND INPUT B IS THE SAME BUT INPUT C is different, it will still add to the database, BUT IF INPUT A AND C IS THE SAME BUT INPUT B is different then it will not add (which is correct).

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 (it's in a 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 to be wrong in my code? Which part should I change? Your help will be much appreciated. Thank you.

like image 687
Isabella Avatar asked Feb 08 '16 11:02

Isabella


People also ask

How do you check if a value exists in an array PHP?

The in_array() function is an inbuilt function in PHP that is used to check whether a given value exists in an array or not. It returns TRUE if the given value is found in the given array, and FALSE otherwise.

What is In_array function in PHP?

The in_array() function searches an array for a specific value. Note: If the search parameter is a string and the type parameter is set to TRUE, the search is case-sensitive.


1 Answers

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

A bit modification to your 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 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);
}   
?>
like image 135
Anant Kumar Singh Avatar answered Sep 28 '22 08:09

Anant Kumar Singh