poorrichard Posted February 18, 2009 Share Posted February 18, 2009 I'm working on a project that has two parts. 1.) A php form that uploads user entries into my database. This part is done. 2.) The same form, used to search the database and return the found results to the user. What ive been able to do so far, is have the form re-print my data that i entered, but it does not matter if it is on my db or not. Here is the code, any help would be appreciated. (Note, im an absolute beginner and my job requires me to do things that i do not know how to do. That's why i'm here ) <?php if (!empty($_POST['submit'])) { $username=""; $password=""; $database="contacts"; $first=$_POST['first']; $last=$_POST['last']; $dept=$_POST['dept']; $book=$_POST['book']; $conf=$_POST['conf']; $journal=$_POST['journal']; $email=$_POST['email']; $web=$_POST['web']; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query = "SELECT first, last, dept, email, web, book, conf, journal FROM contacts WHERE first LIKE '%$first%' and last LIKE '%$last%' and dept LIKE '%$dept%' and email LIKE '%$email%' and web LIKE '%$web%' and book LIKE '%$book%' and conf LIKE '%$conf%' and journal LIKE '%$journal%')"; mysql_query($query); mysql_close(); echo "<b>PLEASE ONLY HIT SUBMIT ONCE ON THIS FORM</b>"; } ?> <html> <body> <form action="search.php" method="post"> First Name: <input type="text" name="first"> Last Name: <input type="text" name="last"><br> Department: <input type="text" name="dept"><br> E-mail-Add: <input type="text" name="email"> <align="right"> Website: <input type="text" name="web"></align><br> <p>Book(s):<BR> <TEXTAREA NAME="book" COLS=40 ROWS=6></TEXTAREA><br> Conference(s):<BR> <TEXTAREA NAME="conf" COLS=40 ROWS=6></TEXTAREA><br> Journal(s):<BR> <TEXTAREA NAME="journal" COLS=40 ROWS=6></TEXTAREA><br> <input type="submit" name="submit" value="submit"> </form> </body> Quote Link to comment https://forums.phpfreaks.com/topic/145766-searching-a-db-and-returning-results/ Share on other sites More sharing options...
allworknoplay Posted February 18, 2009 Share Posted February 18, 2009 Ok, after your MYSQL Query, you're not doing anything to grab the records and printing them out.... Quote Link to comment https://forums.phpfreaks.com/topic/145766-searching-a-db-and-returning-results/#findComment-765318 Share on other sites More sharing options...
poorrichard Posted February 18, 2009 Author Share Posted February 18, 2009 Could you assist me on how to do that? Quote Link to comment https://forums.phpfreaks.com/topic/145766-searching-a-db-and-returning-results/#findComment-765319 Share on other sites More sharing options...
milesap Posted February 18, 2009 Share Posted February 18, 2009 <?php $users = array(); if (!empty($_POST['submit'])) { $users['username'] = ""; $users['password'] = ""; $users['database'] = "contacts"; $users['first'] = $_POST['first']; $users['last'] = $_POST['last']; $users['dept'] = $_POST['dept']; $users['book'] = $_POST['book']; $users['conf'] = $_POST['conf']; $users['journal'] = $_POST['journal']; $users['email'] = $_POST['email']; $users['web'] = $_POST['web']; } else { mysql_connect(localhost,$username,$password); mysql_select_db($users['database']) or die( "Unable to select database"); $query = mysql_query("SELECT * FROM contacts WHERE first='$first' AND last='$last' AND dept='$dept' AND email='$email' AND web='$web' AND book='$book' AND conf='$conf' AND journal='$journal')"); $users = mysql_fetch_assoc($query); echo "<b>PLEASE ONLY HIT SUBMIT ONCE ON THIS FORM</b>"; } echo ' <html> <body> <form action="search.php" method="post"> First Name: <input type="text" name="first" value="' . $users['first'] . '"> Last Name: <input type="text" name="last" value="' . $users['last'] . '"><br> Department: <input type="text" name="dept"><br> E-mail-Add: <input type="text" name="email" value="' . $users['email'] . '"> <align="right"> Website: <input type="text" name="web"></align><br> <p>Book(s):<BR> <TEXTAREA NAME="book" COLS=40 ROWS=6></TEXTAREA><br> Conference(s):<BR> <TEXTAREA NAME="conf" COLS=40 ROWS=6></TEXTAREA><br> Journal(s):<BR> <TEXTAREA NAME="journal" COLS=40 ROWS=6></TEXTAREA><br> <input type="submit" name="submit" value="submit"> </form> </body>'; Didn't check the code, but that should work for your application. Let me know it it works Quote Link to comment https://forums.phpfreaks.com/topic/145766-searching-a-db-and-returning-results/#findComment-765326 Share on other sites More sharing options...
poorrichard Posted February 18, 2009 Author Share Posted February 18, 2009 are all the variables supposed to be $users for your code miles? Quote Link to comment https://forums.phpfreaks.com/topic/145766-searching-a-db-and-returning-results/#findComment-765342 Share on other sites More sharing options...
milesap Posted February 18, 2009 Share Posted February 18, 2009 Every variable that deals with user data should be in the $user array. This not only makes the code easier to read down the line, but gives the added benefit that the array can be populated by either the $_POST or the mysql_fetch_assoc, standardizing these variables for later use. Quote Link to comment https://forums.phpfreaks.com/topic/145766-searching-a-db-and-returning-results/#findComment-765353 Share on other sites More sharing options...
poorrichard Posted February 18, 2009 Author Share Posted February 18, 2009 i'm getting some DB connect issues with the code. I've entered my username and pass here, and changed nothing else $users['username'] = "*****"; $users['password'] = "*****"; $users['database'] = "contacts"; Quote Link to comment https://forums.phpfreaks.com/topic/145766-searching-a-db-and-returning-results/#findComment-765356 Share on other sites More sharing options...
milesap Posted February 18, 2009 Share Posted February 18, 2009 if (!empty($_POST['submit'])) { $username = ""; $password = ""; $database = "contacts"; .... mysql_connect(localhost,$username,$password); mysql_select_db($database) or die( "Unable to select database"); Change those lines and you'll be running again. Quote Link to comment https://forums.phpfreaks.com/topic/145766-searching-a-db-and-returning-results/#findComment-765360 Share on other sites More sharing options...
poorrichard Posted February 18, 2009 Author Share Posted February 18, 2009 Getting more errors of the same DB Failure to connect variety <?php $users = array(); if (!empty($_POST['submit'])) { $username = "***"; $password = "**"; $database = "contacts"; $users['first'] = $_POST['first']; $users['last'] = $_POST['last']; $users['dept'] = $_POST['dept']; $users['book'] = $_POST['book']; $users['conf'] = $_POST['conf']; $users['journal'] = $_POST['journal']; $users['email'] = $_POST['email']; $users['web'] = $_POST['web']; } else { mysql_connect(localhost,$username,$password); mysql_select_db($database) or die( "Unable to select database"); Quote Link to comment https://forums.phpfreaks.com/topic/145766-searching-a-db-and-returning-results/#findComment-765369 Share on other sites More sharing options...
milesap Posted February 18, 2009 Share Posted February 18, 2009 $username = "***"; $password = "**"; $database = "contacts"; if (!empty($_POST['submit'])) { $users['first'] = $_POST['first']; $users['last'] = $_POST['last']; $users['dept'] = $_POST['dept']; $users['book'] = $_POST['book']; $users['conf'] = $_POST['conf']; $users['journal'] = $_POST['journal']; $users['email'] = $_POST['email']; $users['web'] = $_POST['web']; } else { mysql_connect(localhost,$username,$password); mysql_select_db($database) or die( "Unable to select database"); lol now it should work, wasn't paying attention. Your db setting were in the wrong place. Quote Link to comment https://forums.phpfreaks.com/topic/145766-searching-a-db-and-returning-results/#findComment-765389 Share on other sites More sharing options...
poorrichard Posted February 18, 2009 Author Share Posted February 18, 2009 Parse error: syntax error, unexpected $end Quote Link to comment https://forums.phpfreaks.com/topic/145766-searching-a-db-and-returning-results/#findComment-765393 Share on other sites More sharing options...
martinhoods Posted October 28, 2009 Share Posted October 28, 2009 You are using a class ($db->query). Your problem is most likely in your class, unable to deal with "order by `name` asc". Why use a bl**y class over which you have no control, when you can make direct calls that take LESS time and space??? I bet your class does not have the right mysql syntax either! If you really want o improve the system, drop the class and make direct calls: mysql_query, mysql_fetch_array, mysql_free_result! Please, don't use a keyword as a variable name ($return, return) Quote Link to comment https://forums.phpfreaks.com/topic/145766-searching-a-db-and-returning-results/#findComment-946544 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.