【问题标题】:Insert 0 or 1 into MySQL based on checkbox value根据复选框值将 0 或 1 插入 MySQL
【发布时间】:2014-04-17 20:47:06
【问题描述】:

我想通过使用 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;
          }
       ?>

任何建议将不胜感激。

【问题讨论】:

  • 表单正在发布,您正在寻找 $_GET 变量。
  • 停止使用mysql也不错,开始使用msqli或pdo..
  • 请避免使用mysql防止SQL注入..

标签: php mysql checkbox


【解决方案1】:

试试下面的代码

错误

  • 首先您在表单中使用了POST 方法并使用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'] ?>" />

【讨论】:

  • 我试着按照你的方式去做。但是,它不适用于此编码。插入或修改数据库,这将是“数组”。无法插入“0”或“1”。
【解决方案2】:

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

<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" />

【讨论】:

  • 如果我遵循这个编码。您能告诉我如何使用同一页面修改数据库吗?
  • 感谢Christoph,修改为mysql成功。好的!但是,它不能显示在 mysql 数据库的同一页面上。
  • 嘿 Christoph,我想显示在 mysql 数据库的同一页面上。例如,我有 2 个可选选项,仅在 edit.php 上创建信息和修改信息。我试着按照你的指令编码。但是,当我选择 subssm 框时,它不会显示信息。
  • 最后,我已经解决了这个问题。非常感谢您的任何建议。
猜你喜欢
  • 2015-07-19
  • 2011-12-29
  • 2016-06-09
  • 2011-04-10
  • 1970-01-01
  • 2012-08-20
  • 2018-07-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多