Jump to content

Delete album and items related to the album HELP


Lisa23

Recommended Posts

i do think is a small mistake i making can u please have a look if u can

ok here's the problem i am trying to delete an album within the album should also delete the photos related to that album this what i tried gives this error

Delete image failed. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3

 

my tables are

table albums fields album_id, album_name, album_owner, sub_album

table photos fields, photo_id, photo_name, photo_extension, photo_proper photo_owner, photo_date, photo_comments, photo_size, album_id

 

photo_proper is the name image stored in folder

 

 

<?phpdefine('ROOT_DIR', './');define('PROPER', TRUE);/*** include common files*/include_once(ROOT_DIR. 'includes/common.inc.php');// No album id has been selectedif (isset($_GET['albums']))    // get the album name since we need to display   // a message that album 'foo' is deleted   $result = mysql_query("SELECT album_id, album_name, album_owner, sub_album                          FROM albums                          WHERE album_id = $album_id")             or die('Delete image failed. ' . mysql_error());   if (mysql_num_rows($result) == 1) {      $row        = mysql_fetch_assoc($result);      $album_id  = $row['album_id'];      $album_name = $row['album_name'];      // get the image filenames first so we can delete them      // from the server      $result = mysql_query("SELECT photo_id, photo_id                             FROM photos                             WHERE album_id = $album_id")                or die(mysql_error());      while ($row = mysql_fetch_assoc($result)) {          define("GALLERY_IMG_DIR", "./photos/");            unlink(GALLERY_IMG_DIR . $row['photo_proper']);      unlink(GALLERY_IMG_DIR . 'thumbs/' . $row['photo_proper']);      }      $result = mysql_query("DELETE FROM photos                             WHERE album_id = $album_id")                or die('Delete image failed. ' . mysql_error());      $result = mysql_query("DELETE FROM album                             WHERE album_id = $album_id")                or die('Delete album failed. ' . mysql_error());      // album deleted successfully, let the user know about it      echo "<p align=center>Album '$album_name' deleted.</p>";   } else {      echo "<p align=center>Cannot delete a non-existent album.</p>";   }?>

 

 

try 2 error line 5

 

<?phpdefine('ROOT_DIR', './');define('PROPER', TRUE);/*** include common files*/include_once(ROOT_DIR. 'includes/common.inc.php');// No album id has been selectedif (isset($_GET['albums'])) {   // get the image file name so we   // can delete it from the server   $sql = "SELECT album_id, album_name, album_owner, sub_album           FROM albums           WHERE album_id = {$_GET['albums']}";   $result = mysql_query($sql)             or die('Delete photo failed. ' . mysql_error());   if (mysql_num_rows($result) == 1) {      $row = mysql_fetch_assoc($result);      // get the image filenames first so we can delete them      // from the server     $sql = "SELECT photo_id, photo_proper           FROM photos           WHERE photo_id = {$_GET['photos']}";   $result = mysql_query($sql)             or die('Delete photo failed. ' . mysql_error());   if (mysql_num_rows($result) == 1) {      $row = mysql_fetch_assoc($result);      define("GALLERY_IMG_DIR", "./photos/");      // remove the image and the thumbnail from the server      unlink(GALLERY_IMG_DIR . $row['photo_proper']);      unlink(GALLERY_IMG_DIR . 'thumbs/' . $row['photo_proper']);            // and then remove the database entry      $sql = "DELETE FROM photos              WHERE photo_id = {$_GET['photos']}";      $result = mysql_query("DELETE FROM album                             WHERE album_id = $album_id")                or die('Delete album failed. ' . mysql_error());      // album deleted successfully, let the user know about it      echo "<p align=center>Album '$album_name' deleted.</p>";   } else {      echo "<p align=center>Cannot delete a non-existent album.</p>";   }}}?>

 

 

Link to comment
Share on other sites

Hi there,

 

Which one are you currently using? 1 or two, if we know which version you are using we can suggest alternative ways or help you understand the issue a little better.

 

Also, it would be most advantageous if you were to use error_reporting(E_ALL); at the top of all your php files, then you would have had the two missing colon's flagged up from the first example.

 

Rw

Link to comment
Share on other sites

Lisa... are you using MyIsam or Innodb storage engine for those tables?

 

If you are using  Innodb (the default is MyIsam) is not need to implement the code to do cascade deletes because those can be implemented directly in the table(s) definition using FOREIGN KEY and  ON DELETE CASCADE constrains.

 

