根据复选框值将0或1插入MySQL

时间:2014-03-13 08:40:47

标签: php mysql checkbox

我想使用php复选框将"0""1"插入到mysql数据库中。但是,它无法修改或插入mysql数据库。

我的编码edit.php

<?php 
switch($_GET['action']) {
  case "Modify":
  /*************************** case "modify" **********************/
    $colname_RsStaff = "1";
    if (isset($_GET['staffid'])) {
      $colname_RsStaff = (get_magic_quotes_gpc()) 
                         ? $_GET['staffid'] : addslashes($_GET['staffid']);
    }
    mysql_select_db($database_conn, $conn);
    $query_RsStaff = "SELECT * FROM `tbl_staff`
                      WHERE `tbl_staff`.`staffid` = '".$_GET['staffid']."'";
    $RsStaff = mysql_query($query_RsStaff, $conn) or die(mysql_error());
    $row_RsStaff = mysql_fetch_assoc($RsStaff);
    $totalRows_RsStaff = mysql_num_rows($RsStaff);

    $subagent = $row_RsStaff['subagent'];
    $subum = $row_RsStaff['subum'];
    $subssm = $row_RsStaff['subssm'];
    $subtutor = $row_RsStaff['subtutor'];
    break;

  /* ****************Add**************** */
  default:
    $subagent = "";
    $subum = "";
    $subssm = "";
    $subtutor = "";
    break;
}
?> 

<form name="staff" method="post" onsubmit="return validateForm()" action="commit.php?action=<?php echo $_GET['action'] ?>">
   <tr>
      <td height="12" align="right" bgcolor="#CCCCCC"><h5>&nbsp;</h5></td>
      <td height="12" align="left" bgcolor="#dfdfdf">&nbsp;<input type="checkbox" name="subagent[]" value="<?php echo $subagent; ?>" />&nbsp;Agent&nbsp;<input type="checkbox" name="subum[]" value="<?php echo $subum; ?>" />&nbsp;UM&nbsp;</td>
      <td rowspan="2" align="left" bgcolor="#dfdfdf">
      <h6>&nbsp;* Selected one or more</h6></td>
    </tr>
    <tr>
      <td height="12" align="right" bgcolor="#CCCCCC"><h5>&nbsp;</h5></td>
      <td height="12" align="left" bgcolor="#dfdfdf">&nbsp;<input type="checkbox" name="subssm[]" value="<?php echo $subssm; ?>" />&nbsp;SSM&nbsp;&nbsp;  <input type="checkbox" name="subtutor[]" value="<?php echo $subtutor; ?>" />&nbsp;Tutor&nbsp;</td>
    </tr>
    <input type="submit" name="Submit" value="<?php echo $_GET['action'] ?>" />

发布到commit.php

    $editFormAction = $_SERVER['PHP_SELF'];
    if (isset($_SERVER['QUERY_STRING'])) {
     $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
       }

    switch ($_GET['action']) {
    case "Create":
$staffid = $_POST['staffid'];

     mysql_select_db($database_conn, $conn);
     $result ="SELECT * FROM tbl_staff WHERE staffid = '".$_POST['staffid']."'";
      $Staff = mysql_query($result, $conn) or die(mysql_error());
     $totalRows_Staff = mysql_num_rows($Staff);

     $insertSQL = sprintf("INSERT INTO tbl_staff (subagent, subum, subssm, subtutor) VALUES (%s, %s, %s, %s)",
                     GetSQLValueString($_POST['subagent'], "text"),
                 GetSQLValueString($_POST['subum'], "text"),
                         GetSQLValueString($_POST['subssm'], "text"),
                 GetSQLValueString($_POST['subtutor'], "text"),

                     mysql_select_db($database_conn, $conn);
                     $Result1 = mysql_query($insertSQL, $conn) or die(mysql_error());
                  }
                 $insertGoTo = "tr_staff.php";
          if (isset($_SERVER['QUERY_STRING'])) {
          $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
          $insertGoTo .= $_SERVER['QUERY_STRING'];
           }
          header(sprintf("Location: %s", $insertGoTo));
          }
      break;
     /************************* update *************************/
     case "Modify":

    $updateSQL = sprintf("UPDATE tbl_staff SET subagent=%s, subum=%s, subssm=%s, subtutor=%s WHERE staffid=%s",
                           GetSQLValueString($_POST['subagent'], "text"),
                   GetSQLValueString($_POST['subum'], "text"),
                   GetSQLValueString($_POST['subssm'], "text"),
                   GetSQLValueString($_POST['subtutor'], "text"),
                           GetSQLValueString($_POST['staffid'], "int"));

             mysql_select_db($database_conn, $conn);
             $Result1 = mysql_query($updateSQL, $conn) or die(mysql_error());

          $insertGoTo = "tr_staff.php";
             if (isset($_SERVER['QUERY_STRING'])) {
             $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
             $insertGoTo .= $_SERVER['QUERY_STRING'];
           }
          header(sprintf("Location: %s", $insertGoTo));
          //}
           break;
          }
       ?>

任何建议都会受到超级赞赏。

2 个答案:

答案 0 :(得分:0)

尝试以下代码

<强>错误

  • 首先,您已在表单中使用POST metod并使用GET
  • 获取变量
  • 您可以尝试以下方式获取复选框值

    if ($subssm) { <input type='checkbox' name='subssm[]' value='1' /> }else{ <input type='checkbox' name='subssm[]' value='0' /> }

    *的 PHP *

   
           <form name="staff" method="get" onsubmit="return validateForm()" action="commit.php?action=<?php echo $_GET['action'] ?>">
           <tr>
              <td height="12" align="right" bgcolor="#CCCCCC"><h5>&nbsp;</h5></td>
              <td height="12" align="left" bgcolor="#dfdfdf">&nbsp;<input type="checkbox" name="subagent[]" value="<?php echo $subagent; ?>" />&nbsp;Agent&nbsp;<input type="checkbox" name="subum[]" value="<?php echo $subum; ?>" />&nbsp;UM&nbsp;</td>
              <td rowspan="2" align="left" bgcolor="#dfdfdf">
              <h6>&nbsp;* Selected one or more</h6></td>
            </tr>
            <tr>
              <td height="12" align="right" bgcolor="#CCCCCC"><h5>&nbsp;</h5></td>
              <td height="12" align="left" bgcolor="#dfdfdf">&nbsp;
    <?php
    if ($subssm) {
        <input type='checkbox' name='subssm[]' value='1' />
    }else{
        <input type='checkbox' name='subssm[]' value='0' />
    }
    ?>

    &nbsp;SSM&nbsp;&nbsp;  <input type="checkbox" name="subtutor[]" value="<?php echo $subtutor; ?>" />&nbsp;Tutor&nbsp;</td>
            </tr>
            <input type="submit" name="Submit" value="<?php echo $_GET['action'] ?>" />

答案 1 :(得分:0)

这是一个小技巧: 使用复选框时,只需在复选框前添加隐藏字段:

<input type="hidden" name="checkbox" value="0" />
<input type="checkbox" name="checkbox" value="1" />

然后您可以使用

很好地访问该值
<?php
   $checkboxValue = (isset($_POST['checkbox'])) ? intval($_POST['checkbox']) : 0; // returns 0 or 1
?>

并保存了一些代码行。

在您的代码中,我会替换

<input type="checkbox" name="subssm[]" value="<?php echo $subssm; ?>" />

<input type="hidden" name="subssm" value="0" />
<input type="checkbox" name="subssm" <?php if($subssm == "1") echo "checked='checked'"; ?> value="1" />