[SOLVED] MySQL Update Query Prob

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
RealmX
Forum Newbie
Posts: 2
Joined: Wed Apr 07, 2004 7:35 pm

[SOLVED] MySQL Update Query Prob

Post by RealmX »

I've been having a terrible time with this update code for MySQL. I've searched all over the internet, looked up examples, and it still doesn't help me. The problem I'm having is that my variable $count does not get updated in the database query.

Code: Select all

$id=$_GET['id'];
mysql_connect(localhost,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query="SELECT * FROM links";
$result=mysql_query($query);

$count=mysql_result($result,($id),"count");
$count++;

$query = "UPDATE links SET count='$count' WHERE id = $id";
mysql_query($query);
echo mysql_error();
mysql_close();
Any help would be greatly appreciated as I'm quite new to PHP and MySQL.
?>[/php_man]
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Try..

Code: Select all

$db = mysql_connect('localhost',$user,$password) or die(mysql_error());
mysql_select_db($database) or die('Unable to select database');

$query = "UPDATE links SET count=count+1 WHERE id = {$_GET['id']}";
mysql_query($query) or die(mysql_error());
mysql_close($db);
RealmX
Forum Newbie
Posts: 2
Joined: Wed Apr 07, 2004 7:35 pm

Post by RealmX »

That worked very well, thanks :D
Post Reply