Jump to content

How to show mysql table data with an input field


AgentDuck
Go to solution Solved by AgentDuck,

Recommended Posts

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.

 

Link to comment
Share on other sites

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: 33bikpe.png

 

 

Database: amwo7l.png

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by AgentDuck
Link to comment
Share on other sites

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 by computermax2328
Link to comment
Share on other sites

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 by AgentDuck
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by AgentDuck
Link to comment
Share on other sites

 

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.

Link to comment
Share on other sites

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 by Barand
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.