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?
LVL 2
Unspoken1Asked:
Who is Participating?
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
Finally: if you only want to update the counter in the table, you don't need to retrieve it:

<?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 = "UPDATE users SET counter=counter + 1 WHERE username='$username' ";
mysql_query($sql, $conn);

?>
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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);
}

0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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);
}
0
 
Unspoken1Author Commented:
oooo ok thanks...and i changed it to $num at the last minute..it wasnt working before that
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.