SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2013
    Posts
    25
    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

  2. #2
    SitePoint Addict bronze trophy
    Join Date
    Apr 2013
    Location
    Ithaca
    Posts
    351
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)
    umm first of all, there are typos in your code, it should be $_GET, not $GET. Similarly the if condition should check $_POST['joketext'], not $POST['joketext'], this may be the cause of your problem but it may be more.

    Second, shouldnt the last variable in the array be $_POST rather than $_REQUEST? Note the superglobal array request contains get, post and cookie data, so you are essentially duplicating things as whatever is in $_GET is also in $_REQUEST.

    Third, why are you passing reference of the superglobals to an array? Superglobals are available everywhere, if you modify them the changes will appear in all parts of your script. You only use pass by reference when working on integers, strings or other PHP primitive types, but if you have good reasons behind this pass by reference behavior Id be glad to hear.

    Also magic quotes are deprecated and removed in the latest version, avoid them unless you have to.

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2013
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Hall of Famer View Post
    umm first of all, there are typos in your code, it should be $_GET, not $GET. Similarly the if condition should check $_POST['joketext'], not $POST['joketext'], this may be the cause of your problem but it may be more.

    Second, shouldnt the last variable in the array be $_POST rather than $_REQUEST? Note the superglobal array request contains get, post and cookie data, so you are essentially duplicating things as whatever is in $_GET is also in $_REQUEST.

    Third, why are you passing reference of the superglobals to an array? Superglobals are available everywhere, if you modify them the changes will appear in all parts of your script. You only use pass by reference when working on integers, strings or other PHP primitive types, but if you have good reasons behind this pass by reference behavior Id be glad to hear.

    Also magic quotes are deprecated and removed in the latest version, avoid them unless you have to.

    Thanks. I'll change and check when I get home.

  4. #4
    SitePoint Addict
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    363
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Shouldn't it be stripslashes() in that first loop instead of stripeslashes, are they typos? Though I would imagine that would cause an error message.
    http://www.firenza.net - my homage to a car from the 1970s

  5. #5
    SitePoint Enthusiast
    Join Date
    Dec 2013
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Hall of Famer View Post
    umm first of all, there are typos in your code, it should be $_GET, not $GET. Similarly the if condition should check $_POST['joketext'], not $POST['joketext'], this may be the cause of your problem but it may be more.

    Second, shouldnt the last variable in the array be $_POST rather than $_REQUEST? Note the superglobal array request contains get, post and cookie data, so you are essentially duplicating things as whatever is in $_GET is also in $_REQUEST.

    Third, why are you passing reference of the superglobals to an array? Superglobals are available everywhere, if you modify them the changes will appear in all parts of your script. You only use pass by reference when working on integers, strings or other PHP primitive types, but if you have good reasons behind this pass by reference behavior Id be glad to hear.

    Also magic quotes are deprecated and removed in the latest version, avoid them unless you have to.
    Thanks for your help. It was a syntax error with the post like you said. I changed it and it worked. I'll use an IDE when something crazy happens from now on.

  6. #6
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,806
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Try this at the top of your Php files:
    PHP Code:
    <?php 
    error_reporting
    (-1);
    ini_set('display_errs'true);
    It will show all syntax, undeclared variables, etc

  7. #7
    SitePoint Enthusiast
    Join Date
    Dec 2013
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    thanks.

    will do.

  8. #8
    SitePoint Enthusiast
    Join Date
    Dec 2013
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    meant for this to be in the other post.
    Last edited by John Crutchfield; Dec 26, 2013 at 14:58. Reason: wrong topic


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
  •