Jump to content
Killersites Forums

Php Search Mysql


Kevinstan
 Share

Recommended Posts

Hello all. I am trying to add a search field at the top of this that will allow me to search the MySQL database information and return the results in the same table view with the edit and delete options at the end of the table still. I want to be able to search by name and or id number. I cannot figure out how to do this and make it work properly. Hopefully someone in here can help me out. Thank you everyone in advance !

 


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
       <title>View RC Data</title>

	<style type="text/css">
table { border-collapse:collapse; }
table td, table th { border:1px solid black;padding:5px; }
tr:nth-child(even) {background: #ffffff}
tr:nth-child(odd) {background: #ff0000}
</style>


</head>
<body>

<img src="logo.png" alt="logo">

<?php
/* 
       VIEW.PHP
       Displays all data from 'rcrentals' table
*/

       // connect to the database
       include('connect-dbrc.php');

       // get results from database
       $result = mysql_query("SELECT * FROM rcrentals") 
               or die(mysql_error());  

       // display data in table
       // echo "<p><b>View All</b> | <a href='view-paginated.php?page=1'>View Paginated</a></p>";

       echo "<table border='1' cellpadding='5'>";
       echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th>Address</th> <th>City</th> <th>State</th> <th>Zip</th> <th>Phone</th> <th>DL</th> <th>Email</th> <th>Car and Controller</th> <th></th> <th></th></tr>";

       // loop through results of database query, displaying them in the table
       while($row = mysql_fetch_array( $result )) {

               // echo out the contents of each row into a table
               echo "<tr>";
               echo '<td>' . $row['id'] . '</td>';
               echo '<td>' . $row['firstname'] . '</td>';
               echo '<td>' . $row['lastname'] . '</td>';
	echo '<td>' . $row['address'] . '</td>';
	echo '<td>' . $row['city'] . '</td>';
	echo '<td>' . $row['st'] . '</td>';
	echo '<td>' . $row['zip'] . '</td>';
	echo '<td>' . $row['phone'] . '</td>';
	echo '<td>' . $row['dl'] . '</td>';
	echo '<td>' . $row['email'] . '</td>';
	echo '<td>' . $row['carcont'] . '</td>';
               echo '<td><a href="edit.php?id=' . $row['id'] . '">Edit</a></td>';
               // echo '<td><a href="delete.php?id=' . $row['id'] . '">Delete</a></td>';
	echo '<td><a href="delete.php?id=' . $row['id'] . '" onclick="return confirm(\'Confirm?\')">Delete</a></td>';
			echo "</tr>"; 
       } 

       // close table>
       echo "</table>";
?>
<p><a href="../../rc/new.php">Manual Add Entry</a> :: <a href="#">Return to Main Admin Screen</a></p>

</body>
</html> 

Edited by Kevinstan
Link to comment
Share on other sites

Firstly, I'm no expert, just learning as well myself so maybe somebody else can add in here.

Secondly, I think users normally don't know their id so usually it is not a good search tool. If you already know the id, then no search is needed.

Thirdly, a search for first name alone and even first name and last name is not all that specific -- you could have more than one user with the same name.

 

But you could make a form for the entry of the name you want to search. This is the general gist of it, I'm probably missing a good few things here so

you will need to fill it out with validation and all that stuff:

 

So something like:

 


<form method="POST" action="">
<input type="text" name="search_firstname" value="">
<input type="text" name="search_lastname" value="">
<input type="submit" name="submit" value="search">
</form>

// further down then, you could run a query to the database to find the details for that name.

if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {

// I haven't time to add validation, you will need to do that yourself.

$firstname = $_POST['search_firstname'];
$lastname = $_POST['search_firstname'];

$q ="SELECT * FROM rcrentals WHERE firstname = '$firstname' AND lastname = '$lastname' ";
$r = @mysqli_query ($link_to_database, $q);

$data = mysqli_fetch_all($r);

echo ' <p> My search reveals that you live in' . $data[0]['city'] . '</p>';

}

Edited by Stephenius
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
 Share

×
×
  • Create New...