ckerr27 Posted April 2, 2012 Share Posted April 2, 2012 Hey i have created the code below attempting to show a specific members details when their username is entered and the submit button is pressed. but when i enter the username all members details are shown. The code i have used is shown below, can anybody help me with the code to just show the single users details. I have attached an image aswell. <?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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/260193-show-specific-members-details/ Share on other sites More sharing options...
ocpaul20 Posted April 2, 2012 Share Posted April 2, 2012 something like $mname = "Joe"; $sql = sprintf("select * from memberdetails where username = '%s' ",$mname); // will get the member Joe's record ???? Quote Link to comment https://forums.phpfreaks.com/topic/260193-show-specific-members-details/#findComment-1333574 Share on other sites More sharing options...
batwimp Posted April 2, 2012 Share Posted April 2, 2012 You should have a unique ID for each table in your database. Then you can use the WHERE clause to call a specific member from the database. "select * from memberdetails WHERE ID = $uniqueID"; Quote Link to comment https://forums.phpfreaks.com/topic/260193-show-specific-members-details/#findComment-1333586 Share on other sites More sharing options...
ckerr27 Posted April 2, 2012 Author Share Posted April 2, 2012 Yes thats sort of it but that will always return joe's details, i want the user to be able to type in any members username and that members details will be shown? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/260193-show-specific-members-details/#findComment-1333589 Share on other sites More sharing options...
batwimp Posted April 2, 2012 Share Posted April 2, 2012 if(isset($_POST['searchname'])){ $inputName = mysql_real_escape_string($_POST['searchname']); }else{ $inputName=''; } $sql = "select * from memberdetails WHERE username = $inputName"; This is assuming you have a form that take the search input. Replace 'searchname' with whatever you have named the text box on your input form. Quote Link to comment https://forums.phpfreaks.com/topic/260193-show-specific-members-details/#findComment-1333590 Share on other sites More sharing options...
ckerr27 Posted April 2, 2012 Author Share Posted April 2, 2012 $username = "kerr1234"; $sql = sprintf("select * from memberdetails where username = '%s' ",$username); This code will only return kerr1234 details , is there anything i can add to make it show the details or which ever member the user types? Quote Link to comment https://forums.phpfreaks.com/topic/260193-show-specific-members-details/#findComment-1333593 Share on other sites More sharing options...
ckerr27 Posted April 2, 2012 Author Share Posted April 2, 2012 Do you mean as so: if(isset($_POST['searchname'])){ $inputName = mysql_real_escape_string($_POST['searchname']); }else{ $inputName=''; } $sql = "select * from memberdetails WHERE username = $inputName"; $result = mysql_query ($sql); while ($row = mysql_fetch_array($result)) This is giving me an error on the last line of code Quote Link to comment https://forums.phpfreaks.com/topic/260193-show-specific-members-details/#findComment-1333607 Share on other sites More sharing options...
batwimp Posted April 2, 2012 Share Posted April 2, 2012 Sorry, I think I missed some single quotes in there. I mean to put your original one back in: $sql = sprintf("select * from memberdetails where username = '%s' ",$inputName); Are you familiar with capturing input with an HTML for and passing that information to a PHP script? You should have a form with a text box name something like 'inputName' that you can catch with a POST on your script, then insert that into your SQL query. Quote Link to comment https://forums.phpfreaks.com/topic/260193-show-specific-members-details/#findComment-1333612 Share on other sites More sharing options...
ckerr27 Posted April 2, 2012 Author Share Posted April 2, 2012 <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> This is the form i use, is this ok? Quote Link to comment https://forums.phpfreaks.com/topic/260193-show-specific-members-details/#findComment-1333616 Share on other sites More sharing options...
batwimp Posted April 2, 2012 Share Posted April 2, 2012 if(isset($_POST['textfield'])){ $inputName = mysql_real_escape_string($_POST['textfield']); }else{ $inputName=''; } $sql = sprintf("select * from memberdetails where username = '%s' ",$inputName); Quote Link to comment https://forums.phpfreaks.com/topic/260193-show-specific-members-details/#findComment-1333622 Share on other sites More sharing options...
ckerr27 Posted April 2, 2012 Author Share Posted April 2, 2012 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 use the above code to echo but when i enter the username into the form and press the button i get an empty page Quote Link to comment https://forums.phpfreaks.com/topic/260193-show-specific-members-details/#findComment-1333623 Share on other sites More sharing options...
cpd Posted April 2, 2012 Share Posted April 2, 2012 Just because you've written code to show information, it doesn't mean it will. There are various problems which could arise such as no results being returned from your query... Ensure you're getting the expected results by dumping the information; if not there may be something wrong with your input. Work out the problem and go through your script piece by piece killing it at certain points to ensure its getting past specific if statements etc. You can also set your error reporting to all using the error_reporting(-1) at the top of your script. Quote Link to comment https://forums.phpfreaks.com/topic/260193-show-specific-members-details/#findComment-1333637 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.