Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    mysql pho Leaderboard help

    I am trying to write a script in which users can go to my site and update the leaderboard.

    Need to have the option of adding new people and updating people.

    also need the database to display in a column on the site. and ordered in a decending order by players points.

    i am having a lot of trouble with the script.

    Can anyone help?

  • #2
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Post your script and let us know the specific problems you're having

  • #3
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    this is the code for the form i am using.

    Code:
    <form action="leaderboard.php" mehtod="POST" />
      <p><br />
        <br />
        <br />
      </p>
      <table width="100%" border="0" cellspacing="0" cellpadding="2">
        <tr>
          <td><div align="right">Points: </div></td>
          <td><input type="text" name="points"/></td>
        </tr>
        <tr>
          <td><div align="right">First Name: </div></td>
          <td><input type="text" name="name"/></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td><input type="submit" value="Submit" /></td>
        </tr>
      </table>
      <p></p>
      <p>&nbsp;</p>

    this is the code for the script.

    Code:
    <?PHP
    
    define('db_name', 'webadmin_leaderboard');
    define('db_user', 'webadmin');
    define('db_password', 'collide198');
    define('db_host', "localhost");
    
    $link = mysql_connect(db_host, db_user, db_password);
    
    if (!$link)	{
    	die('Could Not Connect: ' . mysql_error());
    }
    
    $db_selected = mysql_select_db(db_name, $link);
    
    if (!$db_selected)	{
    		die('can\'t use' . db_name . ': ' . mysql_error());
    }
    
    echo 'Connected Successfully';
    
    	
    
    $Points = $_POST[`points`];
    
    $sql = "INSERT INTO leaderboard (`points`) VALUES ('$points')";
    
    $name = $_POST[`name`];
    
    $sql = "INSERT INTO leaderboard (`name`) VALUES ('$name')";
    
    
    
    if (!mysql_query($sql))	{
    		die('Error: ' . mysql_error());
    }
    
    mysql_close();
    		
    ?>
    right now the problem i am having is when i use the form to insert information, it does not insert the actual information, it starts a new line but only puts '0' in the fields.

    also, i am not sure how to write the code to display the database on the site in decending order by the "points" column.

    I also need to write a code for a form or edit the original form to "update" an existing players points without getting the "duplicate entry" error message.

    in the database i have 3 columns.

    ID - INT - AI - Index
    points - int - primary
    name - varchar - unique
    Last edited by elcubanoluis; 09-15-2011 at 06:55 AM.

  • #4
    Regular Coder
    Join Date
    Sep 2011
    Posts
    128
    Thanks
    2
    Thanked 21 Times in 21 Posts
    The scripts puts a ´0´ because PHP is case sensative
    PHP Code:
    $points 3;
    echo 
    $Points;
    // Returns: FALSE (False is also a '0') 
    Change this piece of code:
    PHP Code:
    $Points $_POST[`points`];

    $sql "INSERT INTO leaderboard (`points`) VALUES ('$points')"
    to:
    PHP Code:
    $points $_POST[`points`];

    $sql "INSERT INTO leaderboard (`points`) VALUES ('".$points."')"
    and it should work

    and i'm guessing this should't work, because you overwrite the variable:
    PHP Code:
    $Points $_POST[`points`];

    $sql "INSERT INTO leaderboard (`points`) VALUES ('$points')";

    $name $_POST[`name`];

    $sql "INSERT INTO leaderboard (`name`) VALUES ('$name')"
    Change that to:
    PHP Code:
    $points $_POST[`points`];

    $name $_POST[`name`];

    $sql "INSERT INTO leaderboard (`name`,`points`) VALUES ('".$name."','".$points."')"
    And you database table isn't right either.
    Because a primary key can only exists once in the table.
    You set the column 'points' as primary key, but there can be a name with the same amount of points and in the current setting this won't work.

    To get things out of database use this:
    PHP Code:
    $sql "SELECT * FROM leaderboard ORDER BY `points` DESC";
    $result mysql_query($sql);
    while(
    $row mysql_fetch_object($result))
    {
     
    // Do somestuff with this:
     // To get something: $row->column_name
     // They code inside here will be excecuted as many times as there are rows in the database.

    Last edited by Wanna; 09-15-2011 at 07:51 AM. Reason: Adding some code

  • #5
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I changed the code like you suggested,

    using the following:

    Form:

    Code:
    <form action="leaderboard.php" mehtod="POST" />
      <table width="100%" border="0" cellspacing="0" cellpadding="2">
        <tr>
          <td><div align="right">Points: </div></td>
          <td><input type="text" name="Points"/></td>
        </tr>
        <tr>
          <td><div align="right">Name: </div></td>
          <td><input type="text" name="Name"/></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td><input type="submit" value="Submit" /></td>
        </tr>
      </table>
      <p><br />
        <br />
        <br />
      </p>
      <p></p>
      <p>&nbsp;</p>
        <br />
      </p>
      <p></p>
      <p>&nbsp;</p>
    </body>
    </html>
    and script:


    PHP Code:
    <?PHP

    define
    ('db_name''webadmin_leaderboard');
    define('db_user''webadmin');
    define('db_password''collide198');
    define('db_host'"localhost");

    $link mysql_connect(db_hostdb_userdb_password);

    if (!
    $link)    {
        die(
    'Could Not Connect: ' mysql_error());
    }

    $db_selected mysql_select_db(db_name$link);

    if (!
    $db_selected)    {
            die(
    'can\'t use' db_name ': ' mysql_error());
    }

    echo 
    'Connected Successfully';

        

    $name $_POST[`Name`];

    $points $_POST[`Points`];

    $sql "INSERT INTO leaderboard (`name`,`points`) VALUES ('".$name."','".$points."')";  


    if (!
    mysql_query($sql))    {
            die(
    'Error: ' mysql_error());
    }

    mysql_close();
            
    ?>
    I also edited the table so that the id was primary, the name was unique and the points was blank.

    still am having the same problem.

    I am willing to give FTP access to anyone who can fix it.

    I can also give temp access to the cpanel to adjust the database...

    Really need this fixed. its driving me insane. i have followed all the steps on w3schools and various tut videos....

    AHHH!!!

  • #6
    Regular Coder
    Join Date
    Sep 2011
    Posts
    128
    Thanks
    2
    Thanked 21 Times in 21 Posts
    If you want you can send me the FTP data in a PM

    Then i can help you

    Also you could try this first:

    Change this two lines
    PHP Code:
    $name $_POST[`Name`]; 

    $points $_POST[`Points`]; 
    to
    PHP Code:
    $name $_POST['Name']; 

    $points $_POST['Points']; 
    Last edited by Wanna; 09-16-2011 at 10:23 AM. Reason: Adding some code


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •