Jump to content

Cannot delete the value in a database


mark103

Recommended Posts

Hi guys,

 

I am recent working on my php to delete the information in a database. When I insert the value in the url bar something is like: www.mysite.com/delete.php?myfavorites=what ever it is&user=test. When I inserted the value in the url bar, it did not search for the value in a database to delete it while match with the username in the url bar.

 

 

<?php
session_start();
    define('DB_HOST', 'localhost');
    define('DB_USER', 'mydbuser');
    define('DB_PASSWORD', 'mydbpass');
    define('DB_DATABASE', 'mydbname');


$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[] = 'Favorites are missing.';
   $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(isset($_GET['username'])) {
    $insert[] = 'username = \'' . clean($_GET['username']) . '\'';
}


if (count($insert)>0) {
   $names = implode(',',$insert);


if($favorites && $username) {

mysql_query("DELETE valuedata FROM favorites WHERE username='$username'");
$deleted = mysql_affected_rows();
if($deleted > 0) {
    echo("The value in favorites are deleted");
    } else {
        echo("failed");
    }
    }else{
   echo("failed");
   }
}
}
mysql_close($link);
?>

 

 

If you know how to delete the value in a database for each row while it match with the username in the url, please say so as i would like to know how to do it.

 

Any advise would be much appreciated.

 

Thanks,

Mark

Link to comment
Share on other sites

It seems that you have a problem in a logic... Have a look. I don't change your code. I just change a style of you code

<?php 
session_start();
    define('DB_HOST', 'localhost');
    define('DB_USER', 'mydbuser');
    define('DB_PASSWORD', 'mydbpass');
    define('DB_DATABASE', 'mydbname');


$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[] = 'Favorites are missing.';
    $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(isset($_GET['username'])) 
      {
         $insert[] = 'username = \'' . clean($_GET['username']) . '\'';
      }

      if (count($insert)>0) {
      $names = implode(',',$insert);

      if($favorites && $username) 
      {

        mysql_query("DELETE valuedata FROM favorites WHERE username='$username'");
        $deleted = mysql_affected_rows();
        if($deleted > 0) 
        {
          echo("The value in favorites are deleted");
        } 
        else 
        {
          echo("failed");
        }
      }
      else
      {
        echo("failed");
      }
    }
} 
mysql_close($link);
?>

 

You put brackets in a very random positions. I put them in correct places. I mean 'correct places' == 'correct distance from the line beginning'. Now it's clear that if this condition ($favorites == '' && $username == '') is not true you don't have a code to process it!!!

 

Also it looks funny :) :

    $errflag = true;

    if($errflag) 
    {....

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.