Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jan 2005
    Posts
    189
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Updating an SQL Table and Outputting

    Hi,

    I'm trying to make a page that will update prices but I'm having trouble with 2 things. One is I have written some code that will insert the information into the database (which works), but I want to update it 'overwrite' each time instead of add a new row.

    Code:
      mysql_select_db('****');
      $query = "insert into rates values
    	('".$rate01."', '".$rate02."', '".$rate03."', '".$rate04."', '".$rate05."', '".$rate06."')";
      $result = mysql_query($query);
      if ($result)
    	echo "<b>Prices have been Updated.</b>";
    The second issue is after the user inputs these values, how to retreive them and print them out on a new page... So far I have this.

    Code:
      mysql_select_db('***');
      $query01 = "select rate01 from rates";
      $result01 = mysql_query($query01);
    
    // Output the HTML with SQL values
    
    	echo "<span class='body'>Price: $result01</span></p>";
    The Problem is, instead of printing "Price: $4.00" or whatever $result01 is, it's printing "Price: Resource id #3" instead. Anyone know why this is?

    Any help is greatly appreciated!

  • #2
    New Coder
    Join Date
    Apr 2006
    Location
    UK
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You should give columns names.
    In the first query it should be:
    PHP Code:
    $query "insert into rates (rate01, rate02, rate03, rate04, rate05, rate06) values('".$rate01."', '".$rate02."', '".$rate03."', '".$rate04."', '".$rate05."', '".$rate06."')"
    Secondly, you haven't got the data out of your result.
    PHP Code:
    $query01 "select rate01 from rates";
    $result01 mysql_query($query01);
    $row01 mysql_fetch_array($result01);
    list(
    $rate01) = $row01;

    echo 
    "<span class='body'>Price: $rate01</span></p>"
    Last edited by cdwhalley.com; 05-07-2006 at 11:18 AM.

  • #3
    Regular Coder
    Join Date
    Jan 2005
    Posts
    189
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thanks, it's outputting the correct value now in place of the variables!

    The only problem I have left is that first part though. It's still adding a row to the SQL table instead of updating (replacing) the current values. I've searched everywhere and can't seem to find a good example of how to do this.

    It's this part here...
    $query = "INSERT into rates (rate01, rate02, rate03, rate04, rate05) VALUES('".$rate01."', '".$rate02."', '".$rate03."', '".$rate04."', '".$rate05."'')";
    shouldn't it be UPDATE into rates... or something like that? I can't figure this out.
    Last edited by a4udi; 05-07-2006 at 02:51 PM.

  • #4
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,634
    Thanks
    4
    Thanked 148 Times in 139 Posts
    Quote Originally Posted by a4udi
    I'm trying to make a page that will update prices but I'm having trouble with 2 things. One is I have written some code that will insert the information into the database (which works), but I want to update it 'overwrite' each time instead of add a new row.
    An INSERT does exactly that, insert data into your table. If you want to update then you need an UPDATE statement right?

  • #5
    Regular Coder
    Join Date
    Jan 2005
    Posts
    189
    Thanks
    6
    Thanked 0 Times in 0 Posts
    that's right... I just can't get the syntax right i guess and haven't found a good example that's helped me yet.

    Nevermind actually, I just figured it out. was missing a stupid apostrophe somewhere. dumb mistakes.
    Last edited by a4udi; 05-07-2006 at 03:48 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •