0

I have a from which is linked to a PHP script. When the user enters their username I want to to display fields regarding to their record. Specifically 'DOB' and 'email'.

The problem is, when i enter the username it opens test.php but does not show up any records.

My form is:

<form id="form1" name="form1" method="post" action="test.php">
  <label>Name
  <input type="text" name="textfield" />
  </label>
  <p>
    <label>
    <input type="submit" name="Submit" value="Submit" />
    </label>

  </p>
</form>

My PHP script is:

  <?php 

    $host=""; // Host name 
    $username=""; // Mysql username 
    $password=""; // Mysql password 
    $db_name=""; // Database name 
    $tbl_name="members"; // Table name  

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

     $username = $_POST['textfield'];
      echo '</br>';
     $query = mysql_query("SELECT * FROM `members` WHERE `username`='$username'");

    while($result = mysql_fetch_array($query)) {
    //display
    echo $result['DOB'];
    echo $result['email'];
    }
    ?>
5
  • 1
    Should it REALLY be WHERE email='$username' ? Commented Apr 29, 2013 at 23:41
  • I was experimenting, it is fixed now Commented Apr 29, 2013 at 23:43
  • Please use PDO or MySQLi to connect to your databases. Wouldn't want any nasty hackers injecting through vulnerabilities now would we? Commented Apr 29, 2013 at 23:46
  • not sure how to do that. They can have access to it if they so wish. Commented Apr 29, 2013 at 23:50
  • You are setting $tbl_name="members"; // Table name but querying FROM `orders`. Also have you tried running the query directly in MySQL/phpMyAdmin? Commented Apr 29, 2013 at 23:51

2 Answers 2

2

You should connect to a database before you run any queries:

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
Sign up to request clarification or add additional context in comments.

1 Comment

thanks I have just revised my answer but still not calling up the results
0

Instead of

$query = mysql_query("SELECT * FROM `orders` WHERE `username`='$username'");

do this:

$sql = "SELECT * FROM `orders` WHERE `username`='$username'";
$query = mysql_query($sql) or die(mysql_error());
echo 'SQL=' . $sql;

What result do you get? (What is SQL?)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.