1

I am beginner in PHP and I am trying to figure out how to link a form to a PHP script.

Basically, I am creating a search bar where users can search the database for employee details based on their ID. The script would then retrieve the results from the database based on the ID.

Code:

    <form action="results.php" method="get">
        Employee ID: <input type="text" name="name"><input type="submit">
    </form>
    <?php


// connect to the mysql database server
    $db = new PDO('mysql:host=localhost:3306;dbname=db_test14;charset=utf8', 'root', 'password');

// Prepare the statement (the basic outline of your query)
    $st = $db->prepare('SELECT * from techsols_employee WHERE id = ?');

// Actually execute the query, putting in your id
    $st->execute(array($employee_id));

// Get the actual employee details out of the result
    $employee = $st->fetch();
    ?>

Would appreciate some help on this.

3
  • Check out the php manual and read up on form posts. Mind you that you have to actually POST the form to get a $_POST. If you don't want that, check out AJAX. php.net/manual/en/tutorial.forms.php Commented Jan 28, 2014 at 13:08
  • @Nanne: Actually, he's not stating he's using POST anywhere. In fact, GET is more suitable. Commented Jan 28, 2014 at 13:17
  • my bad, s/post/get and continue the same :D Commented Jan 28, 2014 at 13:18

3 Answers 3

3
<form action="results.php" method="GET">
    Employee ID: <input type="text" name="name"><input type="submit">
</form>
<?php
if (isset($_GET['name'])) {

    //! Some validation and sanitising of the _GET here
    $employee_id = $_GET['name'];


    // connect to the mysql database server
    $db = new PDO('mysql:host=localhost:3306;dbname=db_test14;charset=utf8', 'root', 'password');

    // Prepare the statement (the basic outline of your query)
    $st = $db->prepare('SELECT * from techsols_employee WHERE id = ?');

    // Actually execute the query, putting in your id
    $st->execute(array($employee_id));

    // Get the actual employee details out of the result
    $employee = $st->fetch(PDO::FETCH_ASSOC);
    echo $employee['name'];
}
?>

What have I changed?

  • GET to POST (Difference) - Edited after comments back to _GET.
  • $employee_id to equal _POST
  • Checks if you've input by isset
  • Changed the fetch style to PDO::FETCH_ASSOC
Sign up to request clarification or add additional context in comments.

14 Comments

Actually, since he's fetching information, GET is probably a better choice than POST in this case.
Very true @MadaraUchiha!
How would I display the results in the same page or another page?
@Jeiman Post updated. Check the fetch statement and the notes at the bottom of the page. To put it on another page, simply C&P the code between the <?php and ?> and then direct your form to that page.
I see ok. I have lots to learn from this and from PHP. May I know some good sites to learn PHP from?
|
0

Try this Employee ID:

 <?php
      if(count($_GET)){
         $emp_id = intval($_GET['name']);   
        //! Some validation and sanitising of the _POST here
        // connect to the mysql database server
       $db = new PDO('mysql:host=localhost:3306;dbname=db_test14;charset=utf8',
       'root', 'password');

       // Prepare the statement (the basic outline of your query)
       $st = $db->prepare('SELECT * from techsols_employee WHERE id = ?');

       // Actually execute the query, putting in your id
       $st->execute(array($emp_id ));

      // Get the actual employee details out of the result
       $employee = $st->fetch();
    }

Comments

0

check out a sample example in w3schools http://www.w3schools.com/php/php_ajax_database.asp Modify it to make it work for your case.

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.