Jump to content

Searching a DB and returning Results


poorrichard

Recommended Posts

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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");

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • 8 months later...

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)

 

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.