|
-
Jan 8th, 2011, 06:10 PM
#1
Thread Starter
Fanatic Member
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
-
Jan 9th, 2011, 05:23 AM
#2
Thread Starter
Fanatic Member
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
-
Jan 9th, 2011, 11:47 PM
#3
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?
-
Jan 10th, 2011, 05:12 PM
#4
Junior Member
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...
Last edited by west158; Jan 10th, 2011 at 05:22 PM.
-
Jan 10th, 2011, 06:36 PM
#5
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.
-
Jan 10th, 2011, 11:27 PM
#6
Thread Starter
Fanatic Member
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());
}
?>
-
Jan 11th, 2011, 12:54 AM
#7
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());
}
}
?>
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Jan 11th, 2011, 03:40 AM
#8
Thread Starter
Fanatic Member
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
-
Jan 11th, 2011, 06:31 AM
#9
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))
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Jan 11th, 2011, 07:32 AM
#10
Thread Starter
Fanatic Member
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
-
Jan 11th, 2011, 06:49 PM
#11
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.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Jan 11th, 2011, 11:23 PM
#12
Thread Starter
Fanatic Member
Re: Php If statement with mySql?
Hi Mate,
What do you mean exactly?
Thanks,
Jamie
-
Jan 12th, 2011, 12:46 AM
#13
Re: Php If statement with mySql?
 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.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|