Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
若数组中已经存在值,则在PHP中发出错误_Php_Mysql_Arrays_Bootstrap Modal - Fatal编程技术网

若数组中已经存在值,则在PHP中发出错误

若数组中已经存在值,则在PHP中发出错误,php,mysql,arrays,bootstrap-modal,Php,Mysql,Arrays,Bootstrap Modal,我有一个代码,它检查数组中是否已经存在一个值。基本上,程序所做的是首先存储数组中的所有值。然后将使用countarray_keys函数检查它。有三种输入。如果在这3个输入中,某个值出现两次或三次,则会发出错误。现在,我的问题是,如果输入A和输入B相同,但是输入C不同,它仍然会添加到数据库中,但是如果输入A和C相同,但是输入B不同,那么它不会添加正确的内容 以下是我的php代码: <?php include 'config.php'; if(isset($_POST['update_actu

我有一个代码,它检查数组中是否已经存在一个值。基本上,程序所做的是首先存储数组中的所有值。然后将使用countarray_keys函数检查它。有三种输入。如果在这3个输入中,某个值出现两次或三次,则会发出错误。现在,我的问题是,如果输入A和输入B相同,但是输入C不同,它仍然会添加到数据库中,但是如果输入A和C相同,但是输入B不同,那么它不会添加正确的内容

以下是我的php代码:

<?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代码它处于模式中:

<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>
截图:

我的代码中似乎有什么错误?我应该换哪一部分?非常感谢你的帮助。谢谢。

正如你所说:-有三个输入。如果在这3个输入中,某个值出现两次或三次,则会发出错误

需要对代码进行一些修改:-

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

退出循环时,c的值为$aaa,因此仅检查此值是否存在重复

您应该检查循环中是否存在重复项,并设置一个变量,即

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

如果你把数组键$actual,$aaa的结果变为dump,会发生什么?你只是在搜索最后一个数组值,所以如果c和a相等,那么你会得到一个2的计数,但是如果c和其他的不同,你会得到一个计数,你需要把if放在循环中哦,哇,我不敢相信我没有看到。。。$aaa所在的作用域在检查数组是否包含重复项的作用域中不存在。您甚至可以使用$aaa,这是因为PHP的cough类型系统cough。例如,可以将复制检查放在Frach循环中。“WavaMaXimOFF”作为一个旁白,您应该考虑使用MySQLIN的BdIDPARAM来避免SQL注入。可能是三个答案中最好的。@A-2-A告诉你。是A.救生员。非常感谢你。它现在工作得很好。我试过各种组合,现在都能用了。非常感谢!!!!!!!!非常感谢!!!!!!哇,这也有用!!!非常感谢你!!这解决了我的问题!!我可以问一下$dup的目的是什么吗?再次感谢大家!!!!尽管这有上升投票,但我很高兴它不是被接受的答案,因为被接受的答案更干净、更高效。@wandamaximoff$dup只是用来存储发现了副本的事实