SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    No result from fetchAll();

    Hi,

    I'm totally a newbie in the world of PHP and MySQL. I am reading the book of Kevin Yank and while doing one of his examples, I encountered a weird outcome. I'm sure I followed and typed correctly the code written on his book but I wondered why I'm not getting the same result. I've checked the code back and forth and sure that it is right or maybe I'm missing something.

    I'm posting this here because I know a lot from here are very helpful and very kind to starters like me. Any inputs are highly appreciated. Looking forward for comments so that I can proceed with my studies because I think I am stuck because of this weird error.

    Please see below code for your reference.

    index.html
    HTML Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<meta charset="utf-8">
    	<title>Joke CMS</title>
    </head>
    <body>
    	<h1>Joke Management System</h1>
    	<ul>
    		<li><a href="jokes/">Manage Jokes</a></li>
    		<li><a href="authors/">Manage Authors</a></li>
    		<li><a href="categories/">Manage Joke Categories</a></li>
    	</ul>
    </body>
    </html>
    index.php
    PHP Code:
    <?php
    if (get_magic_quotes_gpc())
    {
    $process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
    while (list(
    $key$val) = each($process))
    {
    foreach (
    $val as $k => $v)
    {
    unset(
    $process[$key][$k]);
    if (
    is_array($v))
    {
    $process[$key][stripslashes($k)] = $v;
    $process[] = &$process[$key][stripslashes($k)];
    }
    else
    {
    $process[$key][stripslashes($k)] = stripslashes($v);
    }
    }
    }
    unset(
    $process);
    }

    //Display author list
    include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
    try {
        
    $result $pdo->query('SELECT id, name FROM author');
    } catch (
    PDOException $e){
        
    $error 'Error fetching authors from database! ' $e->getMessage();
        include 
    'error.html.php';
        exit();


    if (isset(
    $_POST['action']) and $_POST['action'] == 'Delete')
        {
            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 fetching authors with their jokes! ' $e->getMessage();
                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 joke category! ' $e->getMessage();
                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 joke from a specific author! ' $e->getMessage();
                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 the author from database! ' $e->getMessage();
                include 
    'error.html.php';
                exit();
            }

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

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


    include 
    'authors.html.php';
    ?>
    authors.html.php
    PHP Code:
    <?php
    include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/helper.inc.php';
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8"/>
        <title>Manage Authors</title>
    </head>
    <body>
        <h1>Manage Authors</h1>
        <p><a href="?add">Add new author</a></p>
        <ul>
            <?php foreach($authors as $author): ?>
                <li>
                    <form action="" method="post">
                        <div>
                            <?php echo htmlout($author['name']); ?>
                            <input type="hidden" value="<?php echo htmlout($author['id']); ?>"/>
                            <input type="submit" name="action" value="Edit"/>
                            <input type="submit" name="action" value="Delete"/>
                        </div>
                    </form>
                </li>
            <?php endforeach; ?>
        </ul>
        <p><a href="..">Return to JMS home</a></p>
    </body>
    </html>
    Best regards,
    Karl
    Last edited by Mittineague; Mar 2, 2014 at 22:33.

  2. #2
    SitePoint Enthusiast
    Join Date
    Feb 2014
    Posts
    41
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    What seems to be the problem?

  3. #3
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by parallelist View Post
    What seems to be the problem?
    The problem is that when I click the "Delete" button, it would not delete the author entry. I am sure I typed it right and wondering why I'm not getting the desired result. I am hoping that someone could help me resolve this one.

  4. #4
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    772
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Without getting all into this, aren't you missing the name="id" here?
    Code:
    <input type="hidden" value="<?php echo htmlout($author['id']); ?>"/>

  5. #5
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,068
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    Add these lines after the <?php at the start of the file:

    PHP Code:
    error_reporting(-1);
    ini_set('display_errors'1); 
    They will cause any errors, warnings and notices to be displayed. For a live site you wouldn;t display any errors, warnings and notices, instead you would log them to an error file
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  6. #6
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Drummin View Post
    Without getting all into this, aren't you missing the name="id" here?
    Code:
    <input type="hidden" value="<?php echo htmlout($author['id']); ?>"/>
    There you are!
    Thanks Drummin for the keen eye, this problem is solved now. I'm starting to wonder now what really the name attribute does.
    But anyways, thanks a lot for the comment. This is what usually happens when you're sleepy coding/studying.

    Cheers,
    Karl

  7. #7
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SpacePhoenix View Post
    Add these lines after the <?php at the start of the file:

    PHP Code:
    error_reporting(-1);
    ini_set('display_errors'1); 
    They will cause any errors, warnings and notices to be displayed. For a live site you wouldn;t display any errors, warnings and notices, instead you would log them to an error file
    Thanks for the tip bro.

  8. #8
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    772
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    I'm starting to wonder now what really the name attribute does
    The name identifies the input field and will be the key in the $_POST array with its matching value. So <input name="toy" value="ball" /> becomes $_POST['toy'] would be ball.

  9. #9
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Drummin View Post
    The name identifies the input field and will be the key in the $_POST array with its matching value. So <input name="toy" value="ball" /> becomes $_POST['toy'] would be ball.
    Thanks for that clear explanation, I really learned from your comments.

  10. #10
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Drummin View Post
    The name identifies the input field and will be the key in the $_POST array with its matching value. So <input name="toy" value="ball" /> becomes $_POST['toy'] would be ball.
    Hi Drummin,
    I am just wondering what's the difference between an
    Code:
    id
    and
    Code:
    name
    attribute. I often notice that both attributes have the same value.
    Thanks!

  11. #11
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    772
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    The name used in form inputs are the identifier that is sent on POST or GET that becomes the key as I already mentioned. The same name can be used more than on time as with radio buttons where a group might have the same name but only the selected radio value is sent to POST. An id on the other hand must be unique on a given page and is often used in Javascript where they want to reference an element by the id or style the element with css. The value given to an id is not sent to POST.

  12. #12
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Drummin View Post
    The name used in form inputs are the identifier that is sent on POST or GET that becomes the key as I already mentioned. The same name can be used more than on time as with radio buttons where a group might have the same name but only the selected radio value is sent to POST. An id on the other hand must be unique on a given page and is often used in Javascript where they want to reference an element by the id or style the element with css. The value given to an id is not sent to POST.
    Thanks again for that Drummin, I understand it now clearly.
    I have another question for you if you don't mind, I am curious too with the fetch() and fetchAll(). Can you explain what's the difference between the two?
    Thanks for your patience and enlightenment.

  13. #13
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    772
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Well I'm sure others could describe this better, but fetchall() is going to return the complete query result as an array that you can later loop through in some other way, e.g. using foreach()
    The fetch() returns a single row of the fields defined in the query containing both the numeric array key and the column name for each field. Unless grabbing a single row of data, you will need to use WHILE to loop through each row. I tend to use FETCH_ASSOC as I just want column names in a WHILE loop. There may be times you just want FETCH_NUM for just using the numeric key. Just depends on what you're wishing to do.


Tags for this Thread

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
  •