In that way you code should only delete the record(s) in the parent table (and control for possible errors).  Maybe this is something that you should look at if you want to learn other alternative.

Link to comment
Share on other sites

not quite sure what all that u said means but i have this one which i use to delete photos and remove photos from directory wrks fine so i just thought i had sum other things for the album this the delete photos

 

<?php
define('ROOT_DIR', './');
define('PROPER', TRUE);
/**
* include common files
*/
include_once(ROOT_DIR. 'includes/common.inc.php');
// No album id has been selected
if (isset($_GET['photos'])) 
{
   // get the image file name so we
   // can delete it from the server
   $sql = "SELECT photo_id, photo_proper

           FROM photos

           WHERE photo_id = {$_GET['photos']}";

   $result = mysql_query($sql)

             or die('Delete photo failed. ' . mysql_error());

   if (mysql_num_rows($result) == 1) {

      $row = mysql_fetch_assoc($result);
      
define("GALLERY_IMG_DIR", "./photos/");

      // remove the image and the thumbnail from the server
      unlink(GALLERY_IMG_DIR . $row['photo_proper']);

      unlink(GALLERY_IMG_DIR . 'thumbs/' . $row['photo_proper']);
      
      // and then remove the database entry

      $sql = "DELETE FROM photos

              WHERE photo_id = {$_GET['photos']}";
echo '<p>Delete sucessfull click <a href="photos.php">here</a> to return to main menu</p>';

      mysql_query($sql)

      or die('deletd photo failed. ' . mysql_error());

   }

}
  else{
   
}


?>

so i modify for the album but nope nt wrking

Link to comment
Share on other sites

perhaps...

 

/*
This presumes - 
$_GET['albums'] contains the valid id of an album
photo_proper contains the full image name and path
*/

$what_album = $_GET['albums'];
/* loop thru photos unlinking each photo listed as in the target album */
$query0 = "SELECT * FROM photos WHERE album_id = $what_album";
$result0 = mysql_query($query0);
WHILE($row = mysql_fetch_array($result0)) {
 $what_image = $row['photo_proper'];
 unlink $what_image;
}
$query1 = "DELETE FROM photos WHERE album_id='$what_album'];
$result1 = mysql_query($query1);
$query = "DELETE FROM albums WHERE album_id='$what_album'];
$result2 = mysql_query($query2);

Link to comment
Share on other sites

