Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Feb 2011
    Posts
    75
    Thanks
    9
    Thanked 2 Times in 2 Posts

    Question Delete image php and mysql by checkbox

    Hello all
    This is what I have done so far after trying to work out how to delete image by php and mysql I'm new to all this
    so forgive me for asking for much needed help
    I need to delete image from the mysql by checkbox using PHP
    This is What I come Up with So far

    Code:
    <?
    $epuid = $_GET["id"];
    $email = $_SESSION['email'];
    $groupname = hex2str($_GET["id"]); 
    $idalb = hex2str($_GET["album"]); 
    $result = mysql_query("SELECT * FROM albums WHERE ID=".$idalb);
    //$idimg = mysql_result($result,0,"ID");
    $resultimg = mysql_query("SELECT * FROM images WHERE albumID =".$idalb);
    //$text = hex2str($_GET['content']);
    $show = mysql_result($result,0,"albshow");
    $name = mysql_result($result,0,"name");
    ?>
    <form method="post" action="">
    
      <input type="hidden" name="groupname" value="<?php echo $groupname ?>" />
      <input type="hidden" name="idalbum" value="<?php echo $idalb ?>" />
      
    <p class="bing">Album Name is :
        <input type="readonly" name="albumname" size="35" value="<? echo $name  ?>" /></p><br clear=all><br clear=all>
        <div style="position:relative;left:30px;border:1px solid #D0E2EC;background-color:#E9F4FA; padding:8px;width:1000px;" class="idiv1">
    <p class="bing">Album Photos</p> <br clear=all>
    
        <div style="position:relative;left:10px;">
        <?  while($row=mysql_fetch_array($resultimg))
    	{
    		echo "<img src='".$row['img']."' width='150' height='150' style='margin:5px;'>";
    		echo "<input type=CHECKBOX name=$resultimg >";
    		}
    		if(isset($_SESSION['session_temp']))
    	{
    	$resultimg = mysql_query("DELETE * FROM images WHERE albumID =".$_SESSION['session_temp']);
    	while($row=mysql_fetch_array($resultimg))
    	{
    		echo "<img src='".$row['img']."' width='150' height='150' style='margin:5px;'>";
    		}
    	}
    	?>
    	</div>
        <br clear=all>
        <input type="submit"  name="Delete" value="Delete Seleted Images!" />
      <br clear=all></div>
      <?php
    if($delete){
    for($i=0;$i<$count;$i++){
    $del_id = $checkbox[$i];
    $sql = "DELETE FROM images WHERE id='$del_id'";
    $result = mysql_query($sql);
    }
    
    if($result){
    echo "<meta http-equiv=\"refresh\" content=\"0;ULR=\">";
    }
    }
    mysql_close();
    ?>
    
    </form>
    I still unable to delete img
    Many Thanks for Replies and Help
    WhatNot

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,079
    Thanks
    2
    Thanked 320 Times in 312 Posts
    You need to organize your code with distinct sections for the content you are outputting on the page (i.e. the album name, form...) and for the form processing logic. The form processing logic needs to come before the form code so that when the form is redisplayed it will reflect the current images.

    In pseudo code -

    Code:
    // form processing
    if(isset($_POST['Delete'])){ // detect that a (your) form was submitted
    
        // all the form processing logic goes here
    
    }
    
    
    // produce the content to output on the page, one of which is the form
    You also need to clean up your code so that it only contains the variables and logic that you need (if it is not used in the form or the form processing code, it shouldn't be in there.)

    Each checkbox needs to submit the corresponding image id. You should use an array name for the checkboxes where the array index is the image id so that you can easily delete all the checked images at one time.

    Lastly, what security do you have in place to prevent anyone from deleting any/all your images? You need to make sure that the current visitor is logged in and has sufficient permission to delete his, any, all, ... images.

    Give me a moment or two and I'll post an outline showing what this would look like.
    Last edited by CFMaBiSmAd; 05-12-2013 at 04:47 PM.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,079
    Thanks
    2
    Thanked 320 Times in 312 Posts
    And you should switch to mysqli or pdo for your database library (any example code likely to be posted to help you will not use the old mysql library.)
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,079
    Thanks
    2
    Thanked 320 Times in 312 Posts
    The following is a simplified example showing one way of doing what I think you are trying to do -

    Code:
    <?php
    // include any code here that this script relies on... (database connection)
    
    session_start();
    if(!isset($_SESSION['user_id'])){
    	header("location: index.php"); // not logged in, go elsewhere
    	exit;
    }
    
    if($_SESSION['user_level'] !== 'admin'){
    	header("location: index.php"); // not an admin, go elsewhere
    	exit;
    }
    
    // form processing
    if(isset($_POST['Delete'])){ // detect that a (your) form was submitted
    	//echo '<pre>',print_r($_POST,true),'</pre>'; // examine the submitted form data
    	if(isset($_POST['del']) && is_array($_POST['del'])){
    		// at least one checkbox was checked
    		
    		// pretend a trusted person submitted the data or add validation of the input data here...
    		
    		// enforce any ownership of images per user here...
    		
    		$ids = implode(',',array_keys($_POST['del'])); // a list of the image ids
    		$query = "DELETE FROM images WHERE id IN($ids)";
    		// echo $query; // examine the actual query statement
    		$mysqli->query($query) or die("Query failed: $query<br>Error: $mysqli->error");
    		echo "$mysqli->affected_rows image(s) were deleted.";
    	}
    }
    
    // produce the content to output on the page
    $query = "SELECT a.name, i.id, i.img FROM albums a LEFT JOIN images i ON a.id = i.albumID ORDER BY a.name,i.img"; // you need to learn joins to get one query to do what you want
    $result = $mysqli->query($query) or die("Query failed: $query<br>Error: $mysqli->error");
    if($result->num_rows == 0){
    	echo "<h3>There are no albums to display.</h3>";
    } else {
    	echo "<form method='post' action=''>";
    	$last_album = null;
    	while($row = $result->fetch_assoc()){
    		if($last_album != $row['name']){
    			// the album name changed
    			echo "<h4>{$row['name']}</h4>"; // output a new album heading
    			$last_album = $row['name']; // remember the new album name
    		}
    		if($row['img']){
    			echo "<p><img src='{$row['img']}' alt=''> {$row['img']} <input type='checkbox' name='del[{$row['id']}]'></p>\n";
    		} else {
    			echo "<p>No images in this album.</p>";
    		}
    	}
    	echo "<input type='submit' name='Delete' value='Delete the selected images'></form>";
    }
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • Users who have thanked CFMaBiSmAd for this post:

    Whatnot (05-12-2013)

  • #5
    New Coder
    Join Date
    Feb 2011
    Posts
    75
    Thanks
    9
    Thanked 2 Times in 2 Posts
    thank you will have to edit it a bit but thank you


  •  

    Posting Permissions

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