使用复选框php&更新数据库表MySQL的 [英] Update database table with checkboxes, php & mysql

查看:69
本文介绍了使用复选框php&更新数据库表MySQL的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create a form that allows you to add administrator controls to users on a database with the use of check boxes. Currently I am able to list the users. I don't know how to update the database when the user selects the check box and clicks submit. Here is what I have so far;

<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");    

}

任何帮助将不胜感激
谢谢

Any help would be greatly appreciated Thanks

更新

我认为我对此有所了解。到目前为止,我已经拥有了它,它会更新数据库tho。

I think im getting somewhere with this. I have this so far, it update database tho..

<?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(); ?>


推荐答案

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

Your $row_Recordset1 array seems to contain the data of your database. Use the primary key of each row (e.g. username or id) and put it to the value of the checkbox. Therefore you can use a html code like this

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

在PHP中,您将得到一个数组,用于 $ _ POST ['enable'] 包含您已选中复选框的所有值。遍历数组并使用类似以下代码的内容更新数据库

In PHP you will get an array for $_POST['enable'] that contain all your values for the checked checkboxes. Loop through the array and use something like the following code to update the database

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

别忘了清洁 $ _ POST 的值,然后循环执行以防止不必要的输入!

Don't forget to sanitize the values of $_POST before looping to prevent unwanted inputs!

对您的html代码进行注释:将< form> 标记。您使用form标记的方式将导致代码无效。您为每个do循环创建一个表单标签,但最后只用一个< / form> 关闭所有标签。

One comment to your html code: put the <form> tag around the table. The way you use the form tag will result in invalid code. You create a form tag for each do loop but close all the tags with just one single </form> at the end.

这篇关于使用复选框php&amp;更新数据库表MySQL的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