Php If statement with mySql?
Hi guys, I have a database in mysql called 'Vital_vip' - Inside that is a table called 'users', In the users table it contains the usernames, passwords e.t.c of each user on my site.
It also has bit called 'Points' - Which displays users points (for my points system)
I am wondering how in php i can tell it that if the number of points in the points part for the current user logged in is below 100 - then it will display the text :
You dont have enough points
-
Hope that makes sense?
Thanks,
Jamie
Re: Php If statement with mySql?
Hi guys, Also what would i put so that, when a user clicks a submit button - it checks the row in the users table - checks the points part for that user,
and if the users points are below 100 - it will echo - unfortunatly you dont have enough points
Thanks,
Jamie
Re: Php If statement with mySql?
do you have any code that you're currently working with? do you know how to connect to a MySQL database with PHP?
Re: Php If statement with mySql?
To display and check if the user has <= (less or equal to) 100 points this is what i'd do:
Connect to database -> Select points from the username/email/nickname or whatever ->if username/email/nickname or whatever's points is less than or equal to 100 points, display the text.
Code:
<?php
$mysql_con=mysql_connect("localhost","username","password"); //connect to mysql databasse
$use_db("database");
//can do verification if the above exist's/connection is successful
$get_username=mysql_query("SELECT points FROM users-table WHERE username ='$username'");
while($row=mysql_fetch_array($get_username)
{
$points=$row['points'];
}
if($points<=100)
{
echo "You do not have 100 points, you have " .$points ." points";
}
?>
that should work, if it doesn't it may need a bit of tweaking, but that's the general gist of what you'd need to do / that's what i'd do.
$username could be $_SESSION['userid'], $_GET/POST['userid'],username,email or anything unique so it doesn't get two of the same record.
If this doesn't work, let me know and i'll correct it. admin/mod feel free to edit this post if this doesn't work...
Re: Php If statement with mySql?
This is very basic stuff. I really recommend reading some tutorials, or a book, because it sounds like you're still struggling with fundamentals.
Re: Php If statement with mySql?
Thanks for the code mate! I was wondering if you could help me merge the codes together, so that if it is below 100, then it will echo the error message,
Code:
<?php
$mysql_con=mysql_connect("localhost","username","password"); //connect to mysql databasse
$use_db("database");
//can do verification if the above exist's/connection is successful
$get_username=mysql_query("SELECT points FROM users-table WHERE username ='$username'");
while($row=mysql_fetch_array($get_username)
{
$points=$row['points'];
}
if($points<=100)
{
echo "You do not have 100 points, you have " .$points ." points";
}
?>
And if they have above 100, it will do the following :
Code:
<?php
if($_POST['doSave'] == 'Buy This Item')
{
// Filter POST data for harmful code (sanitize)
foreach($_POST as $key => $value) {
}
mysql_query("UPDATE users SET
points=points-100
WHERE id='$_SESSION[user_id]'
") or die(mysql_error());
}
?>
Re: Php If statement with mySql?
This might be a stupid idea but you could use a else condition something like:
Code:
<?php
$mysql_con=mysql_connect("localhost","username","password"); //connect to mysql databasse
$use_db("database");
//can do verification if the above exist's/connection is successful
$get_username=mysql_query("SELECT points FROM users-table WHERE username ='$username'");
while($row=mysql_fetch_array($get_username)
{
$points=$row['points'];
}
if($points<=100)
{
echo "You do not have 100 points, you have " .$points ." points";
}else{
if($_POST['doSave'] == 'Buy This Item')
{
// Filter POST data for harmful code (sanitize)
foreach($_POST as $key => $value) {
}
mysql_query("UPDATE users SET
points=points-100
WHERE id='$_SESSION[user_id]'
") or die(mysql_error());
}
}
?>
Re: Php If statement with mySql?
Hi thanks for that! How ever I am getting the error :
Parse error: syntax error, unexpected '{' in /home/vital/public_html/scripts/points/buy/500.php on line 7
it seems to be to do with this part :
Code:
{
$points=$row['points'];
}
Thanks,
Jamie
Re: Php If statement with mySql?
Try replacing:
Code:
while($row=mysql_fetch_array($get_username)
with
Code:
while($row=mysql_fetch_array($get_username))
Re: Php If statement with mySql?
Hi mate,
Unfortunatly - its now giving me the error ' Fatal error: Function name must be a string in /home/vital/public_html/scripts/points/buy/500.php on line 3'
Thanks,
Jamie
Re: Php If statement with mySql?
Jamie,
Is it possible for you to upload a script for our to recreate your empty database? This would make it easier to test for errors in the php.
Re: Php If statement with mySql?
Hi Mate,
What do you mean exactly?
Thanks,
Jamie
Re: Php If statement with mySql?
Quote:
Originally Posted by
JamieWarren09
Hi Mate,
What do you mean exactly?
Thanks,
Jamie
I'm asking whether or not it is possible for you to either upload a test database or a database creation script? This is so people helping you could run the php as you are doing and see what is causing the problem.