Jump to content

searching for a record from form


silverglade

Recommended Posts

Hi, I cannot get my form to find the number "555" entered for a user in the database. There is a table field called "dob" with value 555 for a particular user. Below is the code I am trying to search the table and output a match. Please if anyone can help that would be great. thank you.

 

<?php 
$host		= "";
$database 	= "";
$username 	= "";
$password 	= "*";
$tbl_name   = "users";
$conn = mysql_connect($host, $username, $password) or die("Could not connect: " . mysql_error());
mysql_select_db($database);


$dob = $_POST['dob'];

//THE SEARCH FUNCTION
$result = mysql_query ( "SELECT * FROM users WHERE dob  LIKE '%$dob%' ");

if(isset($_POST['submit'])) 

{
while ($row = mysql_fetch_assoc($result)) {
    $message = $row["dob"];
    

}

}
?>
<html>
<body>

<form action="login_successBACKUP02.php" method="post">


    <input name="dob" type="text" size="20" id="dob" />
    Date of Birth<br />
   
    <input type="submit" value="Store in database and search" />
    <input type="reset" value="Reset fields" />


</form> 
  

</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/245937-searching-for-a-record-from-form/
Share on other sites

thanks for replying. I want to put in lets say "5" which is part of the number, and it should return "555". Or lets say in date of birth I had in the database for that "august 1 1999" , if I entered in the search "august", I would like it to echo back the full "august 1 1999" contents of the field. any more help greatly appreciated thanks.

Well I see 2 issues with this code in particular..

<?php
while ($row = mysql_fetch_assoc($result)) {
    $message = $row["dob"];
}
?>

 

1)  you simply keep overwriting the variable "$message."  So if you in theory had 50 results then the only item stored would be the LAST one

2)  you NEVER echo any php item to the screen.  Where do you expect to see this item??

thanks very much. I changed it, but I don't know how to output the result correctly so that it doesn't keep overwriting itself. That is all I need left please if you know. thanks.

here is the code changed.




<?php


if(isset($_POST['submit'])) 

{
$dob = $_POST['dob'];
//THE SEARCH FUNCTION
$result = mysql_query ( "SELECT * FROM users WHERE dob  LIKE '%$dob%' ");

while ($row = mysql_fetch_assoc($result)) {
    $message = $row["dob"];
    echo $message;

}

}
?>
<html>
<body>

<form action="login_successBACKUP02.php" method="post">


    <input name="dob" type="text" size="20" id="dob" />
    Date of Birth<br />
   
    <input type="submit" value="Store in database and search" />
    <input type="reset" value="Reset fields" />


</form> 
  

</body>
</html>

    If the code was working and now it's not, something changed. Did you change any code?

 

Quote from: searls03 on March 22, 2011, 04:03:36 PM

 

    yeah, just a little bit, I am trying to start from beginning and trace my steps to see what i did wrong unless you see it.

 

 

that's funny

I even shortened it to this and get no output, I enter 555, the value in the DB and it returns nothing. I would like to have '%dob%' in there instead, because that is what I am going to use for a ton of info, but I tried to simplify it even more and it doesn't work . please help if anyone can. thanks.

 

 

<?php

if(isset($_POST['submit'])) 

{
$dob = $_POST['dob'];
//THE SEARCH FUNCTION
$result = mysql_query ( "SELECT * FROM users WHERE dob  LIKE '$dob' ");



while ($row = mysql_fetch_assoc($result)) {

	foreach($row as $var => $val) {
		echo "<b>$var</b>: $val<br/>";
		}
}

}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.