JM2010 Posted July 7, 2009 Share Posted July 7, 2009 Hey All, I have tried and tried with no luck creating a PHP Search forum. What I want to do, is to allow User X, to type in his ID Number and for the site to search a database that would return a number that is associated with his ID Number. How do I do this. I have the database created, I think, so how do I go about creating a web page that wold allow this. Thanks, any help is useful! Quote Link to comment https://forums.phpfreaks.com/topic/165067-php-noob-needs-some-help/ Share on other sites More sharing options...
Maq Posted July 7, 2009 Share Posted July 7, 2009 I have tried and tried with no luck creating a PHP Search forum. You mean form, right? HTML Forms - You need to use forms to allow the user to input and submit data. PHP MySQL - Upon submission you should grab the id with the POST method and query the database where it matches the id and return the fields you want. Quote Link to comment https://forums.phpfreaks.com/topic/165067-php-noob-needs-some-help/#findComment-870428 Share on other sites More sharing options...
chokies12 Posted July 7, 2009 Share Posted July 7, 2009 like this? <?php $var = $_POST['var']; $result = mysql_query("SELECT * FROM table WHERE id='$var'"); if($result == 1) { // check if row exist $result=mysql_fetch_array($result); //get row content $id = $result['associatedNumber']; //get the value of the field } echo $id; ?> <html> <head><title>hello</title></head> <body> <form method="post" action="index.php"> <input type="text" name="var"> <input type="submit" value="submt" name="submit"> </form> <body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/165067-php-noob-needs-some-help/#findComment-870454 Share on other sites More sharing options...
Prismatic Posted July 7, 2009 Share Posted July 7, 2009 like this? <?php $var = $_POST['var']; $result = mysql_query("SELECT * FROM table WHERE id='$var'"); if($result == 1) { // check if row exist $result=mysql_fetch_array($result); //get row content $id = $result['associatedNumber']; //get the value of the field } echo $id; ?> <html> <head><title>hello</title></head> <body> <form method="post" action="index.php"> <input type="text" name="var"> <input type="submit" value="submt" name="submit"> </form> <body> </html> There's a couple things wrong with this. First, never ever take user input and use it directly in a query. <?php $result = mysql_query("SELECT * FROM table WHERE id = '". mysql_real_escape_string($var) ."'"); ?> Second, mysql_query won't tell you how many rows are returned. It will either return false because of an invalid query, or a resource handler (for certain query types). So even if it validates to true, that doesn't mean there's any rows found, just that the query successfully ran. <?php if(mysql_num_rows($result) > 0) { // check if row exist ?> Quote Link to comment https://forums.phpfreaks.com/topic/165067-php-noob-needs-some-help/#findComment-870499 Share on other sites More sharing options...
JM2010 Posted July 7, 2009 Author Share Posted July 7, 2009 thanks for all the help, but do I still use that or what? lol Quote Link to comment https://forums.phpfreaks.com/topic/165067-php-noob-needs-some-help/#findComment-870542 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.