0

我有一个表格,允许通过选中可以正常工作的复选框将工程师添加到工作中。但是,我希望用户通过取消选中另一个表单上的复选框来删除工程师。

该表(多对多表)有 3 列 id_ce(主键)、call_ce(外键)和engineer_ce(外键)。我希望表单检查复选框是否为空,以及是否检查表中是否有带有 call_ce 和 Engineer_ce 的条目,如果存在则将其删除,但我失败了。

这是我目前所拥有的......

foreach($_POST['engineer'] as $engineer_id){
    if(!isset($_POST['engineer'])){
        $sql = "SELECT * FROM calls_engineers WHERE call_ce = '$diary_id' AND engineer_ce = '$engineer_id'";
        $result = mysql_query($sql)or die(mysql_error());
        if(mysql_num_rows($result) > 0){
            $sql = "DELETE FROM calls_engineers WHERE engineer_ce = '$engineer_id'";
            $result = mysql_query($sql)or die(mysql_error());
        }   
    }
}

我怀疑问题可能是未选中的复选框未发布,因此它们不会通过 foreach 循环,但我不确定另一种方法。

任何帮助将不胜感激

4

2 回答 2

4

您可以在页面上放置一个带有工程师 ID 的隐藏字段。说,它会被称为engineer_on_page

现在你可以

foreach($_POST['engineer_on_page'] as $engineer_id) {
     if(!in_array($engineer_id, $_POST['engineer'])){
          //do delete here
     }
}

我希望复选框看起来类似于:

<input type="checkbox" name="engineer[]" value="[engineer_id]"/>

和隐藏字段

<input type="hidden" name="engineer_on_page[]" value="[engineer_id]"/>

您也可以像这样指定复选框字段:

<input type="checkbox" name="engineer[[engineer_id]]" value="[engineer_id]"/>

实际 ID在哪里[engineer_id](1、2、3 等)

在第二种情况下,您可以使用

     if(!isset($_POST['engineer'][$engineer_id])){
          //do delete here
     }
于 2012-09-14T21:55:04.107 回答
0

/* 通过罗希特·巴亚尔 */

$checkbox1=$_POST['stu2']; $chk="";

/* print_r($_POST['stu3']); */
$order1=$_POST['stu3'];

  if(isset($_POST['subject']))
  {

$string = implode(',', $checkbox1);

 echo $del="delete from student_subject where sub_id not in ($string)";
 mysql_query($del);

  }
  //   mysql_query($del);

 foreach ($checkbox1 as $key => $chk1)
 {  
  $chk .= $chk1.",";  

 $check="SELECT * FROM student_subject WHERE sub_id = '".$chk1."' AND form_id='".$_GET['sub']."'";
 $rs = mysql_query($check);
 $data = mysql_fetch_array($rs);
 mysql_num_rows($rs);
 if($data[0] > 1)
  {
 echo "Subject is Already Exists<br/>";

  }

else
  {

  if(isset($_POST['subject']))
  {
     $a= "sub_id='".$chk1."'";



     echo $ins = "insert into student_subject set form_id='".$_POST['stu1']."',
                                    ".$a.",
                     order_no='".$chk1."'";


  }
if (mysql_query($ins))
{
    echo "Now New subject Is Added<br/>";
}
else
{
    echo "Error adding user in database<br/>";
}

} }

?>

于 2016-06-18T06:57:11.673 回答