通过复选框插入

时间:2010-11-23 13:51:34

标签: php sql checkbox insert

我正在尝试通过复选框插入,但它只保存一条记录,假设在表格中我有10条记录,它只保存第10条记录。我认为每个人都有问题。任何人都可以帮助我吗?

<form id="assign" name="assign" method="post" action="assign_individual_exec.php">

    <tr><td class="style3">Select course name:</td>
    <td><select name="name" class="principal2"><?php select_coursename(); ?></select></td></tr>
    <tr><td class="style3"> Start Date</td>
<td><input type="date" name="sdate" ></td><td><script language="JavaScript">
    new tcal ({
        'formname': 'assign',

        'controlname': 'sdate'
    });

    </script></td></tr>



<?php

include('member_table.php');
?>

    <tr>
      <div align="right"class="lien">

        <td><input type="submit" name="individual" value="assign to individual"  />

member_table.php

<?php


$member_id = $_SESSION['SESS_MEMBER_ID'];

   $first= mysql_query("select userid from dept_user where lead_id=$member_id");

echo "<table border='1' id=coursetable class=coursetable>
<tr>
<th>first name</th>
<th>last name</th>
<th>dept name</th>
<th>title</th>
<th>rolename</th>
<th>leader first name</th>
<th>leader last name</th>
</tr>";

 while($res = mysql_fetch_array($first)){

$user1 = $res['userid'];


$display= mysql_query("Select user.firstname, user.lastname, department.dept_name, job_title.title, role.rolename, user.userid From user Inner Join  dept_user On user.userid = dept_user.userid Inner Join  department On department.dept_id = dept_user.dept_id Inner Join  job_title On job_title.title_id = user.titleid Inner Join  role On role.roleid = user.roleid Where user.userid = $user1 "); 




$leader = mysql_query("Select firstname , lastname From user Inner Join  dept_user On user.userid = dept_user.lead_id  Where dept_user.lead_id =$member_id");

while(($row1 = mysql_fetch_array($display)) and ($lead = mysql_fetch_array($leader)))

  {

  echo "<tr>";
  echo "<td>" . $row1['firstname'] . "</td>";
  echo "<td>" . $row1['lastname'] . "</td>";
  echo "<td>" . $row1['dept_name'] . "</td>";
  echo "<td>" . $row1['title'] . "</td>";
echo "<td>"  . $row1['rolename'] . "</td>";
echo "<td>"  . $lead['firstname'] ."</td>";
echo "<td>"  . $lead['lastname'] . "</td>";
echo  "<td>" .'<input type="checkbox" name="userid" value='.$row1['userid'].'>'."</td>";
  echo "</tr>";

  }

 if ($user1 !== 0){

 $second = mysql_query("select userid from dept_user where lead_id=$user1");

 while($res1 = mysql_fetch_array($second)){

 $user2= $res1['userid'];



if($user2 !==0) {



   $display2 = mysql_query("Select user.firstname, user.lastname, department.dept_name, job_title.title, role.rolename, user.userid From user Inner Join  dept_user On user.userid = dept_user.userid Inner Join  department On department.dept_id = dept_user.dept_id Inner Join  job_title On job_title.title_id = user.titleid Inner Join  role On role.roleid = user.roleid Where user.userid = $user2 "); 


   $leader1= mysql_query("Select firstname , lastname From user Inner Join  dept_user On user.userid = dept_user.lead_id  Where dept_user.lead_id = $user1");

while(($res1 = mysql_fetch_array($display2))  and ($lead1 = mysql_fetch_array($leader1)))

  {
  echo "<tr>";
  echo "<td>" . $res1['firstname'] . "</td>";
  echo "<td>" . $res1['lastname'] . "</td>";
  echo "<td>" . $res1['dept_name'] . "</td>";
  echo "<td>" . $res1['title'] . "</td>";
echo "<td>"  . $res1['rolename'] . "</td>";
echo "<td>"  . $lead1['firstname'] . "</td>";
echo "<td>"  . $lead1['lastname'] . "</td>";
echo  "<td>" .'<input type="checkbox" name="userid" value='.$res1['userid'].'>'."</td>";
  echo "</tr>";
  }

    if ($user2 !== 0){

    $third = mysql_query("select userid from dept_user where lead_id=$user2");

while($res2 = mysql_fetch_array($third)) {

 $user3 = $res2['userid'];

 echo "<br/>";

 if ($user3 !== 0){


 $display3 = mysql_query("Select user.firstname, user.lastname, department.dept_name, job_title.title, role.rolename, user.userid From user Inner Join  dept_user On user.userid = dept_user.userid Inner Join  department On department.dept_id = dept_user.dept_id Inner Join  job_title On job_title.title_id = user.titleid Inner Join  role On role.roleid = user.roleid Where user.userid = $user3"); 

    $leader2 = mysql_query("Select firstname, lastname From user Inner Join  dept_user On user.userid = dept_user.lead_id  Where dept_user.lead_id = $user2");

while(($row = mysql_fetch_array($display3)) and ($lead2 = mysql_fetch_array($leader2)))


  {

  echo "<tr>";
  echo "<td>" . $row['firstname'] . "</td>";
  echo "<td>" . $row['lastname'] . "</td>";
  echo "<td>" . $row['dept_name'] . "</td>";
  echo "<td>" . $row['title'] . "</td>";
echo "<td>"  . $row['rolename'] . "</td>";
echo "<td>"  . $lead2['firstname'] . "</td>";
echo "<td>"  . $lead2['lastname'] . "</td>";
echo  "<td>" .'<input type="checkbox" name="userid" value='.$row['userid'].'>'."</td>";
  echo "</tr>";
  }

}
    }
}
}
}

}
}
echo"</table>";
?>

assign_training_exec.php

<?php
include('include.php'); 
    include('config.php');
    require_once('auth.php');
    session_start();


$user[]= clean($_POST['userid']);
    $name = clean($_POST['name']);
    $date = clean($_POST['sdate']);
$month = substr($date, 0 ,2);
 $day= substr($date, 3, -5); 
$yr = substr($date, 6); 
$dash = "-";
$date_str = ($yr.$dash.$month.$dash.$day);


$m_name = $_SESSION['SESS_FIRST_NAME'];

   foreach ($user as $user_array){
    $insert_query = "INSERT INTO course_detail(userid, course_id, date_assign, owner) VALUES('$user_array','$name','$date_str', '$m_name')"; 
    $insert = mysql_query($insert_query); 
}



    if($insert_buffer) {

                exit();
    }else {
        die("Query failed");
    }

?>

5 个答案:

答案 0 :(得分:0)

// this assigns the result of the clean() to the new end element of the array
// $user[] = clean($_POST['userid']);
// 
// should be more like:
$user = array_clean($_POST['userid']);

我不熟悉你的clean(),但你需要使用数组返回函数,而不是单个值。 clean()你的使用可能适用于数组和标量,但如果没有,请尝试下面的那个:

function array_clean( $input_array ) {
   $clean_array = new array();
   if( !is_array($input_array) ) {
    $clean_array[] = clean($input_array);
    return $clean_array;
   }
   foreach( $input_array as $key => $value ) {
      $clean_array[$key] = clean($value);
   }
    return $clean_array;
}

另外,Rodolphe指出,你需要在你想要作为一个数组返回的字段上附加[](感谢注意,Rodolphe)......

while( /*...*/ ) {
   //...
   echo  "<td>" .'<input type="checkbox" name="userid[]" value='.$row1['userid'].'>'."</td>";
   //...
}
enter code here

答案 1 :(得分:0)

我无法从您的文本中做任何事情,但最常见的错误可能是复选框的名称。在其名称中添加“[]”,PHP将自动将其转换为包含所有选中值的数组。

更改

<input type="checkbox" name="the_name">

<input type="checkbox" name="the_name[]">

答案 2 :(得分:0)

您应该检查数组是否存在。如果未选中任何复选框,则$ _POST-数组中不存在任何条目。

答案 3 :(得分:0)

更改* member_table.php中的这一行*:

(当前行:)

echo  "<td>" .'<input type="checkbox" name="userid" value='.$row1['userid'].'>'

更改为:

echo  "<td>" .'<input type="checkbox" name="userid[]" value='.$row1['userid'].'>'

输入名称属性中的[]启用迭代它。

答案 4 :(得分:0)

感谢大家...我之前尝试过在名字上应用数组但是没有用。这次我删除了清洁功能。它运行良好并保存在数据库中。但提供错误消息“查询失败”。这可以修复,只是最后一个代码.. thx guys ..