ckerr27 Posted April 3, 2012 Share Posted April 3, 2012 Hi, I have created a form (code below) to show details about members of my website. When the user enters the username of a certain member the form should retrieve these details from my database(phpmyadmin) and display them. I cant get this to work. Here is the code for my form: <form id="form1" name="form1" method="post" action="getdetails.php"> username <input type="text" name="textfield" value ='' /> <input type="submit" name="Get Details" value="Get Details" /> </label> </p> </form> Here is my getdetails.php file <?php mysql_connect ("localhost","root",""); mysql_select_db ("test"); $sql = "select * from memberdetails"; $result = mysql_query ($sql); while ($row = mysql_fetch_array($result)) { $username= $row["username"]; $firstname= $row["firstname"]; $surname= $row["surname"]; $dob= $row["dob"]; $totalwins= $row["totalwins"]; $totalloses= $row["totalloses"]; $email= $row["email"]; $country= $row["country"]; $info= $row["info"]; echo "<b><u>Username:</b></u> $username<br>"; echo "<b><u>Firstname:</b></u> $firstname<br>"; echo "<b><u>Surname: </b> </u> $surname<br>"; echo "<b><u>Date of Birth:</b></u> $dob<br>"; echo "<b><u>Total Chess Wins:</b></u> $totalwins<br>"; echo "<b><u>Total Chess loses:</b></u> $totalloses<br>"; echo "<b><u>Email Address: </b></u> $email<br>"; echo "<b><u>Born in: </b></u> $country<br>"; echo "<b><u>Other Details:</b></u> $info<br>"; } ?> The above code displays all the users' details from the table not just the one which was typed in. Thanks for any help! Quote Link to comment Share on other sites More sharing options...
smerny Posted April 3, 2012 Share Posted April 3, 2012 you are doing select * from memberdetails what do you expect it to do? add a "WHERE" SELECT * FROM memberdetails WHERE username='$username' you should rename your username text field to "username" because "textfield" for a name is pretty ambiguous.. then you capture that with "$_POST['username'].... then prevent sql injection by something like $username = mysql_escape_real_string($_POST['username']); then run the sql like i posted above Quote Link to comment Share on other sites More sharing options...
Drummin Posted April 3, 2012 Share Posted April 3, 2012 A little trim wouldn't hurt as well. <?php if (isset($_POST['Get Details'])){ $username=mysql_real_escape_string(trim($_POST['textfield'])); $sql = "select * from memberdetails WHERE username='$username'"; $result = mysql_query ($sql); while ($row = mysql_fetch_array($result)) { $username= $row["username"]; $firstname= $row["firstname"]; $surname= $row["surname"]; $dob= $row["dob"]; $totalwins= $row["totalwins"]; $totalloses= $row["totalloses"]; $email= $row["email"]; $country= $row["country"]; $info= $row["info"]; echo "<b><u>Username:</b></u> $username<br>"; echo "<b><u>Firstname:</b></u> $firstname<br>"; echo "<b><u>Surname: </b> </u> $surname<br>"; echo "<b><u>Date of Birth:</b></u> $dob<br>"; echo "<b><u>Total Chess Wins:</b></u> $totalwins<br>"; echo "<b><u>Total Chess loses:</b></u> $totalloses<br>"; echo "<b><u>Email Address: </b></u> $email<br>"; echo "<b><u>Born in: </b></u> $country<br>"; echo "<b><u>Other Details:</b></u> $info<br>"; } }//if (isset($_POST['Get Details'])) ?> Quote Link to comment Share on other sites More sharing options...
ckerr27 Posted April 3, 2012 Author Share Posted April 3, 2012 I have edited my form to look as follows <form name="form1" method="post" action="getdetails.php"> <td> <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> <tr> <td colspan="3"><strong><center>Personal Details</center> </strong></td> </tr> <tr> <td width="78">Username</td> <td width="6">:</td> <td width="294"><input name="username" type="text" id="username"></td> </tr> <tr> <td> </td> <td> </td> <td><input type="submit" name="Submit" value="Get Details"></td> </tr> </table> </td> </form> The getdetails.php is this now: <?php mysql_connect ("localhost","root",""); mysql_select_db ("test"); if (isset($_POST['Get Details'])){ $username=mysql_real_escape_string(trim($_POST['username'])); $sql = "select * from memberdetails WHERE username='$username'"; $result = mysql_query ($sql); while ($row = mysql_fetch_array($result)) { $username= $row["username"]; $firstname= $row["firstname"]; $surname= $row["surname"]; $dob= $row["dob"]; $totalwins= $row["totalwins"]; $totalloses= $row["totalloses"]; $email= $row["email"]; $country= $row["country"]; echo "<b><u>Username:</b></u> $username<br>"; echo "<b><u>Firstname:</b></u> $firstname<br>"; echo "<b><u>Surname: </b> </u> $surname<br>"; echo "<b><u>Date of Birth:</b></u> $dob<br>"; echo "<b><u>Total Chess Wins:</b></u> $totalwins<br>"; echo "<b><u>Total Chess loses:</b></u> $totalloses<br>"; echo "<b><u>Email Address: </b></u> $email<br>"; echo "<b><u>Born in: </b></u> $country<br>"; echo "<b><u>Other Details:</b></u> $info<br>"; }} ?> It is simply returning an empty page?? Quote Link to comment Share on other sites More sharing options...
NLT Posted April 3, 2012 Share Posted April 3, 2012 I noticed from your latest code you have changed "Get Details" to "Submit". Try this: <?php mysql_connect ("localhost","root",""); mysql_select_db ("test"); if (isset($_POST['Submit'])){ $username=mysql_real_escape_string(trim($_POST['username'])); $sql = "select * from memberdetails WHERE username='$username'"; $result = mysql_query ($sql); while ($row = mysql_fetch_array($result)) { $username= $row["username"]; $firstname= $row["firstname"]; $surname= $row["surname"]; $dob= $row["dob"]; $totalwins= $row["totalwins"]; $totalloses= $row["totalloses"]; $email= $row["email"]; $country= $row["country"]; echo "<b><u>Username:</b></u> $username<br>"; echo "<b><u>Firstname:</b></u> $firstname<br>"; echo "<b><u>Surname: </b> </u> $surname<br>"; echo "<b><u>Date of Birth:</b></u> $dob<br>"; echo "<b><u>Total Chess Wins:</b></u> $totalwins<br>"; echo "<b><u>Total Chess loses:</b></u> $totalloses<br>"; echo "<b><u>Email Address: </b></u> $email<br>"; echo "<b><u>Born in: </b></u> $country<br>"; echo "<b><u>Other Details:</b></u> $info<br>"; }} ?> Quote Link to comment Share on other sites More sharing options...
ckerr27 Posted April 3, 2012 Author Share Posted April 3, 2012 Thanks for noticing, but this still will not work, it is not returning an error, simply returning an empty screen? Thanks Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 3, 2012 Share Posted April 3, 2012 If your query does not fail and the submit button doesn't match, that is correct behavior with the code I see. Try adding print_r($_POST); Also... | | V Quote Link to comment Share on other sites More sharing options...
ckerr27 Posted April 3, 2012 Author Share Posted April 3, 2012 Do you mean as follows? Sorry this is my first time using PHP while ($row = mysql_fetch_array($result)) { $username= $row["username"]; $firstname= $row["firstname"]; $surname= $row["surname"]; $dob= $row["dob"]; $totalwins= $row["totalwins"]; $totalloses= $row["totalloses"]; $email= $row["email"]; $country= $row["country"]; print_r($_POST); echo "<b><u>Username:</b></u> $username<br>"; echo "<b><u>Firstname:</b></u> $firstname<br>"; echo "<b><u>Surname: </b> </u> $surname<br>"; echo "<b><u>Date of Birth:</b></u> $dob<br>"; echo "<b><u>Total Chess Wins:</b></u> $totalwins<br>"; echo "<b><u>Total Chess loses:</b></u> $totalloses<br>"; echo "<b><u>Email Address: </b></u> $email<br>"; echo "<b><u>Born in: </b></u> $country<br>"; echo "<b><u>Other Details:</b></u> $info<br>"; }} ?> I think the problem is in the following line: while ($row = mysql_fetch_array($result)) Or do this seem ok? Quote Link to comment Share on other sites More sharing options...
litebearer Posted April 3, 2012 Share Posted April 3, 2012 Aside... checking for the submit may not always work. Better to have a hidden field in the form and then check if that is set and that it contains the designated value. Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 3, 2012 Share Posted April 3, 2012 Aside... checking for the submit may not always work. Better to have a hidden field in the form and then check if that is set and that it contains the designated value. Source? Quote Link to comment Share on other sites More sharing options...
smerny Posted April 3, 2012 Share Posted April 3, 2012 I think the problem is in the following line: while ($row = mysql_fetch_array($result)) Or do this seem ok? nothing wrong with that unless $result is wrong Aside... checking for the submit may not always work. Better to have a hidden field in the form and then check if that is set and that it contains the designated value. anytime a hidden value would get set, the submit value would also get set Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 3, 2012 Share Posted April 3, 2012 Put the print_r as the first line of the file. Quote Link to comment Share on other sites More sharing options...
litebearer Posted April 3, 2012 Share Posted April 3, 2012 @jesirose - source - Multiple times here on PHPFreaks but here is one... Now put that on your server and test it in a variety of browsers starting with Internet explorer. To test it, put the cursor in the text box and press the enter / return key on the keyboard. Most browsers are fine with this but if your version of IE is affected you'll see that the text field is submitted but not the button submit field. This means that any code that should of run in your script that relies on the submit button WILL NOT RUN. Why? Because Internet Explorer only sends the button if you click it with the mouse. If you have the text cursor in a text box and click the enter / return key on your keyboard IE does not send the value of the submit button. This is because you can use multiple submit buttons in one form (EG Edit and Delete on a blog / forum post) and so MS in their wisdom seem to have decided that only sending a clicked button is the wise thing. There is some wisdom in this but only for forms with multiple submit buttons. For forms with just one submit its a bit pointless. I've heard that on IE9 this is no longer a problem but I've tested this on IE5.5, IE6, IE7 and IE8 and it is the same on all of them. IF you do not experience the same symptoms and do not believe it, then please watch this video: quoted from http://www.codingforums.com/showthread.php?p=1144368 Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 3, 2012 Share Posted April 3, 2012 Thanks, I had never heard that before. I've also not experienced that. Personally rather than using a hidden input, I would just check for any other field (except a checkbox). If you have a text field, even if it's empty it should be in the submitted POST array. Unless there's an IE bug about that too? Quote Link to comment Share on other sites More sharing options...
ckerr27 Posted April 3, 2012 Author Share Posted April 3, 2012 Litebearer do you mean use something like: // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $username, table row must be 1 row if($count==1){ // Register $username redirect to file "getdetails.php" session_register("username"); header("location:getdetails.php?musername=" . $username); } 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.