AgentDuck Posted April 23, 2013 Share Posted April 23, 2013 Hello people, i've stumbled across a problem while learning that i can't seem to solve. In my MySQL database tables i have "name" and "coins" and on my page i have an input box, where you type a name, and the site should show how many coins he has. The problem is i can't find out how to that, and i would really appreciate if some of you in here could help me.Thanks in advance. Quote Link to comment Share on other sites More sharing options...
computermax2328 Posted April 23, 2013 Share Posted April 23, 2013 If you want to do it without reloading the page, like how Google does its search, then you are going to need to use Javascript. If you want to use PHP you are going to have to create a process form for when you input the name and then submit it to the server. Quote Link to comment Share on other sites More sharing options...
AgentDuck Posted April 23, 2013 Author Share Posted April 23, 2013 I dont think you get the point. On my php site i have an input field. In that field you should be able to type a name, then it will return how many coins he has. I have hes name and how many coins he has stored in my database. Input field: Database: Quote Link to comment Share on other sites More sharing options...
computermax2328 Posted April 23, 2013 Share Posted April 23, 2013 Ok, so if you are going to do it that way, when the user clicks "Check Balance" you are going to want to have that form that the input and submit button are in pass the information to a process page to query the database and echo out how many coins that person has. You can do a single page submission, which would have this page query the database or create a process page. Can you post the HTML form that you are using so I can help you further? Quote Link to comment Share on other sites More sharing options...
AgentDuck Posted April 23, 2013 Author Share Posted April 23, 2013 (edited) Hello this is the basic form thats basicly just a input field and a button, and yes you got it right. <form method="post" action="<?php echo $PHP_SELF;?>"> Ingame name:<input type="text" size="12" maxlength="12" name="coins"><br /> <input type="submit" value="Check Balance" name="submit"><br /> </form><br /> <?php From here i need it to take that name and then look through the database and find the amount of coins the desired user has. I this what you meant by the form? This is the mysql thingy the problem is, that this just takes the first user and shows hes coins, and i cant seem to find out how to make it like i want. <?php // Make a MySQL Connection mysql_connect("localhost", "root", "******") or die(mysql_error()); mysql_select_db("coinsdb") or die(mysql_error()); // Retrieve all the data from the "example" table $result = mysql_query("SELECT * FROM users") or die(mysql_error()); // store the record of the "example" table into $row $row = mysql_fetch_array( $result ); // Print out the contents of the entry echo "Coins: ".$row['coins']; ?> Edited April 23, 2013 by AgentDuck Quote Link to comment Share on other sites More sharing options...
computermax2328 Posted April 23, 2013 Share Posted April 23, 2013 (edited) Ok! To start, I had never seen "$PHP_SELF" before, so I Googled it and found that you have to use <?php $_SERVER['PHP_SELF'] ?>. Apparently, "PHP_SELF" does not work in php 5 or greater. Since you are doing single page submission at the very top of your page with the form on it you are going to do something like this... //Connect to database if($_POST['submit']){ $name = mysql_escape_real_string($_GET['coins']); $sql = "SELECT `coins` FROM tablename WHERE `name`=$name"; $query = mysql_query($sql, $connection); while ($row = mysql_fetch_array($query)) { $amount = $row['coins']; } } Then you can echo the variable $amount on the page where ever you want it. This is my 200th post by the way. Wooo! Milestone! Edited April 23, 2013 by computermax2328 Quote Link to comment Share on other sites More sharing options...
AgentDuck Posted April 23, 2013 Author Share Posted April 23, 2013 (edited) hmmm im getting a strange problem Notice: Undefined index: submit in C:\xampp\htdocs\Login.php on line 12 im just gonna give you all the code dunno if i messed up somewhere o.O <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Login</title> </head> <?php mysql_connect("localhost", "root", "*******") or die(mysql_error()); mysql_select_db("coinsdb") or die(mysql_error()); //Connect to database if($_POST['submit']){ $name = mysql_escape_real_string($_GET['coins']); $sql = "SELECT `coins` FROM tablename WHERE `name`=$name"; $query = mysql_query($sql, $connection); while ($row = mysql_fetch_array($query)) { $amount = $row['coins']; } } ?> <form method="post" action="<?php echo $PHP_SELF;?>"> Ingame name:<input type="text" size="12" maxlength="12" name="coins"><br /> <input type="submit" value="Check Balance" name="submit"><br /> </form><br /> </body> </html> Edited April 23, 2013 by AgentDuck Quote Link to comment Share on other sites More sharing options...
computermax2328 Posted April 23, 2013 Share Posted April 23, 2013 My bad use isset() if(isset($_POST['submit'])){ } Also in the query where it says table name put your table name. I just want to throw this out there that this is really sloppy code. A lot of stuff on this page is not best practices. I suggest that you continue to practice like you are now so that you can learn why things like putting your MYSQL connect on an HTML page is not a best practice. I am trying to guide you along here and help you troubleshoot. Not give you the answer, even though I kind of already did. You just need to figure out the rest. Let me know what happens Quote Link to comment Share on other sites More sharing options...
AgentDuck Posted April 23, 2013 Author Share Posted April 23, 2013 (edited) Hello your help is very appreciated in my process of learning stuff. Yes im gonna experiment putting the mysql connecter in another file. Anyways. When i press submit im getting Access forbidden! You don't have permission to access the requested object. It is either read-protected or not readable by the server. If you think this is a server error, please contact the webmaster. that error comes up. i dont really see why. I know that im making a big beginner fail, but thats how you learn huh? code look like this <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Login</title> </head> <?php mysql_connect("localhost", "root", "******") or die(mysql_error()); mysql_select_db("coinsdb") or die(mysql_error()); //Connect to database if(isset($_POST['submit'])){ $name = mysql_escape_real_string($_GET['coins']); $sql = "SELECT `coins` FROM users WHERE `name`=$name"; $query = mysql_query($sql, $connection); while ($row = mysql_fetch_array($query)) { $amount = $row['coins']; } echo $amount; } ?> <form method="post" action="<?php echo $PHP_SELF;?>"> Ingame name:<input type="text" size="12" maxlength="12" name="coins"><br /> <input type="submit" value="Check Balance" name="submit"><br /> </form><br /> </body> </html> Edited April 23, 2013 by AgentDuck Quote Link to comment Share on other sites More sharing options...
AgentDuck Posted April 23, 2013 Author Share Posted April 23, 2013 Hello your help is very appreciated in my process of learning stuff. Yes im gonna experiment putting the mysql connecter in another file. Anyways. When i press submit im getting Access forbidden! You don't have permission to access the requested object. It is either read-protected or not readable by the server. If you think this is a server error, please contact the webmaster. that error comes up. i dont really see why. I know that im making a big beginner fail, but thats how you learn huh? code look like this <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Login</title> </head> <?php mysql_connect("localhost", "root", "******") or die(mysql_error()); mysql_select_db("coinsdb") or die(mysql_error()); //Connect to database if(isset($_POST['submit'])){ $name = mysql_escape_real_string($_GET['coins']); $sql = "SELECT `coins` FROM users WHERE `name`=$name"; $query = mysql_query($sql, $connection); while ($row = mysql_fetch_array($query)) { $amount = $row['coins']; } echo $amount; } ?> <form method="post" action="<?php echo $PHP_SELF;?>"> Ingame name:<input type="text" size="12" maxlength="12" name="coins"><br /> <input type="submit" value="Check Balance" name="submit"><br /> </form><br /> </body> </html> I also think i placed the "echo" quite wrong. But if i didnt place it there, it would say that it coulden't find the variable. Your help is appreciated a lot anyways. Quote Link to comment Share on other sites More sharing options...
Barand Posted April 23, 2013 Share Posted April 23, 2013 (edited) I don't know what causes the access error, but there are three four other changes required You use form method POST but you are trying to use $_GET['coins'] $name is a string value and so should be quoted in the query ie name = '$name' As you are retrieving a single row you do not need the while loop. Just fetch the row. You don't store the connection in any variable but you later try to use $connection (not needed for mysql as it will use the current connection) Edited April 23, 2013 by Barand Quote Link to comment Share on other sites More sharing options...
computermax2328 Posted April 24, 2013 Share Posted April 24, 2013 Thanks for picking up my errors man. I just typed it up real quick so the person could see an example. I didn't think that they were going to want an exact solution. Just trying to give him a push. The $connection thing was my bad. Something I do in my code so I did it naturally. Quote Link to comment Share on other sites More sharing options...
Solution AgentDuck Posted April 24, 2013 Author Solution Share Posted April 24, 2013 Fixed it myself. Found another form, and played around with the code you provided me. Thanks alot for your help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.