SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Member
    Join Date
    Dec 2012
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Confirm Delete solution - chapter 7 - PHP and MySQL Novice to Ninja

    Dear Kevin,

    Could you post the solution for the "Confirm on Delete" challenge, so I can what I have done is correct.

    Thanks,

    Regards,

    Ronnie

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Hi Ronnie 2013,

    Welcome to the forums.

    There isn't a hard and fast solution to the exercise, so why don't you post what you have coded and ask for feedback.

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2009
    Location
    Port Harcourt
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ronnie 2013, you can use a simple javascript to achieve what you want.
    Use this: <input type="submit" name="action" value="Delete" onclick="return confirm('Delete This User?')" />

  4. #4
    SitePoint Member
    Join Date
    Jun 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, since nobody answered, here is my attempt - which has failed. It says that "$author['id']" on the third line is undefined.


    PHP Code:
    //to confirm and delete an author from the joke databank
    if (isset($_POST['action']) and $_POST['action'] == 'Delete')
    {
        
    $authordeleteid $author['id'];
        include 
    'confirmdeleteauthor.html.php';
        exit();
    }

    if (isset(
    $_POST['connfirmdeleteauthor']))
    {
      include 
    $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';

      
    // Get jokes belonging to author
      
    try
      {
        
    $sql 'SELECT id FROM joke WHERE authorid = :id';
        
    $s $pdo->prepare($sql);
        
    $s->bindValue(':id'$_POST['id']);
        
    $s->execute();
      }
      catch (
    PDOException $e)
      {
        
    $error 'Error getting list of jokes to delete.';
        include 
    'error.html.php';
        exit();
      }

      
    $result $s->fetchAll();

      
    // Delete joke category entries
      
    try
      {
        
    $sql 'DELETE FROM jokecategory WHERE jokeid = :id';
        
    $s $pdo->prepare($sql);

        
    // For each joke
        
    foreach ($result as $row)
        {
          
    $jokeId $row['id'];
          
    $s->bindValue(':id'$jokeId);
          
    $s->execute();
        }
      }
      catch (
    PDOException $e)
      {
        
    $error 'Error deleting category entries for joke.';
        include 
    'error.html.php';
        exit();
      }

      
    // Delete jokes belonging to author
      
    try
      {
        
    $sql 'DELETE FROM joke WHERE authorid = :id';
        
    $s $pdo->prepare($sql);
        
    $s->bindValue(':id'$_POST['id']);
        
    $s->execute();
      }
      catch (
    PDOException $e)
      {
        
    $error 'Error deleting jokes for author.';
        include 
    'error.html.php';
        exit();
      }

      
    // Delete the author
      
    try
      {
        
    $sql 'DELETE FROM author WHERE id = :id';
        
    $s $pdo->prepare($sql);
        
    $s->bindValue(':id'$_POST['id']);
        
    $s->execute();
      }
      catch (
    PDOException $e)
      {
        
    $error 'Error deleting author.';
        include 
    'error.html.php';
        exit();
      }

      
    header('Location: .');
      exit();

    Last edited by cpradio; Jun 27, 2013 at 09:14. Reason: Added php tags

  5. #5
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,136
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    I think you'll want to use $_POST['id'] where you have $author['id'].

  6. #6
    SitePoint Member
    Join Date
    Jun 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, that did it! Seems to be other problems with my solution but I'm working my way through it.

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,136
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Great. As you get stuck, don't hesitate to ask us for help. We're more than willing

  8. #8
    SitePoint Member
    Join Date
    Jul 2014
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi everyone, I'm having the same problem completing this task or i think it is. i get an error of
    Notice: Undefined index: id in C:\xampp\htdocs\php_sandbox\chapter7\admin\authors\index.php on line 22
    Any pointers would be great and deeply appreciated. This is what i've done so far:
    PHP Code:
    <?php
    //Display author list
    include $_SERVER['DOCUMENT_ROOT'].'/includes/db.inc.php';
    try
    {
        
    $result $pdo->query('SELECT id, name FROM author');
    }
    catch(
    PDOException $e)
    {
        
    $error 'Error fetching authors from the database!';
        include 
    'error.html.php';
        exit();
    }

    foreach(
    $result as $row)
    {
        
    $authors[] = array('id'=> $row['id'], 'name'=> $row['name']);    
    }

    if( isset(
    $_POST['action']) and $_POST['action'] == 'Delete'){
        
        
    $authorId $authors['id'];
        include 
    'confirmdelete.html.php';
        
        if( 
    $_POST['action'] == 'no' ){
            
    header('Location:authors.html.php');
            exit();
        }else{
            if(isset(
    $_POST['action']) and $_POST['action'] == 'Delete')
            {
                include 
    $_SERVER['DOCUMENT_ROOT'].'/includes/db.inc.php';
                
                try
                {
                    
    $sql 'SELECT id from joke where authorId = :id';
                    
    $s $pdo-> prepare($sql);
                    
    $s -> bindValue(':id'$_POST['id']);
                    
    $s -> execute();
                }catch(
    PDOException $e){
                    
    $error 'Error getting list of jokes to delete.'$e->getMessage();
                    include 
    'error.html.php';
                    exit();
                }
                
                
    $result $s->fetchAll();
                
    //delete joke category entries
                
    try
                {
                    
    $sql='DELETE FROM jokecategory WHERE id=:id';
                    
    $s$pdo-> prepare($sql);
                    
    //foreach joke category entries
                    
    foreach( $result as $row){
                        
    $jokeId $row['id'];
                        
    $s-> bindValue(':id'$jokeId);
                        
    $s -> execute();
                    }
                }
                catch( 
    PDOException $e )
                {
                    
    $error 'Error deleting category entries for joke';
                    include 
    'error.html.php';
                    exit();
                }
                
    //delete jokes belonging to author
                
    try
                {
                    
    $sql='DELETE FROM joke where authorId=:id';
                    
    $s $pdo-> prepare($sql);
                    
    $s -> bindValue(':id'$_POST['id']);
                    
    $s-> execute();
                }
                catch(
    PDOException $e)
                {
                    
    $error 'Error delete jokes from author';
                    include 
    'error.html.php';
                    exit();
                }
                
                
    //delete the author
                
    try
                {
                    
    $sql 'DELETE FROM author WHERE id = :id';
                    
    $s $pdo-> prepare($sql);
                    
    $s-> bindValue(':id'$_POST['id']);
                }
                catch(
    PDOException $e)
                {
                    
    $error 'Error deleting author.';
                    include 
    'error.html.php';
                    exit();
                }
                
    header('Location:.');
                exit();
            }

        }
    }

    include 
    'authors.html.php';
    ?>
    and then in my 'confirmdelete.html.php i have the following :
    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Confirm Delete Page</title>
    </head>

    <body>
    <form action="" method='post'>
        <h1>Are you Sure you want to delete this author?</h1>
        <input type='submit' name='action' value='Yes'/>
        <input type="submit" name="action" value="No" />
        <input type="hidden" name="id" value="<?php $authorId?>"    
    </form>
    </body>
    </html>

  9. #9
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,136
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure if this is it, but your hidden input tag is incomplete.

    You have:
    HTML Code:
    <input type="hidden" name="id" value="<?php $authorId; ?>"
    It should be: (also you are missing the term echo in front of $authorId)
    HTML Code:
    <input type="hidden" name="id" value="<?php echo $authorId; ?>" />

  10. #10
    SitePoint Member
    Join Date
    Jul 2014
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy

    thanks for spotting that but unfortunately that did not solve it. i've tried setting the
    Code:
    $authorid = $_POST['id']
    but thishasn't worked either.

  11. #11
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,136
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Okay, let's start from step one. So you have a list of authors on a page, you click Delete and get the confirmation page. Good so far?

    1) Can you provide the output HTML for the confirmation page?
    2) Did the input hidden field for 'id' get populated? (It isn't empty)

    Lastly, another bug in your code (I think). Find my comment in the below code
    PHP Code:
    if( isset($_POST['action']) and $_POST['action'] == 'Delete'){
        
        
    $authorId $authors['id'];
        include 
    'confirmdelete.html.php';
        
        if( 
    $_POST['action'] == 'no' ){
            
    header('Location:authors.html.php');
            exit();
        }else{
            if(isset(
    $_POST['action']) and $_POST['action'] == 'Yes'// cpradio - Since this is confirming the Delete, and the values for action are Yes or No, this needs to be Yes
            
    {
                include 
    $_SERVER['DOCUMENT_ROOT'].'/includes/db.inc.php';
                
                try
                {
                    
    $sql 'SELECT id from joke where authorId = :id';
                    
    $s $pdo-> prepare($sql);
                    
    $s -> bindValue(':id'$_POST['id']);
                    
    $s -> execute();
                }catch(
    PDOException $e){
                    
    $error 'Error getting list of jokes to delete.'$e->getMessage();
                    include 
    'error.html.php';
                    exit();
                }
                
                
    $result $s->fetchAll();
                
    //delete joke category entries
                
    try
                {
                    
    $sql='DELETE FROM jokecategory WHERE id=:id';
                    
    $s$pdo-> prepare($sql);
                    
    //foreach joke category entries
                    
    foreach( $result as $row){
                        
    $jokeId $row['id'];
                        
    $s-> bindValue(':id'$jokeId);
                        
    $s -> execute();
                    }
                }
                catch( 
    PDOException $e )
                {
                    
    $error 'Error deleting category entries for joke';
                    include 
    'error.html.php';
                    exit();
                }
                
    //delete jokes belonging to author
                
    try
                {
                    
    $sql='DELETE FROM joke where authorId=:id';
                    
    $s $pdo-> prepare($sql);
                    
    $s -> bindValue(':id'$_POST['id']);
                    
    $s-> execute();
                }
                catch(
    PDOException $e)
                {
                    
    $error 'Error delete jokes from author';
                    include 
    'error.html.php';
                    exit();
                }
                
                
    //delete the author
                
    try
                {
                    
    $sql 'DELETE FROM author WHERE id = :id';
                    
    $s $pdo-> prepare($sql);
                    
    $s-> bindValue(':id'$_POST['id']);
                }
                catch(
    PDOException $e)
                {
                    
    $error 'Error deleting author.';
                    include 
    'error.html.php';
                    exit();
                }
                
    header('Location:.');
                exit();
            }

        }


  12. #12
    SitePoint Member
    Join Date
    Jul 2014
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To answer your first quesiton yes and no, in the sense that it does not go to a new page but pastes the confirmation page at the top of the index.php page. I hope that is correct?
    This is the html for the confirmation page and the revised code in relation to the changes u suggested in the previous post is directly below.
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Confirm Delete Page</title>
    </head>
    
    <body>
    <form action="" method='post'>
    	<h1>Are you Sure you want to delete this author?</h1>
        <input type='submit' name='action' value='Yes'/>// cpradio - Since this is confirming the Delete, and the values for action are Yes or No, this needs to be Yes
        <input type="submit" name="action" value="No" />
        <input type="hidden" name="id" value="<?php $authorId; ?>"/> 
    </form>
    </body>
    </html>
    Code:
    <?php
    //Display author list
    include $_SERVER['DOCUMENT_ROOT'].'/includes/db.inc.php';
    try
    {
    	$result = $pdo->query('SELECT id, name FROM author');
    }
    catch(PDOException $e)
    {
    	$error = 'Error fetching authors from the database!';
    	include 'error.html.php';
    	exit();
    }
    
    foreach($result as $row)
    {
    	$authors[] = array('id'=> $row['id'], 'name'=> $row['name']);	
    }
    
    if( isset($_POST['action']) and $_POST['action'] == 'Delete'){
    	
    	$authorId = $_POST['id'];
    	include 'confirmdelete.html.php';
    	
    	if( $_POST['action'] == 'No' ){
    		header('Location:authors.html.php');
    		exit();
    	}else{
    		if(isset($_POST['action']) and $_POST['action'] == 'Yes')
    		{
    			include $_SERVER['DOCUMENT_ROOT'].'/includes/db.inc.php';
    			
    			try
    			{
    				$sql = 'SELECT id from joke where authorId = :id';
    				$s = $pdo-> prepare($sql);
    				$s -> bindValue(':id', $_POST['id']);
    				$s -> execute();
    			}catch(PDOException $e){
    				$error = 'Error getting list of jokes to delete.'. $e->getMessage();
    				include 'error.html.php';
    				exit();
    			}
    			
    			$result = $s->fetchAll();
    			//delete joke category entries
    			try
    			{
    				$sql='DELETE FROM jokecategory WHERE id=:id';
    				$s= $pdo-> prepare($sql);
    				//foreach joke category entries
    				foreach( $result as $row){
    					$jokeId = $row['id'];
    					$s-> bindValue(':id', $jokeId);
    					$s -> execute();
    				}
    			}
    			catch( PDOException $e )
    			{
    				$error = 'Error deleting category entries for joke';
    				include 'error.html.php';
    				exit();
    			}
    			//delete jokes belonging to author
    			try
    			{
    				$sql='DELETE FROM joke where authorId=:id';
    				$s = $pdo-> prepare($sql);
    				$s -> bindValue(':id', $_POST['id']);
    				$s-> execute();
    			}
    			catch(PDOException $e)
    			{
    				$error = 'Error delete jokes from author';
    				include 'error.html.php';
    				exit();
    			}
    			
    			//delete the author
    			try
    			{
    				$sql = 'DELETE FROM author WHERE id = :id';
    				$s = $pdo-> prepare($sql);
    				$s-> bindValue(':id', $_POST['id']);
    			}
    			catch(PDOException $e)
    			{
    				$error = 'Error deleting author.';
    				include 'error.html.php';
    				exit();
    			}
    			header('Location:.');
    			exit();
    		}
    
    	}
    }
    
    include 'authors.html.php';
    ?>

  13. #13
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,136
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    In your confirmation file, you still have
    Code:
    <input type="hidden" name="id" value="<?php $authorId; ?>"/>
    When you meant to have (notice the adding of echo)
    Code:
    <input type="hidden" name="id" value="<?php echo $authorId; ?>"/>

  14. #14
    SitePoint Member
    Join Date
    Jul 2014
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it is at times like this i feel like i just want to....obviously not echoing out the id means it won't display the id. I wonder how i missed that.
    Thank you very much i feel like playing the happy song by pharel right now.
    Quote Originally Posted by cpradio View Post
    In your confirmation file, you still have
    Code:
    <input type="hidden" name="id" value="<?php $authorId; ?>"/>
    When you meant to have (notice the adding of echo)
    Code:
    <input type="hidden" name="id" value="<?php echo $authorId; ?>"/>

  15. #15
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,136
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    The most obvious mistakes are usually the ones that are hardest to find.

    Our brains show us what we expect to see there because we've been staring at it so long. Give it to someone new to look at it, and it is glaringly obvious to them. Always be will to pair review your code with someone else, it will save your hide many times in the future

  16. #16
    SitePoint Member
    Join Date
    Jul 2014
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you very much for the advise, one more question though when i do a
    PHP Code:
    var_dump $_POST['Id]'); 
    i get a string of 1. I was expecting an int value. do you know why this may be happening.
    Last edited by Mittineague; Jul 24, 2014 at 11:53. Reason: diff tag

  17. #17
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,136
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Yep, PHP is a loosely type language so all input is a string (well this is true for most internet languages; input being strings), so you'd have to manually cast it.

    PHP Code:
    $id = (int)$_POST['id']; 
    However, since the book has you using prepared statements, you technically do not need to cast it because the prepared statement will do it for you.
    PHP Code:
    $s -> bindValue(':id'$_POST['id']); 
    You can also specify the type you want it converted too as well.
    PHP Code:
    $s -> bindValue(':id'$_POST['id'], PDO::PARAM_INT); 

  18. #18
    SitePoint Member
    Join Date
    Jul 2014
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    Yep, PHP is a loosely type language so all input is a string (well this is true for most internet languages; input being strings), so you'd have to manually cast it.

    PHP Code:
    $id = (int)$_POST['id']; 
    However, since the book has you using prepared statements, you technically do not need to cast it because the prepared statement will do it for you.
    PHP Code:
    $s -> bindValue(':id'$_POST['id']); 
    You can also specify the type you want it converted too as well.
    PHP Code:
    $s -> bindValue(':id'$_POST['id'], PDO::PARAM_INT); 
    Thanks once again for clearing that up


Bookmarks

Posting Permissions

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