Ciaran
Ciaran

Reputation: 177

Update database table with checkboxes, 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

UPDATE

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

Upvotes: 0

Views: 8073

Answers (3)

mixable
mixable

Reputation: 1158

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'] ?>">

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);
    // ...
}

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

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.

Upvotes: 1

zaw
zaw

Reputation: 684

<input type="checkbox" name="enable" id="enable" value='something' />

<?php
   if(isset($_POST['submit'])){
      if(!empty($_POST['enable'])) {
          //do your update query
      }
   }
?>

Hope it helps.

Upvotes: 1

KoolKabin
KoolKabin

Reputation: 17713

Things to notice:

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

should have value which should contain particular row val like [here we assume username as unique field but recommend the use of id instead of username]:

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

to get its value we use: $_POST['enabled']

so like using on it may be something like:

$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}');");
}

and final suggestion for listing use while instead of do while

Upvotes: 1

Related Questions