Jump to content

Help get details from sql database please


ckerr27

Recommended Posts

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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']))
?>

Link to comment
Share on other sites

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??

Link to comment
Share on other sites

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>";
          }}
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

@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

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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);

}

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.