数组插入数据库

时间:2012-09-21 19:08:49

标签: php

我试图在php中创建一个学生列表并获取其每个值并将其插入数据库中。

$b_lname = $_POST['bstudnt_lname'];
$b_fname = $_POST['bstudnt_fname'];
$b_mname = $_POST['bstudnt_mname'];

$department = $_POST['class_department'];
$section = $_POST['section_name'];
$year_grade = $_POST['faclty_advse_year'];

$school_id ="a3arew542q3dq";

$counter2 = 0;

$button = $_POST['add_students'];

if(isset($button)){

    foreach($b_lname as $blast => $x){ 
        $arraylname[] ="".$x;
        }  
    foreach($b_fname as $bfirst => $y){
            $arrayfname[] ="".$y;
            $counter2+=1;
        }
    foreach($b_mname as $bmid => $z){
            $arraymname[] ="".$z;
            $counter2+=1;
        }   



        for($v=0;$v<=$counter2-1;$v++){

            if($arrayfname[$v]!="" && $arraymname[$v]="" &&   $arraylname[$v]=""){

            $insert = "INSERT INTO  students(school_id,firstname,middlename,lastname,gender,department,section,year_grade) 
           VALUES('$school_id','$arrayfname[$v]','$arraymname[$v]','$arraylname[$v]','male','$department','$section','$year_grade')";

                  if(@!mysql_query($insert)){
                    die('error insert'.mysql_error());


                    }
            }



        }

}

但是我遇到麻烦,如果数组没有值,它就不会插入数据库中。 我做了一个if语句,但我猜它没用。

2 个答案:

答案 0 :(得分:0)

if (count($array)) {
  //insert into db
} else {
  // array is emtpy
}

答案 1 :(得分:0)

问题在于:

if($arrayfname[$v]!="" && $arraymname[$v]="" &&   $arraylname[$v]=""){

您使用单个=作为后两个条件。将其更改为=====