not quite sure what all that u said means    :(

ok... never mind.

 

try this code or starting from litebearer posted adjust yours

<?php

   error_reporting(E_ALL); 
   ini_set("display_errors", 1); 

   define('ROOT_DIR', './');
   define('PROPER', TRUE);
   define("GALLERY_IMG_DIR", "./photos/");

/**
* include common files
*/
include_once(ROOT_DIR. 'includes/common.inc.php');


// No album id has been selected
if (isset($_GET['albums'])) 
{
   // get the image file name so we
   // can delete it from the server
   $sql = "SELECT album_id, album_name, album_owner, sub_album
            FROM albums
            WHERE album_id = {$_GET['albums']}";

   $result = mysql_query($sql) or die('Album Selection failed. ' . mysql_error());

   if (mysql_num_rows($result) == 1) {

      $row = mysql_fetch_assoc($result);

      // get the image filenames first so we can delete them
      // from the server
      $psql = "SELECT photo_id, photo_proper
              FROM photos
              WHERE album_id = {$row['album_id']}";

      $presult = mysql_query($psql) or die('Photo Selection failed. ' . mysql_error());

      while ($prow = mysql_fetch_assoc($presult)) {
         // remove the image and the thumbnail from the server
         unlink(GALLERY_IMG_DIR . $prow['photo_proper']);
         unlink(GALLERY_IMG_DIR . 'thumbs/' . $prow['photo_proper']);
      } 
           
      // and Now we remove the database entries 

      $sql = "DELETE FROM photos
               WHERE album_id = {$row['album_id']}";
      mysql_query($sql) or die('Delete of Album associated photos failed. ' . mysql_error());               

      $sql =  "DELETE FROM album
                    WHERE album_id = {$row['album_id']}";              
      mysql_query($sql) or die('Delete album failed. ' . mysql_error());

      // album deleted successfully, let the user know about it
      echo "<p align=center>Album $row['album_name'] deleted.</p>";
   } else {
      echo "<p align=center>Cannot delete a non-existent album.</p>";
   }

}

?>

Link to comment
Share on other sites

i tried urs Offline mikosiko  Enthusiast

result

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\xampp\htdocs\rookierods\deletealbum.php on line 56

 

this line

      echo "<p align=center>Album $row['album_name'] deleted.</p>";

 

Link to comment
Share on other sites

perhaps...

 

/*
This presumes - 
$_GET['albums'] contains the valid id of an album
photo_proper contains the full image name and path
*/

$what_album = $_GET['albums'];
/* loop thru photos unlinking each photo listed as in the target album */
$query0 = "SELECT * FROM photos WHERE album_id = $what_album";
$result0 = mysql_query($query0);
WHILE($row = mysql_fetch_array($result0)) {
 $what_image = $row['photo_proper'];
 unlink $what_image;
}
$query1 = "DELETE FROM photos WHERE album_id='$what_album'];
$result1 = mysql_query($query1);
$query = "DELETE FROM albums WHERE album_id='$what_album'];
$result2 = mysql_query($query2);

 

i tried urs litebearer

error

 

Parse error: syntax error, unexpected T_VARIABLE in C:\xampp\htdocs\rookierods\deletealbum.php on line 40

this how i put it

<?php

   error_reporting(E_ALL); 
   ini_set("display_errors", 1); 

   define('ROOT_DIR', './');
   define('PROPER', TRUE);
   define("GALLERY_IMG_DIR", "./photos/");

/**
* include common files
*/
include_once(ROOT_DIR. 'includes/common.inc.php');


/*
This presumes - 

    

$_GET['albums'] contains the valid id of an album

    

photo_proper contains the full image name and path
*/

$what_album = $_GET['albums'];
/* loop thru photos unlinking each photo listed as in the target album */
$query0 = "SELECT * FROM photos WHERE album_id = $what_album";
$result0 = mysql_query($query0);
WHILE($row = mysql_fetch_array($result0)) {

    

$what_image = $row['photo_proper'];

    

unlink $what_image;
}
$query1 = "DELETE FROM photos WHERE album_id='$what_album'];
$result1 = mysql_query($query1);
$query = "DELETE FROM albums WHERE album_id='$what_album'];
$result2 = mysql_query($query2);

?>

Link to comment
Share on other sites

Lisa... you have to stick with one version and try to find the problem...posting 2 codes don't help to the people trying to help you... so...

change this line

 

   

  echo "<p align=center>Album $row['album_name'] deleted.</p>";

 

     

echo "<p align=center>Album {$row['album_name']} deleted.</p>";

 

if that doesn't fix the problem look for unmatched { or ) or missing ;

 

Link to comment
Share on other sites

Lisa... you have to stick with one version and try to find the problem...posting 2 codes don't help to the people trying to help you... so...

change this line

 

   

  echo "<p align=center>Album $row['album_name'] deleted.</p>";

 

     

echo "<p align=center>Album {$row['album_name']} deleted.</p>";

 

if that doesn't fix the problem look for unmatched { or ) or missing ;

 

sorry about that  ok i changed that echo line but now

it deletes evrything but gives this notice

 

Notice: Use of undefined constant THIS_SCRIPT - assumed 'THIS_SCRIPT' in C:\xampp\htdocs\rookierods\includes\common.inc.php on line 154

which on the common .inc.php is this line

if (in_array(THIS_SCRIPT, $access_pages))

the whole part is like this

 

if (in_array(THIS_SCRIPT, $access_pages))
{
    if ($mode == "new" || THIS_SCRIPT == 'newsletter' || THIS_SCRIPT == 'admin')
    {
        // start a session
        session_start();

        if (!isset($_SESSION['logged_in']))
        {
            header("location:" .ROOT_DIR. "login.php");
            exit();
        }
    }
}

 

Link to comment
Share on other sites

so the code that we were working on is working fine ... good

 

this is a different problem.. as we don't know what do you have in your common.inc.php we can only guess what the problem could be...

 

try changing THIS_SCRIPT for $THIS_SCRIPT in all the places.

 

otherwise, post your common.inc.php code

 

Link to comment
Share on other sites

so the code that we were working on is working fine ... good

 

this is a different problem.. as we don't know what do you have in your common.inc.php we can only guess what the problem could be...

 

try changing THIS_SCRIPT for $THIS_SCRIPT in all the places.

 

otherwise, post your common.inc.php code

 

 

issue on this line

if (in_array(THIS_SCRIPT, $access_pages))

this is the comon.inc.php

<?php
/**
* check for hacking attempt
*/
if (!defined("PROPER"))
{
    die("hacking attempt");
}

/**
* deal with backwards compatibility
*/
if (!isset($_SERVER))
{
    $_GET = &$HTTP_GET_VARS;
    $_POST = &$HTTP_POST_VARS;
    $_SERVER = &$HTTP_SERVER_VARS;
    $_COOKIE = &$HTTP_COOKIE_VARS;
    $_SESSION = &$HTTP_SESSION_VARS;
}

/**
* what globals are allowed
*/
$allowed_globals = array(

    'GLOBALS',
    '_POST',
    '_GET',
    '_FILES',
    '_REQUEST',
    '_SERVER',
    '_COOKIE',
    '_SESSION',
    'allowed_globals'

);

/**
* check any globals that are passed
* and unset if not allowed
*/
foreach ($GLOBALS as $key => $value)
{
    if (!in_array($key, $allowed_globals) && $key != 'key' && $key != 'value')
    {
        unset($GLOBALS[$key]);
    }
}

/**
* deal with magic quotes (stripslashes)
*/
if (get_magic_quotes_gpc())
{
    function strip_quotes (&$vars) 
    { 
        if (is_array($vars)) 
        {
            foreach ($vars AS $key => $value) 
            { 
                if (is_string($value)) 
                { 
                    $vars[$key] = stripslashes($value); 
                } 
                elseif (is_array($value))
                { 
                    $vars[$key] = strip_quotes($value); 
                } 
            } 
        } 

        return $vars; 
    } 

    $_GET = strip_quotes($_GET);
    $_POST = strip_quotes($_POST);
    $_COOKIE = strip_quotes($_COOKIE);
}

include_once(ROOT_DIR. "classes/template.class.php");
include_once(ROOT_DIR. "classes/error.class.php");
include_once(ROOT_DIR. "classes/database.class.php");
include_once(ROOT_DIR. "classes/sessions.class.php");
include_once(ROOT_DIR. "includes/constants.inc.php");

/**
* setup the database connection
*/
$db_name = 'eurico_edy';
$db_pass = '';
$db_user = 'root';
$db_host = 'localhost';

if (!$db = new db_handler($db_user, $db_pass, $db_name, $db_host))
{
    die("COULD NOT CONNECT TO DATABASE");
}

/**
* setup style
*/
if (!$template = new template(ROOT_DIR))
{
    die("COULD NOT INITIALISE TEMPLATE ENGINE");
}

/**
* begin error handling
*/
if (!$errors = new error_handler())
{
    die("COULD NOT INITIALISE ERROR HANDLER");
}

$sessions = new session_handler();

/**
* get the mode
*/
if (isset($_REQUEST['mode']))
{
    $mode = trim($_REQUEST['mode']);
}
else
{
    $mode = '';
}

/**
* what page are we on?
*/
if (isset($_REQUEST['page']))
{
    $page = intval($_GET['page']) - 1;
}
else
{
    $page = 0;
}

/**
* for certain pages we need to check admin access
*/
$access_pages = array(

    'drivers',
    'news',
    'newsletter',
    'admin'

);

if (in_array(THIS_SCRIPT, $access_pages))
{
    if ($mode == "new" || THIS_SCRIPT == 'newsletter' || THIS_SCRIPT == 'admin')
    {
        // start a session
        session_start();

        if (!isset($_SESSION['logged_in']))
        {
            header("location:" .ROOT_DIR. "login.php");
            exit();
        }
    }
}
?>

Link to comment
Share on other sites

nope not in the constant thsi what i gt in the constant file

 

<?php

$table_prefix = '';

/**
* define table names
*/
define('ALBUMS_TABLE', $table_prefix. 'albums');
define('COMMENTS_TABLE', $table_prefix. 'comments');
define('PHOTOS_TABLE', $table_prefix. 'photos');
define('SESSIONS_TABLE', $table_prefix. 'sessions');
define('USERS_TABLE', $table_prefix. 'users');

?>

Link to comment
Share on other sites

nope not in the constant thsi what i gt in the constant file

 

 

that should be there... but hard to tell you which value it should have because we don't know the whole system.

 

either you can define it with any value and test what happens or analyze the code completely and try to understand how it work.. from that understanding you should be able ti figure out what value that constant should have... by now your original script is working and the message that you are getting is just a NOTICE.

 

 

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.