Results 1 to 13 of 13

Thread: Php If statement with mySql?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    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

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    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

  3. #3
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    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?
    Like Archer? Check out some Sterling Archer quotes.

  4. #4
    Junior Member
    Join Date
    Mar 2009
    Posts
    16

    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.

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    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());
    
    
    
    
     }
     
    ?>

  7. #7
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    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

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    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

  9. #9
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    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

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    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

  11. #11
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    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

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Re: Php If statement with mySql?

    Hi Mate,

    What do you mean exactly?

    Thanks,
    Jamie

  13. #13
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Php If statement with mySql?

    Quote Originally Posted by JamieWarren09 View Post
    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
  •  



Click Here to Expand Forum to Full Width