Jump to content

How to delete more than one database row?


mark103

Recommended Posts

Hi guys,

 

I need a bit of your help. I have successfully deleted a row in a database after I have inserted the value name with a var function that match with a username in the url bar. Now I would like to delete more than one row, but I don't know how to do this?

 

What I am trying to achieve is to enter the url something like this:

 

www.mysite.com/delete.php?favorites=the value1&use=test

 

 

It will search for a value in the database and then delete it. But if I add the &favorites=the value2 or more than 2, then search for the values in each row while looking for a username before delete the rows

 

Here's the current code:

 

<?php
session_start();
    define('DB_HOST', 'localhost');
    define('DB_USER', 'mydbuser');
    define('DB_PASSWORD', 'mydbpass');
    define('DB_DATABASE', 'mydbname');
       
    $errmsg_arr = array();
    $errflag = false;

    $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
    if(!$link) {
  die('Failed to connect to server: ' . mysql_error());
    }

    $db = mysql_select_db(DB_DATABASE);
    if(!$db) {

die("Unable to select database");
    }

   function clean($var){

return mysql_real_escape_string(strip_tags($var));
    }
    $favorites = clean($_GET['favorites']);
    $username = clean($_GET['user']);


if($favorites == '' && $username == ''){
   // both are empty
   $errmsg_arr[] = 'Both name and email are missing. You must enter one or the other.';
   $errflag = true;
}

    if($errflag) {
  $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
  echo implode('<br />',$errmsg_arr);
   }
   else {
$insert = array();
if(isset($_GET['favorites'])) {
    $insert[] = 'favorites = \'' . clean($_GET['favorites']) . '\'';
}


if(($favorites) && isset($username)) {
   mysql_query("DELETE FROM favorites WHERE username='$username' AND favorites='$favorites'");
   $deleted = mysql_affected_rows();
   if($deleted > 0) 
   {
     echo "favorites rows is deleted";
   } 
   else 
   {
     echo("favorites are already deleted");
   }
}
}
?>

 

 

Any advice would be much appreciated.

 

Thanks,

Mark

you would need to make each favorite have a unique name or they will overwrite, and I dont think you can pass an array as a querystring.

so it would have to be favorite1=value&favorite2=value

you can post arrays definately so maybe a post instead of a get?

 

then at the posted script loop through the array do delete the items

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.