silverglade Posted August 29, 2011 Share Posted August 29, 2011 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 More sharing options...
MasterACE14 Posted August 29, 2011 Share Posted August 29, 2011 if you want that EXACT number use this query instead: "SELECT * FROM users WHERE dob='".$dob."' " Link to comment https://forums.phpfreaks.com/topic/245937-searching-for-a-record-from-form/#findComment-1263046 Share on other sites More sharing options...
silverglade Posted August 29, 2011 Author Share Posted August 29, 2011 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 https://forums.phpfreaks.com/topic/245937-searching-for-a-record-from-form/#findComment-1263048 Share on other sites More sharing options...
silverglade Posted August 29, 2011 Author Share Posted August 29, 2011 The reason I am just testing it with this is to simplify, I was having major problems with this when it was large. Link to comment https://forums.phpfreaks.com/topic/245937-searching-for-a-record-from-form/#findComment-1263052 Share on other sites More sharing options...
cunoodle2 Posted August 29, 2011 Share Posted August 29, 2011 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 https://forums.phpfreaks.com/topic/245937-searching-for-a-record-from-form/#findComment-1263053 Share on other sites More sharing options...
cunoodle2 Posted August 29, 2011 Share Posted August 29, 2011 ALSO these two items should ONLY be called AFTER you have determined that the form has been submitted.. $dob = $_POST['dob']; //THE SEARCH FUNCTION $result = mysql_query ( "SELECT * FROM users WHERE dob LIKE '%$dob%' "); Link to comment https://forums.phpfreaks.com/topic/245937-searching-for-a-record-from-form/#findComment-1263054 Share on other sites More sharing options...
silverglade Posted August 29, 2011 Author Share Posted August 29, 2011 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 https://forums.phpfreaks.com/topic/245937-searching-for-a-record-from-form/#findComment-1263057 Share on other sites More sharing options...
silverglade Posted August 29, 2011 Author Share Posted August 29, 2011 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 https://forums.phpfreaks.com/topic/245937-searching-for-a-record-from-form/#findComment-1263060 Share on other sites More sharing options...
silverglade Posted August 29, 2011 Author Share Posted August 29, 2011 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 https://forums.phpfreaks.com/topic/245937-searching-for-a-record-from-form/#findComment-1263068 Share on other sites More sharing options...
voip03 Posted August 29, 2011 Share Posted August 29, 2011 $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 https://forums.phpfreaks.com/topic/245937-searching-for-a-record-from-form/#findComment-1263073 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.