SitePoint Sponsor

User Tag List

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

    PHP/MYSQL novice to ninja page 200 challenge issue

    I've been learning php/mySQL thru a manual by sitepoint and have run into a problem trying to complete a challenge in the book.

    They've asked me to create a delete confirmation screen for a db of jokes. I'd like to list the the name of the author to be deleted and all of his/her jokes. I believe the bindValue command is causing me issues...
    Any insight as to what my be wrong in this code would be great help. Cheers!
    PHP Code:
    <?php
           
            
    include $_SERVER['DOCUMENT_ROOT'] . '/phpbed/includes/dbconnect.php';
            echo 
    'test3<br>';
            echo 
    'id ' $_POST['id'] . '<br>';
            echo 
    $_POST['id'];
            
    $sql 'SELECT joketext FROM joke WHERE authorid = :id';
            
    $sql->bindValue(':id'$_POST['id']);
            
    $result $pdo->query($sql);
            echo 
    "test4";
            while (
    $row $result->fetch())
            {
                
    $joke[] = $row['joketext'];
            }
            echo 
    'test5';
            
    ?>
            <div>
            including all of this authors jokes?
            </div>
                <?php foreach ($joke as $bleh): ?>
            
            <blockquote>
                <p><?php echo $bleh?> </p>
            </blockquote>
            
            <?php endforeach; ?>
    Please note that i've plopped a bunch of test echoes and i can't seem to get past test4.

    Thanks guys!

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,219
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Yep, it is quite simple actually, and you are really close.

    But you are using the $sql variable as if it were the $pdo variable.

    You need to change your $sql = 'SELECT joketext FROM joke WHERE authorid = :id''; to:

    PHP Code:
    $sql $pdo->prepare('SELECT joketext FROM joke WHERE authorid = :id'); 

  3. #3
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Unfortunately that didn't do the trick...
    Here's the printed result
    "
    Confirm author deletion

    Are you sure you would like to delete the following?
    Author Id = 2
    Author name = Joan Smith.
    test3
    id 2
    test4
    "

    and the new code

    PHP Code:
    <?php include $_SERVER['DOCUMENT_ROOT'] . '/phpbed/includes/dbconnect.php'?>
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>Are you sure?</title>
        </head>
        <body>
            <h1>Confirm author deletion</h1>
            <div>
                Are you sure you would like to delete the following?  
            </div>
            <div>
                Author Id =         
            <?php echo $authorid;?>
            </div>
            <div>
                <?php echo "Author name = " $authorname ."."?>
            </div>
            <?php
           
            
    include $_SERVER['DOCUMENT_ROOT'] . '/phpbed/includes/dbconnect.php';
            
            echo 
    'test3<br>';
            echo 
    'id ' $_POST['id'] . '<br>';
            
            
    $sql $pdo->prepare('SELECT joketext FROM joke WHERE authorid = :id');
            
    $sql->bindValue(':id'$_POST['id']);
            
    $result $pdo->query($sql); 
            
            echo 
    'test4';
            
            while (
    $row $result->fetch())
            {
                
    $joke[] = $row['joketext'];
            }
            echo 
    'test5';
            
    ?>
            <div>
            including all of this authors jokes?
            </div>
                <?php foreach ($joke as $bleh): ?>
            
            <blockquote>
                <p><?php echo $bleh?> </p>
            </blockquote>
            
            <?php endforeach; ?>
        </body>
    </html>
    However if I simply declare the authored without doing a prepare() it works
    PHP Code:
    $sql = ('SELECT joketext FROM joke WHERE authorid = 1');
            
            
    $result $pdo->query($sql); 
    Edit: Thanks in advanced!

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,219
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Try changing $result = $pdo->query($sql); to $result = $pdo->execute();

  5. #5
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    God you're fast! I just want to give you a hug!
    But it didn't work...
    PHP Code:
    $sql $pdo->prepare('SELECT joketext FROM joke WHERE authorid = :id');
            
    $sql->bindValue(':id'$_POST['id']);
            
    $result $pdo->execute(); 
    I also tried putting $sql into the execute().

  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,219
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    My bad, I had a typo. Instead of $pdo->execute(); it should have read $sql->execute();

  7. #7
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    We're really close, only now I don't get past test4...

    PHP Code:
    echo 'test3<br>';
            echo 
    'id ' $_POST['id'] . '<br>';
            
            
    $sql $pdo->prepare('SELECT joketext FROM joke WHERE authorid = :id');
            
    $sql->bindValue(':id'$_POST['id']);
            
    $result $sql->execute(); 
            
            echo 
    'test4';
            
            while (
    $row $result->fetch())
            {
                
    $joke[] = $row['joketext'];
            }
            echo 
    'test5'
    Thanks again!

  8. #8
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I get the impression that execute isn't passing anything into $result...

  9. #9
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,219
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Sorry, learning the entire PDO bit myself, try this:
    PHP Code:
    echo 'test3<br>';
            echo 
    'id ' $_POST['id'] . '<br>';
            
            
    $sql $pdo->prepare('SELECT joketext FROM joke WHERE authorid = :id');
            
    $sql->bindValue(':id'$_POST['id']);
            
    $sql->execute(); 
            
            echo 
    'test4';
            
            while (
    $row $sql->fetch())
            {
                
    $joke[] = $row['joketext'];
            }
            echo 
    'test5'

  10. #10
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes! It worked!

    I wonder why we can't use the '=' to pass on to the execute().

    Thank you so much!

  11. #11
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,219
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Likely because it is an object/class reference. So the result is self contained in the class and you can then use the fetch() method to gain access to the result set


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
  •