SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Threaded View

  1. #1
    SitePoint Member
    Join Date
    Dec 2013
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP not adding joke to database

    Ho. I'm going through the PHP and MySQL Novice to Ninja book, and doing the add joke part.
    As far as I can see, th ecode is right. I'm not getting any errors, but when it redirects to the joke list, the new jokes don't show up/ I'm not getting any errors. Checked the MySQL database at my site and the new jokes aren't being added.

    Here is my code:

    //index.php

    PHP Code:
    <?php
    if (get_magic_quotes_gpc())
    {
    $process = array(&$GET, &$_COOKIE, &$_REQUEST);
    while (list(
    $key$val) = each($process))
    {
    foreach (
    $val as $k=> $v)
    {
        unset(
    $process[$key][$k]);
        if (
    is_array($v))
        {
        
    $process[$key][stripeslashes($k)]=$v;
        
    $process[] = &$process[$key][stripeslashes($k)];
        }
        else
        {
        
    $process[$key][stripeslashes($k)] = stripeslashes($v);
            
    }
    }
    }
    unset(
    $process);
    }
        if (isset(
    $_GET['addjoke']))
        {
        include 
    'form.html.php';
        exit();
        }
        try
        {
        
    $pdo = new PDO('mysql:host=localhost;dbname=greyto5_ijdb','********''*******');
        
    $pdo->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
        
    $pdo->exec('SET NAMES "utf8"');
        }
        catch (
    PDOException $e)
        {
        
    $error 'Unable to connect to the database server.';
        include 
    'error.html.php';
        exit();
        }
        
    if (isset(
    $POST['joketext']))
        {
        try
        {
            
    $sql 'INSERT INTO jokes SET
                    joketext = :joketext,
                    jokedate = CURDATE()'
    ;
                    
    $s $pdo->prepare($sql);
                    
    $s->bindValue(':joketext'$_POST['joketext']);
                    
    $s->execute();
                    }
                    catch(
    PDOException $e)
                    {
                    
    $error 'Error adding submitted joke: ' $e->getMessage();
                    include 
    'error.html.php';
                    exit();
                    }
                    
    header('Location: .');
                    exit();
                    }
                    try
                    {
                    
    $sql ='SELECT joketext FROM jokes';
                    
    $result $pdo->query($sql);
                    }
                    catch(
    PDOException $e)
                    {
                    
    $error 'Error fetching jokes: '$e->getMessage();
                    include 
    'error.html.php';
                    exit();
                    }
                    while(
    $row $result->fetch())
                    {
                    
    $jokesarray[] = $row['joketext'];
                    }
                    include 
    'jokes.html.php';



    ////jokes.html.php

    PHP Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title>List of Jokes</title>
    </head>
    <body>  
    <p><a href="?addjoke"> Add your own joke</a></p>
    <p>Here are all the jokes in the database:</p>
    <?php foreach ($jokesarray as $joke): ?>
    <blockquote>
    <p> <?php echo htmlspecialchars($jokeENT_QUOTES'UTF-8');?>
    </p>
    </blockquotes>
    <?php endforeach; ?>
    </body>
    </html>
    //form.html.php

    PHP Code:
    <!DOCTYPE html>
    <
    html lang="en"
    <head
    <
    meta charset="utf-8">
    <
    title>Add Joke</title>
    <
    style type="text/css"
    textarea {
        
    displayblock;
        
    width100%;
        }
        </
    style>
        </
    head>
        <
    body>
        <
    form action ="?" method "post">
        <
    div>
        <
    label for ="joketext">Type your joke here:</label>
        <
    textarea id="joketext" name="joketext" rows="3" cols="40">
        </
    textarea>
        </
    div>
        <
    div><input type="submit" value="Add"></div>
        </
    form>
        </
    body>
        </
    html
    ///error.html.php

    PHP Code:
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <title>Script Error</title>
      </head>
      <body>
        <p>
          <?php echo $error?>
        </p>
      </body>
    </html>
    Last edited by cpradio; Dec 3, 2013 at 09:30. Reason: Added php tags


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
  •