Simple MySQL question

Spacemail by SpaceshipSpacemail by Spaceship
SpaceshipSpaceship
SpaceshipSpaceship
Watch

DomainSage

Established Member
Impact
23
Hi Folks,

I know this is pretty basic, but I can't seem to work it out :) Not really done any MySQL programming before.

Basically all it needs to do is increase a number held in a table by one, at the moment I have :

Code:
$db = mysql_connect("localhost", "bargaintraffic", "password here");
mysql_select_db("bargaintraffic",$db);
$result = mysql_query("SELECT * FROM test",$db);
$traffic = mysql_result($result,$unid,"intraffic");
$traffic ++;
$sql = "UPDATE test SET intraffic = '$traffic' WHERE id = $unid";

'test' is the table.
$unid is passed into the page.

It reads the value for intraffic correctly and $traffic is incremented, but does not write the new value to the database.

I know it is probably something really basic I am missing, but have searched the net without finding anything.

Hope someone can help :)

All the best.

Richard
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
add

mysql_query($sql) or die(mysql_error());

to enter it into the database, and if it doesn't work it will give you an error telling you why. You could also try putting ' ' around the id after WHERE, I normally quote everything just to be safe =P.

Final Code:

$sql = "UPDATE test SET intraffic = '" . $traffic . "' WHERE id = '" . $unid . "'";
mysql_query($sql) or die(mysql_error());
 
0
Close
Hi Ryan,

Thanks for the suggestions, tried them ( and various combinations :)) but still not working. Strange thing is that it does not report an errors either.

Something that just struck me .... If I change the update line to write to another field then that works fine, so my question is .... When it reads from the database ( intraffic field ) does that field become 'locked', if so does it need to be released before writting to it ?

As I say I am new to MySQL :)

All the best.

Richard
 
0
Close
mysql_query("UPDATE test SET intraffic=(intraffic + 1) WHERE id='$unid'");

See how that works, I use it on some of my websites for a counter.
 
1
Close
Thanks again, but still did not work ... However have worked out what was wrong :)

Just changed :
$traffic = mysql_result($result,$unid,"intraffic");
to :
$traffic = mysql_result($result,($unid-1),"intraffic");

Works fine now :)

Thanks for you help, rep left.

All the best.

Richard
 
Last edited:
0
Close
unless you are using anything else from the first query then there is no need for 2 queries. If that is the case then the suggestion by NonProphet would benefit you the most.
 
0
Close
Hi Peter,

Thanks for the reply, I'm still working on scripts for the site so am just getting it working for the moment. When I have finished I'll go through it and tidy it up a bit :) I know it is the wrong way of programming but am really just having fun :)

All the best.

Richard
 
0
Close
AIVikingsAIVikings

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Switch between accounts
You can now stay logged in to more than one NamePros account in the same browser. Click your avatar, then "Accounts in this browser", to...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back