Jump to content

Help with PHP user search of MYsql Database


tabithaboof

Recommended Posts

I am quite new to PHP/MYsql and I have a site for high scores on a video game based around a simple MYsql database. something like this


username, level1, level2, level3
bob , 1000 , 2000 , 3000
kate , 2000 , 3000 , 4000

I am trying to create a form to let users query the database to find out all the scores for a particular player. I am doing this using an HTML input form:

<form method="post" action="scoreuser.php">

search form:<input type=text name='user' size=20 maxlength=20><input type=submit>

</form>

Based on what I have read "user" should be a variable I can use in the following script for the page "scoreuser.php"


$query = "select * from tbl WHERE username='user'";
$result = mysql_db_query("scoreattack", $query);

if ($result) {

while ($r = mysql_fetch_array($result)) {

$username = $r["username"];
$level1 = $r["level1"];


echo "
$username
$level1"

Unfotunately this doesnt seem to work at all, if I enter "bob" into my form and submit the query I get a blank page. However if I change this line:

$query = "select * from tbl WHERE username='user'";

to

$query = "select * from tbl WHERE username='bob'";

Than I can display bobs scores. I am assuming I am not connecting the form to "scoreuser.php" correctly or I am getting the syntax for the 'user' variable wrong somewhere.

I am sorry if this is a slightly long query but I wanted to be as clear as possible. Any help would be most greatly appreciated.

Link to comment
Share on other sites

[!--quoteo(post=371452:date=May 4 2006, 11:47 PM:name=tabithaboof)--][div class=\'quotetop\']QUOTE(tabithaboof @ May 4 2006, 11:47 PM) [snapback]371452[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I am quite new to PHP/MYsql and I have a site for high scores on a video game based around a simple MYsql database. something like this
username, level1, level2, level3
bob , 1000 , 2000 , 3000
kate , 2000 , 3000 , 4000

I am trying to create a form to let users query the database to find out all the scores for a particular player. I am doing this using an HTML input form:

<form method="post" action="scoreuser.php">

search form:<input type=text name='user' size=20 maxlength=20><input type=submit>

</form>

Based on what I have read "user" should be a variable I can use in the following script for the page "scoreuser.php"
$query = "select * from tbl WHERE username='user'";
$result = mysql_db_query("scoreattack", $query);

if ($result) {

while ($r = mysql_fetch_array($result)) {

$username = $r["username"];
$level1 = $r["level1"];
echo "
$username
$level1"

Unfotunately this doesnt seem to work at all, if I enter "bob" into my form and submit the query I get a blank page. However if I change this line:

$query = "select * from tbl WHERE username='user'";

to

$query = "select * from tbl WHERE username='bob'";

Than I can display bobs scores. I am assuming I am not connecting the form to "scoreuser.php" correctly or I am getting the syntax for the 'user' variable wrong somewhere.

I am sorry if this is a slightly long query but I wanted to be as clear as possible. Any help would be most greatly appreciated.
[/quote]

Close, but to get the variable from the form you must use $_POST, like the following:

[code]
$query = "SELECT * FROM tbl WHERE username='$_POST[user]'";
[/code]
Link to comment
Share on other sites

[!--quoteo(post=371459:date=May 5 2006, 01:08 AM:name=Prismatic)--][div class=\'quotetop\']QUOTE(Prismatic @ May 5 2006, 01:08 AM) [snapback]371459[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Close, but to get the variable from the form you must use $_POST, like the following:

[code]
$query = "SELECT * FROM tbl WHERE username='$_POST[user]'";
[/code]
[/quote]


Thank you SO much, you have no idea how long I have been fiddling with this with no luck.
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.