NameSilo

Simple MySQL question

Spaceship Spaceship
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
•••
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
•••
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
•••
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
•••
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
•••
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
•••
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
•••
Dynadot — .com Registration $8.99Dynadot — .com Registration $8.99

We're social

Unstoppable Domains
Domain Recover
DomainEasy — Live Options
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back