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.

Jeiman
  • 1,121
  • 9
  • 27
  • 50
  • 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. http://www.php.net/manual/en/tutorial.forms.php – Nanne Jan 28 '14 at 13:08
  • @Nanne: Actually, he's not stating he's using POST anywhere. In fact, GET is more suitable. – Madara's Ghost Jan 28 '14 at 13:17
  • my bad, `s/post/get` and continue the same :D – Nanne Jan 28 '14 at 13:18

3 Answers3

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
Community
  • 1
  • 1
ʰᵈˑ
  • 11,279
  • 3
  • 26
  • 49
  • Actually, since he's fetching information, `GET` is probably a better choice than `POST` in this case. – Madara's Ghost Jan 28 '14 at 13:14
  • Very true @MadaraUchiha! – ʰᵈˑ Jan 28 '14 at 13:16
  • How would I display the results in the same page or another page? – Jeiman Jan 28 '14 at 13:37
  • @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 `` and then direct your form to that page. – ʰᵈˑ Jan 28 '14 at 13:40
  • I see ok. I have lots to learn from this and from PHP. May I know some good sites to learn PHP from? – Jeiman Jan 28 '14 at 13:54
  • You can't get any better than [the manual](http://php.net) & trial/error approach. Also, find books by Larry Ullman, such an awesome author! – ʰᵈˑ Jan 28 '14 at 13:55
  • Awesome dude. Thanks alot for the info and references. It will help me out alot. :) – Jeiman Jan 28 '14 at 13:56
  • Not a problem! Glad to help. – ʰᵈˑ Jan 28 '14 at 13:58
  • Another question, this is how I would link two PHP pages together, one being the search page and the other the results page. The result page having the following code `header('Location: results.php'); ` – Jeiman Jan 28 '14 at 14:08
  • Don't use `header` - just change the `form action="someOtherPage.php"` – ʰᵈˑ Jan 28 '14 at 14:11
  • (Don't use `header`, as - by your way - you'd loose your _GET values) – ʰᵈˑ Jan 28 '14 at 14:11
  • Ah, I see. I did some messing around and it seems that I need to replicate the main page exactly excluding the `fetch` statement and placing it in the results page? Am I correct? – Jeiman Jan 28 '14 at 14:18
  • In results.php, just put in the PHP code, and leave the HTML code for the other page. – ʰᵈˑ Jan 28 '14 at 14:19
  • Yeah cool. I'm leaving the HTML code behind to give it abit of styling. But thanks again for the tips. – Jeiman Jan 28 '14 at 14:23
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();
    }
Maz I
  • 3,664
  • 2
  • 23
  • 38
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.

suchitra nair
  • 535
  • 7
  • 13