SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Associating form inputs and getting two parameters into PHP from form

    I'm still puzzling over the "Delete data from database" code in Kevin Yank's book, "Build your own Database Driven Website Using PHP and MYSQL, 4th edition".

    Here is my complete code. As it is, I can add and delete jokes, but on the webpage, I can only see the joke's id number and the delete button (no joke). In Kevin Yank's book, 4th edition, and online tutorial from the book, the SELECT code should be able to take more than one parameter. I have it set to only the id parameter in
    PHP Code:
     $result mysqli_query($link'SELECT id FROM joke'); 
    If I add a comma and joketext to the SELECT, like this:
    PHP Code:
     $result mysqli_query($link'SELECT id, joketext FROM joke'); 
    , my localhost server crashes. If anyone can help me figure this out, I'll buy the book and finish the tutorial. Otherwise, I'm just about ready to give up on PHP and MYSQL.


    <?php
    if (get_magic_quotes_gpc())
    {
    function stripslashes_deep($value)
    {
    $value = is_array($value) ?
    array_map('stripslashes_deep', $value) :
    stripslashes($value);
    return $value;
    }
    $_POST = array_map('stripslashes_deep', $_POST);
    $_GET = array_map('stripslashes_deep', $_GET);
    $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
    $_REQUEST = array_map('stripslashes_deep', $_REQUEST);
    }
    if (isset($_GET['addjoke']))
    {
    include 'form.html.php';
    exit();
    }
    $link = mysqli_connect('localhost', 'root', 'password');
    if (!$link)
    {
    $error = 'Unable to connect to the database server.';
    include 'error.html.php';
    exit();
    }
    if (!mysqli_set_charset($link, 'utf8'))
    {
    $output = 'Unable to set database connection encoding.';
    include 'output.html.php';
    exit();
    }
    if (!mysqli_select_db($link, 'jokedb'))
    {
    $error = 'Unable to locate the joke database.';
    include 'error.html.php';
    exit();
    }
    if (isset($_POST['joketext']))
    {
    $id = mysqli_real_escape_string($link, $_POST['id']);
    $sql = 'INSERT INTO joke SET
    joketext="' . $joketext . '",
    jokedate=CURDATE()';
    if (!mysqli_query($link, $sql))
    {
    $error = 'Error adding submitted joke: ' . mysqli_error($link);
    include 'error.html.php';
    exit();
    }
    header('Location: .');
    exit();
    }
    if (isset($_GET['deletejoke']))
    {
    /* need to use post to be able to delete and show result on page */
    $id = mysqli_real_escape_string($link, $_POST['id']);
    $sql = "DELETE FROM joke WHERE id='$id'";
    if (!mysqli_query($link, $sql))
    {
    $error = 'Error deleting joke: ' . mysqli_error($link);
    include 'error.html.php';
    exit();
    }
    header('Location: .');
    exit();
    }
    $result = mysqli_query($link, 'SELECT id FROM joke');
    if (!$result)
    {
    $error = 'Error fetching jokes: ' . mysqli_error($link);
    include 'error.html.php';
    exit();
    }
    while ($row = mysqli_fetch_array($result))
    {
    $jokes[] = array('id' => $row['id'], 'text' => $row['joketext']);
    }
    include 'jokes.html.php';
    ?>


    Here's the complete code for "jokes.html.php":

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>Delete Joke</title>
    <meta http-equiv="content-type"
    content="text/html; charset=utf-8"/>

    </head>
    <body>
    <p><a href="?addjoke">Add your own joke</a></p>
    <p>Here are all the jokes in the database: </p>
    <?php foreach ($jokes as $joke): ?>
    <form action="?deletejoke" method="POST">
    <blockquote>
    <p>
    <?php echo htmlspecialchars($joke['text'], ENT_QUOTES, 'UTF-8'); ?>
    <input type="hidden" name="id" value="<?php echo $joke['id']; ?>"/>
    <input type="submit" value="Delete"/>
    </p>
    </blockquote>
    </form>
    <?php endforeach; ?>
    </body>
    </html>

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If it's crashing, that's not a php code issue. You might have an incompatible or just plain buggy version of the mysqli extension installed.

    You might consider using xampp or wamp. They're packages that usually work pretty good with minimal installation effort.

  3. #3
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks so much. I was wondering if there was something wrong with the installation or configuration of Apache. I'll give one of your suggestions a try in a few days and let you know how it goes.

  4. #4
    SitePoint Evangelist Waffles's Avatar
    Join Date
    Nov 2005
    Posts
    435
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http://www.sitepoint.com/forums/showthread.php?t=629360

    perhaps this is relevant, same query, similar problems

  5. #5
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried fbrill's solution of installing PHP 5.3.0 and got everything working. Thanks everyone for your help.
    To see fbrill's solution, see:
    http://www.sitepoint.com/forums/showthread.php?t=627677

  6. #6
    SitePoint Member
    Join Date
    Jul 2009
    Location
    Athens, Greece
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks for suggesting the fbrill's solution, I faced the same problem and by installing PHP 5.3.0 everything works just fine!


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
  •