Delete Multiple Records

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
igoy
Forum Contributor
Posts: 203
Joined: Fri May 02, 2003 11:57 pm
Location: India
Contact:

Delete Multiple Records

Post by igoy »

I have a mysql table where there is no primary key, nothing like 'ID' or so. . I want to delete multiple records in this table using checkboxes ..
can it be possible....???
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

If you have nothing to uniquely identify a record then it's going to be a bit of a PITA. Create some unique ID for each record and you'll make your life much easier.

Mac
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

you can add primary id's after the fact, I would suggest doing that
every table except maybe many to many should have a unique id
User avatar
igoy
Forum Contributor
Posts: 203
Joined: Fri May 02, 2003 11:57 pm
Location: India
Contact:

Hmmm

Post by igoy »

Well. . you can't help going by rules so i created unique ID column in the table. . . i did infact everything ele required .. .now. . still it's not deleting multiple records . . i'm using chekboxes to select the records . .and submitting to other php called delete.php .. .but it's not deleting multiple records .. it deletes the last one in selected records.

any suggestion . .??
User avatar
Heavy
Forum Contributor
Posts: 478
Joined: Sun Sep 22, 2002 7:36 am
Location: Viksjöfors, Hälsingland, Sweden
Contact:

Post by Heavy »

Your question involves to many unknown factors to answer. Tell us some more.

How does the record selection form look like (HTML with checkboxes)?
What distinguishes one record from another among your checkboxes?
How is the database table designed, what things are PRIMARY or UNIQUE?
How do you build the query to delete the records?
Any error messages?
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

I think you're looking for arrays

your checkbox field should be name=delete[]
that sets the check boxes up as an array, then when you process the form do a foreach on that field and loop through and delete the records
User avatar
igoy
Forum Contributor
Posts: 203
Joined: Fri May 02, 2003 11:57 pm
Location: India
Contact:

Still Lost !! :(

Post by igoy »

Hi Jplush,

Sorry for replying late.. I was out of town..
Back and still lost about multiple delete problem... :( ..sad

I tried creating array as you said but in vain ..
not ur fault. . it's my dumbness for not udnerstanding foreach() stuff..

basically I'm using Macromedia Dreamweaver MX for site creation and do some coding myself (as and when required)...

Posting below is Code Dreamweaver generated for Deleting Record..
( It can delete one record at a time ) ..

Code: Select all

<? 
$table = $_GET['page'];
$goto = $page.".php";
?>
<?php require_once('../Connections/connpv.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;
}

if ((isset($HTTP_POST_VARS['delbox'])) && ($HTTP_POST_VARS['delbox'] != "")) {
$deleteSQL = sprintf("DELETE FROM $table WHERE rid=%s",
GetSQLValueString($HTTP_POST_VARS['delbox'], "int"));

  mysql_select_db($database_connpv, $connpv);
  $Result1 = mysql_query($deleteSQL, $connpv) or die(mysql_error());
}

?>
I hope i'm not expecting too much. . but you gyus are great hope for us amatuers ...

Thanks Again.
Post Reply