SitePoint Sponsor

User Tag List

Results 1 to 21 of 21

Thread: PHP Update code

  1. #1
    Non-Member
    Join Date
    Mar 2005
    Posts
    832
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Update code

    Does anyone know why this code will not update its always saying there was an error while rpocessing your request

    PHP Code:
    <?
    include "db_connect.php";

    $id=$_GET['id'];

    $query 'UPDATE chatmail SET read="y" WHERE id="' $id '"'

    if (
    mysql_query($query)) 
    echo 
    "Worked"
    else 
    echo 
    "There was an error while processing your request.";
    ?>

  2. #2
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Turn on/up error reporting, see if that helps:
    PHP Code:
    //Add at the very top of your script
    ini_set('display_errors'1);
    error_reporting(E_ALL); 
    PHP questions? RTFM
    MySQL questions? RTFM

  3. #3
    Non-Member
    Join Date
    Mar 2005
    Posts
    832
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no errors appeared.

  4. #4
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, meant to include in that first post that you should also familiarize yourself with this function:

    mysql_error
    PHP questions? RTFM
    MySQL questions? RTFM

  5. #5
    SitePoint Addict aniltc's Avatar
    Join Date
    Nov 2006
    Location
    INDIA
    Posts
    399
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    try to change the code as
    PHP Code:
    <?
    include "db_connect.php";

    $id=$_GET['id'];

    $query "UPDATE chatmail SET read="y" WHERE id='$id'";

    if (
    mysql_query($query))
    echo 
    "Worked";
    else
    echo 
    "There was an error while processing your request.";
    ?>
    it should work

  6. #6
    Non-Member
    Join Date
    Mar 2005
    Posts
    832
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    blank page but it showed when i changed the " to ' but it still said the error.

  7. #7
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    Regina, SK
    Posts
    318
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First off, you're leaving yourself open to sql injection attacks with that code. Read up on mysql_real_escape_string()

    Then, as kromey mentioned, echo out mysql_error() to find out what the database is returning after you run the query.
    Reduce or eliminate spam on your sites without CAPTHCAs
    - maybe it is possible: http://formantispam.thekerrs.ca/

  8. #8
    Non-Member
    Join Date
    Mar 2005
    Posts
    832
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    im confusded now

  9. #9
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this...
    Code:
    <?php
    include("db_connect.php");
    
    $id=$_GET['id'];
    
    $query = "UPDATE chatmail SET read='y' WHERE id='$id'";
    
    if (mysql_query($query))
    echo "Worked";
    else
    echo "There was an error while processing your request.";
    ?>

  10. #10
    Non-Member
    Join Date
    Mar 2005
    Posts
    832
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    still the same

  11. #11
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    tidalwave, why dont you do some debugging?

    you ignored kromeys post.

  12. #12
    Non-Member
    Join Date
    Mar 2005
    Posts
    832
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i didnt understand what kromey said i got this new code but it still not doing anything:

    PHP Code:
    <?
    include "db_connect.php";
    $query "UPDATE chatmail SET read = 'y' WHERE id = '2'";
    mysql_query($query);
    ?>

  13. #13
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    he gave you a link to the manual. you should read it. it describes what the function does. it shows you how to use it.

    you simply must learn to use the manual.

    even if someone doesnt provide you with a hyperlink, the name of the function is given so you can read the relevant section(s) of the manual.

    if you dont know how to use a function, the manual is available to you.

  14. #14
    Non-Member
    Join Date
    Mar 2005
    Posts
    832
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok the error said:

    mysql_error(UPDATE chatmail SET read = 'y' WHERE id = '2')

  15. #15
    Non-Member
    Join Date
    Mar 2005
    Posts
    832
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thats what it outputted on a browser

  16. #16
    SitePoint Evangelist
    Join Date
    Mar 2006
    Location
    Sweden
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should do something like:
    $query = "UPDATE chatmail SET read = 'y' WHERE id = '2'";
    mysql_query($query) or exit( mysql_error() );

    But just when you debug. When you get it working, remove it so there is no risk that mysql_error() will be outputted to your visitors.

  17. #17
    Non-Member
    Join Date
    Mar 2005
    Posts
    832
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is the error it gives me:

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'read = 'y' WHERE id = '2'' at line 1

  18. #18
    SitePoint Evangelist
    Join Date
    Mar 2006
    Location
    Sweden
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What datatype are you using for those columns, read and id?

  19. #19
    Non-Member
    Join Date
    Mar 2005
    Posts
    832
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    varchar for read and id is on int and auto_increment and on unique

  20. #20
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    Regina, SK
    Posts
    318
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahh, found it. READ is a reserved word in MySQL. Put backquotes (`) around it and you should be fine:

    PHP Code:
    $query "UPDATE chatmail SET `read` = 'y' WHERE id = '2'"
    Reduce or eliminate spam on your sites without CAPTHCAs
    - maybe it is possible: http://formantispam.thekerrs.ca/

  21. #21
    Non-Member
    Join Date
    Mar 2005
    Posts
    832
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah works now thank you so much


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
  •