Home Ask Login Register

Developers Planet

Your answer is one click away!

Wanda Maximoff February 2016

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

I have a code where it checks if a value already exists in the array. Basically, what the program does is that it first stores all the value 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        

Answers


Anant February 2016

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);
}   
?>


Egg Vans February 2016

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

You should check for duplicates inside the loop and set a variable i.e.

$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";
}

Post Status

Asked in February 2016
Viewed 3,809 times
Voted 7
Answered 2 times

Search




Leave an answer


Quote of the day: live life