0

I have been trying to build a basic search script. Having a lot of trouble. I need the search form and search result to be two separate pages. The script is not working.

The search script is taking me to the search_result page, but the inputs are empty.

enter image description here

<html>
<body>

<form action="search_result.php" method="POST">
<input type="text" name="reg" />
<input type="submit" value="Search" />
</form>

</html>
</body>

PAGE 2:

<html>
<body>


<?php
$host="localhost";
$username="XXXXXXXXXXX";
$password="XXXXXXXXXXX";
$db_name="XXXXXXXXXXXX";
$tbl_name="reg_add";
mysql_connect("$host", "$username", "$password") or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


$record = $_POST['record']; // if coming from e.g. a form
$result=mysql_query(" SELECT * FROM reg_add WHERE reg='" . mysql_real_escape_string($record) . "'");


$row = mysql_fetch_assoc($result);
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$reg = $row['reg'];
?>



<input  name="reg" value="<? echo "$record" ?>">

<input  name="first_name" value="<? echo "$first_name" ?>">

<input  name="last_name" value="<? echo "$last_name" ?>">

</body>
</html>
4
  • 2
    in what way is it not working? Errors? What's the expected vs the actual result? Commented Apr 26, 2012 at 15:40
  • 3
    you did not say anything useful about the problem and the two scripts don't seem to be connected (first one submits something in $_GET on the reg key, the second one expects something in $_POST on the record key) Commented Apr 26, 2012 at 15:42
  • The search script is taking me to the search_result page, but the inputs are empty. Commented Apr 26, 2012 at 15:49
  • 1
    did you even read the comments and answers added here? you should learn the difference between post php.net/manual/en/reserved.variables.post.php and get php.net/manual/en/reserved.variables.get.php Commented Apr 26, 2012 at 15:52

2 Answers 2

1

Looks like you are looking for $_POST['record'] but passing through $_GET['reg']

Other than that the search isn't very good, it'll only find it if the exact reg is found, is that what you require?

Sign up to request clarification or add additional context in comments.

3 Comments

Yes. I only want to find reg which is the primary field. I have about 40 other fields related to key record.
I updated the script, but it won't work. I added a picture of the result.
$record can't be set if you aren't getting output. Again, I see you've changed it from GET to POST now, but the field name is still reg, not record. So be certain you've changed the $record = $_POST['record']; line to $record = $_POST['reg'];
1

HTML form submit method is GET and you're trying to retrieve it in POST method. Change either one, and check the key/name attributes of form elements too. `

<form action="search_result.php" method="POST">
<input type="text" name="record" />
<input type="submit" value="Search" />
</form>

2 Comments

I updated the script, but it won't work. I added a picture of the result.
You are not retrieving what you're posting. Your updated code is still incorrect. If you are submitting text field value with reg as name it can be retrieved using $_POST['reg']. What I can see is you are using $_POST['record']. Just replace your form with one I've posted. That should work.After getting correct data, if your script is not displaying anything that means there's problem with your query or no record might be present. Easiest way to debug is $_POST is , simply use print_r($_POST); .

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.