SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: UPDATE problems

  1. #1
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    UPDATE problems

    i have a query that pulls a value from my database, adds one, then reinserts it via UPDATE.
    well, everything works till the update.
    the variable holding the new number has the correct num as i'm printing it b4 insertion.

    but when i update the record it inserts 0.
    here's the code.
    PHP Code:
    <?
      $get_num 
    "SELECT num_of_links from links_catagories WHERE cat_id = '$cat_id'";
      
    $do_get_num mysql_query($get_num$db_connect);
      
    $row mysql_fetch_array($do_get_num);
      
    $temp_num $row[num_of_links] + 1;
      
    $insert_new "UPDATE links_catagories SET num_of_links = '$temp_num' WHERE cat_id = '$cat_id'";
      echo 
    $temp_num;
      echo 
    $row[num_of_links];
      if(!
    mysql_query($insert_new$db_connect))
      {
        echo 
    mysql_error();
        exit;
      }
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  2. #2
    SitePoint Zealot vodica's Avatar
    Join Date
    Jul 2001
    Location
    SLovenia
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Helo, why don't you try printing the value and the query you run.... ? It should help! I had the same problems... I hope it helps
    Last edited by vodica; Oct 6, 2001 at 01:30.
    while(true) sheep++;

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2001
    Location
    Mountain View, CA
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure what the proper syntax is but you could try changing ...

    $temp_num = $row[num_of_links] + 1;

    to ...

    $temp_num = ($row["num_of_links"] + 1);

    and see if it helps. BTW, I don't think you need to surround numbers in a query with single quotes but that is probably not the cause of the problem.
    John
    AdBizInfo - Online Advertising Resource
    Top10Links - Directory Of Top Sites
    MovieRumors - Movie Message Board

  4. #4
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you should be using this instead anyway:

    UPDATE links_catagories SET num_of_links = num_of_links + 1 WHERE cat_id = '$cat_id'
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  5. #5
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Vodica, i am printing out the values. don't you see the echo $temp_num; in there?

    doc, can i just run that one query and have it update the value by 1?
    what i'm saying is could i just do this to update the value by 1:
    PHP Code:
    <?
    $update 
    "UPDATE links_catagories SET num_of_links = num_of_links + 1 WHERE cat_id = '$cat_id'";
    if(
    mysql_query($update$db_connect))
    {
      echo 
    "Update succuessful!";
    }
    else
    {
      echo 
    "Update failed!";
    }
    ?>
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  6. #6
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Defender1
    doc, can i just run that one query and have it update the value by 1?
    Well I'm not Dr LP, but yes that is how it works

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  7. #7
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by seanf
    Well I'm not Dr LP, but yes that is how it works
    that's fine.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR


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
  •