0

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:

    <?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.

19
  • The array you are using to iterate is empty (boolean) or another value other than an array on line 15. Just figure out why that is not an array. Commented Sep 3, 2017 at 14:54
  • the start of the first for each loop is line 15. how do I know it's an array? Commented Sep 3, 2017 at 14:59
  • Well presumably it's saying $area is supposed to be an array? If it's not an array, then you can't use foreach() on it. Use if(!is_array($area)) { print_r($area); die('Not an array'); } before the foreach(). Commented Sep 3, 2017 at 15:14
  • @Rasclatt Will it still iterate and insert the data into the database? because it is from multiple dropdowns populated by data from database. Commented Sep 3, 2017 at 15:19
  • No, not in my snippet cause it dies. I don't know what your script is doing, you can check if array and then display whatever inside that if block. Commented Sep 3, 2017 at 15:22

1 Answer 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'];

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

$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.

Sign up to request clarification or add additional context in comments.

1 Comment

Comments are not for extended discussion or debugging sessions. If you are still having trouble and need to add more information, edit the question. Mahmood can then update his answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.