Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2003
    Location
    Belgium
    Posts
    23

    Unanswered: How to remove a row with checkboxes ???

    Hi,

    I'm having trouble writing the code for deleting a row with a checkbox.

    My page allready contains an update and a insert form, which works great, but removing rows with a checkbox stills a pain in the ass.

    This is my php code. I really don't know what I have to put in place of ?????????, or what I specifically have to do, am I doing something wrong ?

    Thanks in advance !!!

    Code:
    <?php require_once('../../Connections/fisherintranet.php'); ?>
    <?php
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
    {
      $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $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;
    }
    
    $editFormAction = $HTTP_SERVER_VARS['PHP_SELF'];
    if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
      $editFormAction .= "?" . $HTTP_SERVER_VARS['QUERY_STRING'];
    }
    
    if ((isset($HTTP_POST_VARS["MM_insert"])) && ($HTTP_POST_VARS["MM_insert"] == "form1")) {
      $insertSQL = sprintf("INSERT INTO licenties (company, license) VALUES (%s, %s)",
                           GetSQLValueString($HTTP_POST_VARS['company'], "text"),
                           GetSQLValueString($HTTP_POST_VARS['license'], "text"));
    
      mysql_select_db($database_fisherintranet, $fisherintranet);
      $Result1 = mysql_query($insertSQL, $fisherintranet) or die(mysql_error());
    }
    
    if ((isset($HTTP_POST_VARS["MM_update"])) && ($HTTP_POST_VARS["MM_update"] == "form3")) {
      $updateSQL = sprintf("UPDATE licenties SET company=%s, license=%s WHERE license=%s",
                           GetSQLValueString($HTTP_POST_VARS['company'], "text"),
                           GetSQLValueString($HTTP_POST_VARS['licupdate'], "text"),
                           GetSQLValueString($HTTP_POST_VARS['license'], "text"));
    
      mysql_select_db($database_fisherintranet, $fisherintranet);
      $Result1 = mysql_query($updateSQL, $fisherintranet) or die(mysql_error());
    
      $updateGoTo = "licenties.php";
      if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
        $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
        $updateGoTo .= $HTTP_SERVER_VARS['QUERY_STRING'];
      }
        header(sprintf("Location: %s", $updateGoTo));
    }
    
    
    
    
    
    if ((isset($HTTP_POST_VARS["MM_remove"])) && ($HTTP_POST_VARS["MM_remove"] == "form2")) {
      $removeSQL = sprintf("Delete from licenties WHERE 'ID'==???????????");
    
      mysql_select_db($database_fisherintranet, $fisherintranet);
      $Result1 = mysql_query($removeSQL, $fisherintranet) or die(mysql_error());
    
      $removeGoTo = "licenties.php";
      if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
        $removeGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
        $removeGoTo .= $HTTP_SERVER_VARS['QUERY_STRING'];
      }
        header(sprintf("Location: %s", $removeGoTo));
    }
    
    
    
    
    
    session_start();
    if ($HTTP_SESSION_VARS["statusCheck"] != "Admin")
      header("Location: menu.php");
    $colname_licenties = "1";
    if (isset($HTTP_SESSION_VARS['companyCheck'])) {
      $colname_licenties = (get_magic_quotes_gpc()) ? $HTTP_SESSION_VARS['companyCheck'] : addslashes($HTTP_SESSION_VARS['companyCheck']);
    }
    mysql_select_db($database_fisherintranet, $fisherintranet);
    $query_licenties = sprintf("SELECT license FROM licenties WHERE company = '%s' ORDER BY license ASC", $colname_licenties);
    $licenties = mysql_query($query_licenties, $fisherintranet) or die(mysql_error());
    $row_licenties = mysql_fetch_assoc($licenties);
    $totalRows_licenties = mysql_num_rows($licenties);
    
    $colname_alles = "1";
    if (isset($HTTP_SESSION_VARS['companyCheck'])) {
      $colname_alles = (get_magic_quotes_gpc()) ? $HTTP_SESSION_VARS['companyCheck'] : addslashes($HTTP_SESSION_VARS['companyCheck']);
    }
    mysql_select_db($database_fisherintranet, $fisherintranet);
    $query_alles = sprintf("SELECT * FROM licenties WHERE company = '%s' ORDER BY license ASC", $colname_alles);
    $alles = mysql_query($query_alles, $fisherintranet) or die(mysql_error());
    $row_alles = mysql_fetch_assoc($alles);
    $totalRows_alles = mysql_num_rows($alles);
    ?>
    This is my code of my form :
    Code:
    <form name="form2" method="post" action="<?php echo $editFormAction; ?>">
      <table width="30%" border="1" align="center" cellpadding="0" cellspacing="0" bgcolor="#999966">
        <tr> 
          <td colspan="2" bgcolor="#999900"><div align="center"> <font color="#FFFFFF" face="Times New Roman, Times, serif"><strong>licenses</strong></font></div></td>
        </tr>
        <?php do { ?>
        <tr> 
          <td><div align="center"> 
              <p> 
                <input name="<?php echo $row_alles['ID']; ?>" type="checkbox" id="<?php echo $row_alles['ID']; ?>" value="<?php echo $row_alles['ID']; ?>">
                <font color="#FFFFFF"> </font></p>
            </div></td>
          <td><div align="center"> 
              <p><font color="#FFFFFF"><?php echo $row_licenties['license']; ?></font></p>
            </div></td>
        </tr>
        <?php } while ($row_licenties = mysql_fetch_assoc($licenties)); ?>
      </table>
      <p align="center">
        <input name="MM_remove" type="hidden" id="MM_remove" value="form1">
        <input type="submit" name="Submit" value="Remove selected license">
      </p>
    </form>
    You help me, and I'll try to help you

  2. #2
    Join Date
    Oct 2002
    Location
    Plymouth UK
    Posts
    116
    You need to change your checkbox ID from this:
    Code:
    ...type="checkbox" id="<?php echo $row_alles['ID']; ?>" value="...
    To something like this:
    Code:
    ...type="checkbox" id="somename[]" value="...
    What this does is references an array for the values, which are then passed back in the header in the normal way. The problem with your code as it stands is you have difficulty referencing the checkboxes (you have no idea beforehand what ID they will have). With the use of an array you don't need to know.

    To process the checkboxes with the delete you would need to do something like this:

    Code:
    $somename_array = $HTTP_POST_VARS["somename"];
    
    foreach($somename_array as $somename_value){
      $sql = "DELETE FROM licenties WHERE ID=$somename_value";
      ...db specific processing of $sql statement here...
    }
    This will delete all the records that you have checked. I only know the Oracle database instructions so you would need to add the mySQL code.

    Hope this is of some use.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •