SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Guru
    Join Date
    Nov 2000
    Posts
    740
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Updating MySQL with PHP problem

    I can't work out why my db is not updating. Any ideas?

    My code:

    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <?
    /*
    $ip_addr = $_SERVER["REMOTE_ADDR"]; 
    , date='date()', time='time()', ipaddress='$ip_addr'
    */
    include('sb/dbconnect.inc');
    mysql_query("UPDATE Test SET keyword='keyword', detail='$detail'") or exit(mysql_error());
    mysql_close();
    ?>
    <body>
    hello 
    </body>
    </html>
    PHP Code:
    <?
    mysql_connect
    ("localhost""User""PW") or die ('I cannot connect to the database because: ' mysql_error());
    mysql_select_db ("mydb"); 
    ?>

  2. #2
    SitePoint Evangelist
    Join Date
    Jul 2004
    Location
    USA
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your SQL Query is incomplete, you need some sort of "WHERE `field` = 'something'"

    so

    PHP Code:
    mysql_query("UPDATE Test SET keyword='keyword', detail='$detail' WHERE `field` = 'something'"
    Also, where is the value of $detail coming from.
    BKerr

  3. #3
    SitePoint Guru
    Join Date
    Nov 2000
    Posts
    740
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So to show all records, do I put WHERE keyword=* or something like that? $detail is a passed parameter. i.e. mypage.php?detail=somestuff

  4. #4
    SitePoint Evangelist
    Join Date
    Jul 2004
    Location
    USA
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, the SQL 'SELECT' does not require the WHERE parameter, only the UPDATE.

    For $detail, in case you didnt know, it doesnt automatically become a variable, you need to set it with a line:

    PHP Code:
    $detail $_GET['detail']; 
    BKerr

  5. #5
    SitePoint Guru
    Join Date
    Nov 2000
    Posts
    740
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am sorry, I wasn't clear. I mean't for my UPDATE to work, do I put WHERE keyword=*. i.e. does that work as a wildcard?

  6. #6
    SitePoint Evangelist
    Join Date
    Jul 2004
    Location
    USA
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well you could just do
    PHP Code:
    UPDATE table SET field 'whatever' WHERE `primary` != '' 
    Since your Primary Key has to have some content.
    BKerr

  7. #7
    SitePoint Evangelist
    Join Date
    Jul 2004
    Location
    USA
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you receiving any kind of SQL error when your run it?
    BKerr

  8. #8
    SitePoint Evangelist
    Join Date
    Jul 2004
    Location
    USA
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guelphdad
    No you don't. If you do not have a where condition then the update would be carried out for each row of the affected column in the update.
    I realized that, thats why I asked if he was recieving any kind of error.
    BKerr

  9. #9
    SitePoint Guru
    Join Date
    Nov 2000
    Posts
    740
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am receiving no error and it still doesn't update. My primary key field was called id so I did:

    PHP Code:
    mysql_query("UPDATE Test SET keyword='keyword', detail='detail' WHERE 'id' !=''") or exit(mysql_error()); 



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
  •