0

伙计们我试图在 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 声明,但我想它没有用。

4

2 回答 2

0
if (count($array)) {
  //insert into db
} else {
  // array is emtpy
}
于 2012-09-21T19:11:29.857 回答
0

问题在这里:

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

您将单个=用于后两个条件。将它们更改为=====

于 2012-09-21T19:13:18.630 回答