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
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.

Link to comment
Share on other sites

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??

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

    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

Link to comment
Share on other sites

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/>";
		}
}

}
?>

Link to comment
Share on other sites

$result = mysql_query ( "SELECT * FROM users WHERE dob  LIKE '$dob' ");

Remove single quotation mark

$result = mysql_query ( "SELECT * FROM users WHERE dob  LIKE $dob ");

OR

$result = mysql_query ( "SELECT * FROM users WHERE dob  LIKE '" . $dob. "' ");

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.