【问题标题】:Update database table with checkboxes, php & mysql使用复选框、php 和 mysql 更新数据库表
【发布时间】:2012-06-03 09:33:21
【问题描述】:

我正在尝试创建一个表单,该表单允许您使用复选框向数据库上的用户添加管理员控件。目前我可以列出用户。当用户选中复选框并单击提交时,我不知道如何更新数据库。这是我目前所拥有的;

<table class="fileTable" border="1">
  <tr>
    <th scope="col">Username</th>
    <th scope="col">First Name</th>
    <th scope="col">Surname</th>
    <th scope="col">Email Address</th>
    <th scope="col">Enabled</th>
  </tr>
  <?php do { ?>

  <tr>

    <td><?php echo $row_Recordset1['username']; ?></td>
    <td><?php echo $row_Recordset1['fName']; ?></td>
    <td><?php echo $row_Recordset1['sName']; ?></td>
    <td><?php echo $row_Recordset1['email']; ?></td>

    <form action="" method="post">
    <td>
    <input type="checkbox" name="enable" id="enable"></td>
    </td>



  </tr>
  <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
     <input class="submit" type="submit" value="Submit" name="submit">
    </form>
</table>


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

$query = mysql_query("UPDATE student SET enable = 1 WHERE");    

}

任何帮助将不胜感激 谢谢

更新

我想我在这方面有所收获。到目前为止,我有这个,它会更新数据库..

<?php require_once('Connections/localhost.php'); ?>
<? ob_start(); ?>
<?php

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

foreach($_POST['enable'] as $enable) {
    $query = mysql_query('UPDATE student SET enable = 1');
}



}
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$colname_Recordset1 = "-1";
if (isset($_GET['1'])) {
  $colname_Recordset1 = $_GET['1'];
}
mysql_select_db($database_localhost, $localhost);
$query_Recordset1 = sprintf("SELECT * FROM student WHERE enable = 0", GetSQLValueString($colname_Recordset1, "int"));
$Recordset1 = mysql_query($query_Recordset1, $localhost) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
$sql="SELECT * FROM student";
?>

<p class="headText">Enable Student</p>

<p>Click the check box for each of the students you would like to enble and click the submit button</p>
<form id="submit" action="" method="post">
<table class="fileTable" border="1">
  <tr>
    <th scope="col">Username</th>
    <th scope="col">First Name</th>
    <th scope="col">Surname</th>
    <th scope="col">Email Address</th>
    <th scope="col">Enabled</th>
  </tr>
  <?php do { ?>

  <tr>

    <td><?php echo $row_Recordset1['username']; ?></td>
    <td><?php echo $row_Recordset1['fName']; ?></td>
    <td><?php echo $row_Recordset1['sName']; ?></td>
    <td><?php echo $row_Recordset1['email']; ?></td>

    <td>
    <input type="checkbox" name="enable[]" value="<?php echo $row_Recordset1['id'] ?>"/>
    </td>


  <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
     <input class="submit" type="submit" value="Submit" name="submit">
    </form>
    </tr>
</table>
 </form>


<?php

mysql_close();


mysql_free_result($Recordset1);
?>
<? ob_flush(); ?>

【问题讨论】:

  • 也许使用复选框值属性作为值用户ID
  • 在您更新的代码中:当循环通过 $_POST['enable'] 时,对于 ALL 学生记录,enable 的值设置为 1。使用'UPDATE student SET enable = 1 WHERE id = "'.$enable.'"' 代替mysql_query()

标签: php mysql checkbox


【解决方案1】:

注意事项:

<input type="checkbox" name="enable" id="enable"> 

应该具有应该包含特定行 val 的值,例如 [这里我们假设用户名是唯一字段,但建议使用 id 而不是用户名]:

<input type="checkbox" name="enable[]" id="enable" value="<?php echo $row_Recordset1['username'];?>">

为了获得它的价值,我们使用:$_POST['enabled']

所以喜欢使用它可能是这样的:

$query1 = mysql_query("UPDATE student SET enable = 0;");
if( isset( $_POST['enabled'] ){
     $strAllUsernameCombined = implode("','", $_POST['enabled']);
     $query1 = mysql_query("UPDATE student SET enable = 0 where username in ('{$strAllUsernameCombined}');");
}

以及列出使用 while 而不是 do while 的最终建议

【讨论】:

    【解决方案2】:
    <input type="checkbox" name="enable" id="enable" value='something' />
    
    <?php
       if(isset($_POST['submit'])){
          if(!empty($_POST['enable'])) {
              //do your update query
          }
       }
    ?>
    

    希望对你有帮助。

    【讨论】:

      【解决方案3】:

      您的$row_Recordset1 数组似乎包含您数据库的数据。使用每行的主键(例如用户名或 id)并将其放入复选框的值。因此,您可以使用这样的 html 代码

      <input type="checkbox" name="enable[]" value="<?php echo $row_Recordset1['id'] ?>">
      

      在 PHP 中,您将获得 $_POST['enable'] 的数组,其中包含您选中复选框的所有值。循环遍历数组并使用类似以下代码来更新数据库

      <?php
      foreach($_POST['enable'] as $enable) {
          $query = mysql_query('UPDATE student SET enable = 1 WHERE id = '.$enable);
          // ...
      }
      

      不要忘记在循环之前清理$_POST 的值,以防止不必要的输入!

      对您的 html 代码的注释:将 &lt;form&gt; 标记放在桌子周围。您使用表单标签的方式将导致无效代码。您为每个 do 循环创建一个表单标签,但最后只使用一个 &lt;/form&gt; 关闭所有标签。

      【讨论】:

        猜你喜欢
        • 2016-12-14
        • 2012-08-16
        • 2013-06-12
        • 2017-06-06
        • 2014-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-04
        相关资源
        最近更新 更多