SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 46
  1. #1
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Delete Joke Ch 4 of Build your own Data Driven Web Site...

    I'm trying to delete jokes in the exercise in Chapter 4 of Kevin Yank's 4th Edition of BUILD YOUR OWN DATABASE DRIVEN WEB SITE USING PHP & MYSQL, but I'm having problems. The code causes the server to crash when I test it with localhost. It has a problem with the comma in the line:

    $result = mysqli_query($link, 'SELECT id, joketext FROM joke');

    If I remove the comma after id, the server doesn't crash, but the code will not delete the joke. The code worked until I made the changes for the "delete jokes" part of the exercise. Now (as long as there is no comma) I can see the joke number and delete button but no joke. If I delete "id" from the above line of code, I can see the joketext and delete buttons, but they do not work. If I enclose id, joketext in parenthesis like this (id, joketext), I get a message saying there should only be one parameter.

    Here is my code:
    if (isset($_GET['deletejoke']))
    {
    $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, joketext 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';

    My include code:

    <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>

    I'm really new to PHP and need some help.
    Last edited by lanwendy; Jul 19, 2009 at 06:46. Reason: code madeturned into a smiley

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,044
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)
    Hi lanwendy, welcome to the forums

    Is $link a valid resource coming from either mysqli_connect() or mysqli_init() ??

  3. #3
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote:
    Is $link a valid resource coming from either mysqli_connect() or mysqli_init() ??


    Yes, earlier in the code it connects to the database using mysqli_connect(). As long as there is no comma in:
    $result = mysqli_query($link, 'SELECT id, joketext FROM joke');
    there is no problem with the connection. I think the problem is with the select having two parameters or the problem comes at a different point in the code but the id and joketext don't seem to be associated together.
    Actually, when I couldn't get this working, I copied and pasted the code from the book's pdf file to my own file. I changed the password to my own and changed the database name to what I had called my database. Everything else is identical to the book and there was no problem with the code prior to adding the delete portion. Could there be something wrong with my Apache configuration?

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    This...
    PHP Code:
    "DELETE FROM joke WHERE id='id'"
    Should be...
    PHP Code:
    "DELETE FROM joke WHERE id = " . (int)$id " LIMIT 1"
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,044
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)
    I notice you are using $_GET['deletejoke'] -- From a link eg. joke.php?deletejoke=1 or a form with an action = GET

    But you are using $_POST['id'] -- from a form with an action = POST

    There very well could be a syntax error somewhere, but the inconsistency could have something with the DELETE not working.

  6. #6
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    This...
    PHP Code:
    "DELETE FROM joke WHERE id='id'"
    Should be...
    PHP Code:
    "DELETE FROM joke WHERE id = " . (int)$id " LIMIT 1"
    Thanks for your suggestion. I tried making this change, but the server still shuts down.

  7. #7
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague View Post
    I notice you are using $_GET['deletejoke'] -- From a link eg. joke.php?deletejoke=1 or a form with an action = GET

    But you are using $_POST['id'] -- from a form with an action = POST

    There very well could be a syntax error somewhere, but the inconsistency could have something with the DELETE not working.
    Thanks for your help. I noticed that too. The line in the form looks like this: <form action="?deletejoke" method="post"> . I've tried changing all to POST. I also tried changing all to GET. When I changed to GET, I changed this line in the form to GET as well. However, none of these attempts worked.

  8. #8
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,044
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)
    The problem was staring me in the face but I didn't see it right away.

    My book (2nd edition) uses the variable $jokeid NOT $id

    "id" is the name of the field, so
    PHP Code:
    $sql "DELETE FROM joke WHERE id='id'"
    is saying something like "delete the row(s) from the joke table where the field named id has a value of the field name id"

    The code for that line in my book (2nd edition NOT 4th) is
    PHP Code:
    if (isset($_GET['deletejoke'])) {
    $jokeid $_GET['deletejoke'];
    $sql "DELETE FROM Jokes WHERE ID='$jokeid'";
    if (@
    mysql_query($sql)) {
    echo(
    '<p>The joke has been deleted.</p>');
    } else {
    echo(
    '<p>Error deleting joke: ' mysql_error() . '</p>');

    so I don't know how much that differs from the 4th edition or if your code is exactly the same as your book's or if the book is at fault.

    Changing a tutorial's code around will help you learn things and IMHO it is good to experiment. But I usually try the code as-is first to make sure it works OK before I start messing with it.

    In any case, my version is now 5 years old so it looks like it's time for me to get my hands on a copy of the newer version.

  9. #9
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague View Post
    The problem was staring me in the face but I didn't see it right away.

    My book (2nd edition) uses the variable $jokeid NOT $id

    "id" is the name of the field, so
    PHP Code:
    $sql "DELETE FROM joke WHERE id='id'"
    is saying something like "delete the row(s) from the joke table where the field named id has a value of the field name id"

    The code for that line in my book (2nd edition NOT 4th) is
    PHP Code:
    if (isset($_GET['deletejoke'])) {
    $jokeid $_GET['deletejoke'];
    $sql "DELETE FROM Jokes WHERE ID='$jokeid'";
    if (@
    mysql_query($sql)) {
    echo(
    '<p>The joke has been deleted.</p>');
    } else {
    echo(
    '<p>Error deleting joke: ' mysql_error() . '</p>');

    so I don't know how much that differs from the 4th edition or if your code is exactly the same as your book's or if the book is at fault.

    Changing a tutorial's code around will help you learn things and IMHO it is good to experiment. But I usually try the code as-is first to make sure it works OK before I start messing with it.

    In any case, my version is now 5 years old so it looks like it's time for me to get my hands on a copy of the newer version.
    Thanks for continuing to puzzle over my code, Mittineague. I noticed the missing variable sign yesterday when I was checking it again. Obviously, it needs it and the fault is mine, not the book's. However, it didn't fix the immediate problem of the server crashing. It really doesn't seem to like the SELECT function to have two parameters. It won't even work if I change [PHP] $result = mysqli_query($link, 'SELECT id, joketext FROM joke'); [PHP] to [PHP] $result = mysqli_query($link, 'SELECT * FROM joke'); [PHP] However, the code is broken but doesn't cause the server to crash if I change it to [PHP] $result = mysqli_query($link, 'SELECT id FROM joke'); [PHP] or to [PHP] $result = mysqli_query($link, 'SELECT joketext FROM joke'); [PHP]. I'm using the developer's configuration of the Apache server on my local computer.

  10. #10
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When you say "server crash", are you getting something like an operating system popup message telling you that? Specifically, what makes you think the server "crashes".

  11. #11
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    When you say "server crash", are you getting something like an operating system popup message telling you that? Specifically, what makes you think the server "crashes".
    I get a popup message that says:
    Apache HTTP Server has encountered a problem and needs to close. We are sorry for the inconvenience. ... Please tell Microsoft about this problem.

    Error signature:
    szAppName : httpd.exe szAppVer : 2.2.11.0 szModName : php_mysqli.dll
    szModVer : 5.2.10.10 offset : 00002b6b

    It also says if I send the error report to Microsoft, it will also send this technical information: C:\DOCUME~1\Wendy\LOCALS~1\Temp\WERc1ec.dir00\httpd.exe.mdmp
    C:\DOCUME~1\Wendy\LOCALS~1\Temp\WERc1ec.dir00\appcompat.txt
    However, when I go looking for these two files, I cannot find them, even though I have my computer set up so that I can view all files.

  12. #12
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,044
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)
    Another member is having a similar problem http://www.sitepoint.com/forums/show...=1#post4331346

    I can't imagine that "id" would be considered a "reserved word", but I'm thinking it may be a naming conflict somewhere.

    If you change the field name to something like "jid" does it then work?

  13. #13
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, Im having the same problem, I did try chaning to $jid with no luck!

    As lanwendy says I get the same Apache error and if you remove the id from

    $result = mysqli_query($link, 'SELECT id, joketext FROM joke'); change to
    $result = mysqli_query($link, 'SELECT joketext FROM joke');

    Then it works fine, except it does not work because it does not parse the id of the joke (That you need to delete it)

    Any help and insight would be greatly appreciated.

  14. #14
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I HAVE FOUND THE SOLUTION!

    Don't ask me how, or why, but i had a suspicion that in can be the PHP installation.

    So I installed PHP 5.3.0 and did the set-up according to the book (4th ed) (Accept u must use the php.ini-production file as basis for the settings) and its working 100&#37;, so the code is solid as is!

    To download and instal go to:

    Will post url shortly - sorry the forum doesnt want me to publish links because i have les than 10 posts.

    and get the VC6 x86 Thread Safe - Zipped version.

    Hope this sorts out your problem as well!

  15. #15
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, ive got my posts up to 10 so the URL is:

    http://windows.php.net/download/

  16. #16
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This worked for me too. I installed PHP 5.3.0 and got everything working. Thanks so much for the help.

  17. #17
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool stuff! Happy Coding

  18. #18
    SitePoint Member
    Join Date
    Jul 2009
    Location
    Athens, Greece
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks fbrill for me as well, I faced the same problem with the "delete joke" exercise and thanks to your suggestion to install php 5.3.0 everything works just fine

  19. #19
    SitePoint Member
    Join Date
    Aug 2009
    Location
    Melbourne, Australia
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Worked for me too!
    Thanks.
    Any clues as to why the problem arose with php 5.2?

  20. #20
    SitePoint Enthusiast
    Join Date
    May 2008
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I too had this issue, I have installed PHP 5.3 and I'm still having probelms.

    The Jokes list page displays fine with the Delete Buttons, but when I press one of these to delete a joke, nothing happents. No Errors or any thing and none of the jokes are deleted. I think the page does refresh though. Soemthing is not right. I've gone over and over the code from the book. Am I going mad and missing the obvious?

    Here is my code;

    index.php

    <?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 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, 'ijdb'))
    {
    $error = 'Unable to locate the joke database.';
    include 'error.html.php';
    exit();
    }

    if (isset($_POST['joketext']))
    {
    $joketext = mysqli_real_escape_string($link, $_POST['joketext']);
    $sql = 'INSERT INTO joke SET
    joketext="' . $joketext . '",
    jokedate =CURDATE()';
    if (!mysqli_query($link, $sql))
    {
    $error = 'Error adding submitted joke: ' . mysql_error($link);
    include 'error.html.php';
    exit();
    }

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

    if (isset($_GET['deletejoke']))
    {
    $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 joketext 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';
    ?>

    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-gb">
    <head>
    <meta name="language" content="en" />
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <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 ($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>



    The Apache Server Starts fine, no probs and indicates its using PHP 5.3.0 so its using the new install as directed in the previous posts.

    Can anybody help?

  21. #21
    SitePoint Enthusiast
    Join Date
    May 2008
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've found the problem now. It was a missing id from the SELECT function while fetchings jokes for the jokes list. I must add though. I was'nt effected by the PHP version 5.2..

    Does anybody know if its just the windows PHP installation thats effected or may this be an issue on some web servers?

  22. #22
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This specific issue was probably just windows, or maybe needed specific version combinations of php/apache/mysql. Regardless, if the web server is run by a commercial hosting company, they would be on top of something severe like this. Constant server crashes are pretty devastating to their business

  23. #23
    SitePoint Zealot
    Join Date
    Jul 2009
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, I've downloaded php5.3 and I'm trying to install it again but the file php.ini-dist isn't present? There is only ini-development and ini-production.

  24. #24
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use development

  25. #25
    SitePoint Enthusiast
    Join Date
    May 2008
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I used the production ini file, configue it following Kevin Yanks book and it works a treat. Not sure what the difference is?


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
  •