Search code examples
phpsqlformspopulateauto-populate

Populate fields from database based on user input SQL


I have a form where the user inputs their ID and this then populates their name from a database? There is a whole form I just copied the relevant parts and the sql below.

User ID: <input value="User ID" name="user_id">

$sql = "SELECT user_firstname, user_surname FROM users_tbl WHERE xxxx = users_tbl.user_id"
$result = pg_query($sql);

I have made it this far, but im not sure what to do.


Solution

  • You probably want something like ...

    page1.php

    <form method="POST" action="page2.php">
    User ID: <input name="user_id" value="User ID">
    <input type="submit" value="go">
    </form>
    

    page2.php

    $id = mysql_escape_string( $_POST['user_id'] );
    $sql = "SELECT `user_firstname`, `user_surname` FROM `users_tbl `WHERE `id` = '$id' LIMIT 1";
    ...