如何在插入查询之前同时foreach循环2数组

时间:2022-10-23 21:35:45

I want to Insert data using foreach loop and the thing is I want to foreach loop two arrays simultaneously however in the code I got so far it there are duplicates in the database after insertion. So far,this is the php code I got for inserting:

我想使用foreach循环插入数据,事情是我想同时循环两个数组但是在我到目前为止所获得的代码中,插入后数据库中有重复数据。到目前为止,这是我插入的php代码:

    <?php 
include('dbcon.php');
if (isset($_POST['submit'])){
$month = $_POST['month'];
$guard = $_POST['guard_id'];
$time = $_POST['time_id'];

$area1 =  $_POST['aream'];
$area2 =  $_POST['areat'];
$area3 =  $_POST['areaw'];
$area4 =  $_POST['areath'];
$area5 =  $_POST['areaf'];

$m = $_POST['m'];
$t = $_POST['t'];
$w = $_POST['w'];
$th = $_POST['th'];
$f = $_POST['f'];





    foreach($area1 as $aream)
    {                       
//monday sched
foreach ($m as $daym){

    //check conflict

                $query=mysql_query("select *,COUNT(*) as count from gov_sched 
                natural join member natural join time where guard_id='$guard' and gov_sched.time_id='$daym' and area='$aream' 
                and day='m' and month='$month'")or die(mysql_error());
                    $row=mysql_fetch_array($query);
                    $count=$row['count'];
                    $time1=date("h:i a",strtotime($row['time_in']))."-".date("h:i a",strtotime($row['time_out']));
                    $guard1=$row['lastname']." ".$row['firstname'];
                    $area1=$row['area'];

                $queryt=mysql_query("select * from member where member_id='$guard'")or die(mysql_error());
                        $rowt=mysql_fetch_array($queryt);
                        $guardt=$rowt['lastname']." ".$rowt['firstname'];

                $querytime=mysql_query("select * from time where time_id='$daym'")or die(mysql_error($con));
                        $rowt=mysql_fetch_array($querytime);
                        $timet=date("h:i a",strtotime($rowt['time_in']))."-".date("h:i a",strtotime($rowt['time_out']));    


                if ($count==0)
                {

                    mysql_query("INSERT INTO gov_sched(month,guard_id,area,time_id,day) VALUES ('$month','$guard','$aream','$daym','m')")or die(mysql_error());

                    echo "<span class='text-success'>$month $timet $guardt at $aream every Monday successfully added!</span><br>";    
                }            
                else{

                    echo "<span class='text-danger'>$month $time1 is already taken by $guard1 at $aream every Monday </span><br>";

                }

            }
        }



              foreach($area2 as $areat)
    {        


    foreach ($t as $dayt){
    //check conflict
         $query=mysql_query("select *,COUNT(*) as count from gov_sched 
            natural join member natural join time where guard_id='$guard' and gov_sched.time_id='$dayt' and area='$areat' 
            and day='t' and month='$month'")or die(mysql_error());
                $row=mysql_fetch_array($query);
                $count=$row['count'];
                $time1=date("h:i a",strtotime($row['time_in']))."-".date("h:i a",strtotime($row['time_out']));
                $guard1=$row['lastname']." ".$row['firstname'];
                $area1=$row['area'];

            $queryt=mysql_query("select * from member where member_id='$guard'")or die(mysql_error());
                    $rowt=mysql_fetch_array($queryt);
                    $guardt=$rowt['lastname']." ".$rowt['firstname'];

            $querytime=mysql_query("select * from time where time_id='$dayt'")or die(mysql_error($con));
                    $rowt=mysql_fetch_array($querytime);
                    $timet=date("h:i a",strtotime($rowt['time_in']))."-".date("h:i a",strtotime($rowt['time_out']));    


            if ($count==0)
            {
                mysql_query("INSERT INTO gov_sched(month,guard_id,area,time_id,day) VALUES ('$month','$guard','$areat','$dayt','t')")or die(mysql_error());

                echo "<span class='text-success'>$month $timet $guardt at $areat every Tuesday successfully added!</span><br>";    
            }            
            else{

                echo "<span class='text-danger'>$month $time1 is already taken by $guard1 at $areat every Tuesday </span><br>";

            }

        }
    }


            foreach($area3 as $areaw)
{

        foreach ($w as $dayw){
    //check conflict
    $query=mysql_query("select *,COUNT(*) as count from gov_sched 
    natural join member natural join time where guard_id='$guard' and gov_sched.time_id='$dayw' and area='$areaw' 
    and day='w' and month='$month'")or die(mysql_error());
        $row=mysql_fetch_array($query);
        $count=$row['count'];
        $time1=date("h:i a",strtotime($row['time_in']))."-".date("h:i a",strtotime($row['time_out']));
        $guard1=$row['lastname']." ".$row['firstname'];
        $area1=$row['area'];

    $queryt=mysql_query("select * from member where member_id='$guard'")or die(mysql_error());
            $rowt=mysql_fetch_array($queryt);
            $guardt=$rowt['lastname']." ".$rowt['firstname'];

    $querytime=mysql_query("select * from time where time_id='$dayw'")or die(mysql_error($con));
            $rowt=mysql_fetch_array($querytime);
            $timet=date("h:i a",strtotime($rowt['time_in']))."-".date("h:i a",strtotime($rowt['time_out']));    


    if ($count==0)
    {
        mysql_query("INSERT INTO gov_sched(month,guard_id,area,time_id,day) VALUES ('$month','$guard','$areaw','$dayw','w')")or die(mysql_error());

        echo "<span class='text-success'>$month $timet $guardt at $areaw every Wednesday successfully added!</span><br>";    
    }            
    else{

        echo "<span class='text-danger'>$month $time1 is already taken by $guard1 at $areaw every Wednesday </span><br>";

    }

   }
}



foreach($area4 as $areath)
                {
            foreach ($th as $dayth){
    //check conflict

                            $query=mysql_query("select *,COUNT(*) as count from gov_sched 
                            natural join member natural join time where guard_id='$guard' and gov_sched.time_id='$dayth' and area='$areath' 
                            and day='th' and month='$month'")or die(mysql_error());
                                $row=mysql_fetch_array($query);
                                $count=$row['count'];
                                $time1=date("h:i a",strtotime($row['time_in']))."-".date("h:i a",strtotime($row['time_out']));
                                $guard1=$row['lastname']." ".$row['firstname'];
                                $area1=$row['area'];

                            $queryt=mysql_query("select * from member where member_id='$guard'")or die(mysql_error());
                                    $rowt=mysql_fetch_array($queryt);
                                    $guardt=$rowt['lastname']." ".$rowt['firstname'];

                            $querytime=mysql_query("select * from time where time_id='$dayth'")or die(mysql_error($con));
                                    $rowt=mysql_fetch_array($querytime);
                                    $timet=date("h:i a",strtotime($rowt['time_in']))."-".date("h:i a",strtotime($rowt['time_out']));    


                            if ($count==0)
                            {
                                mysql_query("INSERT INTO gov_sched(month,guard_id,area,time_id,day) VALUES ('$month','$guard','$areath','$dayth','th')")or die(mysql_error());

                                echo "<span class='text-success'>$month $timet $guardt at $areath every Thursday successfully added!</span><br>";    
                            }            
                            else{

                                echo "<span class='text-danger'>$month $time1 is already taken by $guard1 at $areath every Thursday </span><br>";

                            }

                        }
                    }

 foreach($area5 as $areaf)
                    {

                foreach ($f as $dayf){
    //check conflict

                                $query=mysql_query("select *,COUNT(*) as count from gov_sched 
                                natural join member natural join time where guard_id='$guard' and gov_sched.time_id='$dayf' and area='$areaf' 
                                and day='f' and month='$month'")or die(mysql_error());
                                    $row=mysql_fetch_array($query);
                                    $count=$row['count'];
                                    $time1=date("h:i a",strtotime($row['time_in']))."-".date("h:i a",strtotime($row['time_out']));
                                    $guard1=$row['lastname']." ".$row['firstname'];
                                    $area1=$row['area'];

                                $queryt=mysql_query("select * from member where member_id='$guard'")or die(mysql_error());
                                        $rowt=mysql_fetch_array($queryt);
                                        $guardt=$rowt['lastname']." ".$rowt['firstname'];

                                $querytime=mysql_query("select * from time where time_id='$dayf'")or die(mysql_error($con));
                                        $rowt=mysql_fetch_array($querytime);
                                        $timet=date("h:i a",strtotime($rowt['time_in']))."-".date("h:i a",strtotime($rowt['time_out']));    


                                if ($count==0)
                                {
                                    mysql_query("INSERT INTO gov_sched(month,guard_id,area,time_id,day) VALUES ('$month','$guard','$areaf','$dayf','f')")or die(mysql_error());

                                    echo "<span class='text-success'>$timet $guardt at $areaf every Friday successfully added!</span><br>";    
                                }            
                                else{

                                    echo "<span class='text-danger'>$time1 is already taken by $guard1 at $areaf every Friday </span><br>";

                                }

                            }
                        }
}
?>  

I think this way of using foreach loop isnt fit to my insertion of data so any help I could get there please? thanks in advance.

我认为这种使用foreach循环的方式不适合我的数据插入所以任何帮助我都能到达那里吗?提前致谢。

1 个解决方案

#1


1  

you are naming dropdowns as area_id. if you are expecting $_POST['area'] from this html then you must change it to $_POST['area_id'];

您将下拉列表命名为area_id。如果您期望从此html中获得$ _POST ['area'],那么您必须将其更改为$ _POST ['area_id'];

EDITED you can loop $m and $area at the same. eg

EDITED你可以循环$ m和$ area。例如

$i=0;
foreach($m[0] as $a){
    echo $a[0];
    echo $area[0][$i];
    $i++;
}

in this example array size must be same.. else it throw a error.

在此示例中,数组大小必须相同..否则会引发错误。

EDITED you can loop a array like this .

编辑你可以像这样循环一个数组。

 $maxLength = count($m[0]) > count($area[0]) ? count($m[0]) : count($area[0]);
        for($i = 0; $i < $maxLength; ++$i){
         $ma = array_key_exists($i, $m[0]) ? ' '. $m[0][$i] : "";  
         $a= array_key_exists($i, $area[0]) ? ' '. $area[0][$i] : "";
          echo "$ma $a <br/>";
     }

let me know if it didn't work.

如果它不起作用,请告诉我。

#1


1  

you are naming dropdowns as area_id. if you are expecting $_POST['area'] from this html then you must change it to $_POST['area_id'];

您将下拉列表命名为area_id。如果您期望从此html中获得$ _POST ['area'],那么您必须将其更改为$ _POST ['area_id'];

EDITED you can loop $m and $area at the same. eg

EDITED你可以循环$ m和$ area。例如

$i=0;
foreach($m[0] as $a){
    echo $a[0];
    echo $area[0][$i];
    $i++;
}

in this example array size must be same.. else it throw a error.

在此示例中,数组大小必须相同..否则会引发错误。

EDITED you can loop a array like this .

编辑你可以像这样循环一个数组。

 $maxLength = count($m[0]) > count($area[0]) ? count($m[0]) : count($area[0]);
        for($i = 0; $i < $maxLength; ++$i){
         $ma = array_key_exists($i, $m[0]) ? ' '. $m[0][$i] : "";  
         $a= array_key_exists($i, $area[0]) ? ' '. $area[0][$i] : "";
          echo "$ma $a <br/>";
     }

let me know if it didn't work.

如果它不起作用,请告诉我。