...

View Full Version : Updating an SQL Table and Outputting



a4udi
05-07-2006, 06:34 AM
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.


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.



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? :confused:

Any help is greatly appreciated!

cdwhalley.com
05-07-2006, 12:16 PM
You should give columns names.
In the first query it should be:


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


$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>";

a4udi
05-07-2006, 03:47 PM
Thanks, it's outputting the correct value now in place of the variables! :thumbsup:

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. :p

guelphdad
05-07-2006, 03:54 PM
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?

a4udi
05-07-2006, 04:39 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum