[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Counter++;

Posted on 2006-05-31
4
Medium Priority
?
782 Views
Last Modified: 2013-12-12
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?
0
Comment
Question by:Unspoken1
  • 3
4 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16801898
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
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16801906
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
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 300 total points
ID: 16801912
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
 
LVL 2

Author Comment

by:Unspoken1
ID: 16801922
oooo ok thanks...and i changed it to $num at the last minute..it wasnt working before that
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month19 days, 11 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question