Link to home
Start Free TrialLog in
Avatar of Unspoken1
Unspoken1

asked on

Counter++;

Alright here is my code...


<?php
$username = "$_POST[loginname]";
$password = "$_POST[password]";
$conn=mysql_connect ("localhost", "asdf", "asdfasdf") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("cheatkol_users");
$sql = "SELECT username FROM users WHERE username='$username'";
$result = mysql_query($sql, $conn);
while ($userarray = mysql_fetch_array($result)){
$test = $userarray['username'];
$num = $userarray['counter'];
}
if($test!=""){
$counter++;
print"$counter";
$query="UPDATE users SET counter='$counter' WHERE username='$username'";
$result = mysql_query($query, $conn);
}


The problem I am having is that $num is returning blank....
I have counter in my mysql database, and it's value is already 1.
So what's up with it?
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

what about using $num instead of $counter (as you assign $num and not $counter)

<?php
$username = "$_POST[loginname]";
$password = "$_POST[password]";
$conn=mysql_connect ("localhost", "asdf", "asdfasdf") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("cheatkol_users");
$sql = "SELECT username FROM users WHERE username='$username'";
$result = mysql_query($sql, $conn);
while ($userarray = mysql_fetch_array($result)){
  $test = $userarray['username'];
  $num = $userarray['counter'];
}
if($test!=""){
$num++;
print"$counter";
$query="UPDATE users SET counter='$num' WHERE username='$username'";
$result = mysql_query($query, $conn);
}

Also: you don't query for the counter field:

<?php
$username = "$_POST[loginname]";
$password = "$_POST[password]";
$conn=mysql_connect ("localhost", "asdf", "asdfasdf") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("cheatkol_users");
$sql = "SELECT username , counter FROM users WHERE username='$username'";
$result = mysql_query($sql, $conn);
while ($userarray = mysql_fetch_array($result)){
  $test = $userarray['username'];
  $num = $userarray['counter'];
}
if($test!=""){
$num++;
print"$counter";
$query="UPDATE users SET counter='$num' WHERE username='$username'";
$result = mysql_query($query, $conn);
}
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Unspoken1
Unspoken1

ASKER

oooo ok thanks...and i changed it to $num at the last minute..it wasnt working before that