Site search displaying whole table instead of search results? - php

I have a database with employee information being displayed into a table and I have managed to make the search box, I have connected to my database and everything now the problem is, when I search for the name i.e. "Daniel" it just gives me back the table which I already have, so then there are two of the tables instead of "Daniels" information.
Heres my code-
<!doctype html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="../Css/index_stylesheet.css">
</head>
<body>
<p>
<?php
$form = "<html>
<h1>Search!</h1>
<form method=\"get\">
<label>Name:
<input type=\"text\" name=\"keyname\" />
</label>
<input type=\"submit\" value=\"Search\" />
</form>
</body>
</html>";
//capture search term and remove spaces at its both ends if there is any
if(!empty($_GET['keyname'])){
$keyname = $_GET['keyname'];
$searchTerm = trim($keyname);
//database connection info
$host = "localhost"; //server
$db = "development_suite"; //database name
$user = "root"; //dabases user name
$pwd = ""; //password
//connecting to server and creating link to database
$link = mysqli_connect($host, $user, $pwd, $db);
//MYSQL search statement
$query = "SELECT firstname ,surname,address,mobile,email
FROM development_suite.eeg_staff;";
$results = mysqli_query($link,$query);
/* check whethere there were matching records in the table
by counting the number of results returned */
if(mysqli_num_rows($results) >= 1){
$output = "$row_1";
while($row = mysqli_fetch_array($results))
{
$output .= "First Name: " . $row['firstname'] . "<br />";
$output .= "Surname: " . $row['surname'] . "<br />";
$output .= "Address: " . $row['address'] . "<br />";
$output .= "Mobile: " . $row['mobile'] . "<br />";
$output .= "Email: " . $row['email'] . "<br /><br />";
}
}else{
$output = "There was no matching record for the name " .
strip_tags($searchTerm);
}
} else {
$output = "Enter name you are searching for.";
}
echo "$form\n$output";
?>
</body>
</html>

You must add where clause in your query. Something like ...
$query = "SELECT firstname ,surname,address,mobile,email
FROM development_suite.eeg_staff
WHERE firstname LIKE '" . $_REQUEST['keyname'] . "';";
Best regards,
Nebojsa

Related

Displaying image with users product post information?

Im making a website like craigslist where people can make a posting of something they want to sell with a post title, post description, and email.
The problem:
I can pull all data from the db except for the image. When images are uploaded they are stored in C:\xampp\htdocs\uploads and the imagename is saved in the database with the rest of the info. Below is my php. Im trying to pull the name of the image from the db and use that to grab it like this
echo "<img src='uploads/".$image."' width='200'> ";
Im new to php so any tips are appreciated. Is this an ok method to store/retrieve user uploaded images?
Thanks
<?php
$host = "localhost"; /* Host name */
$user = "root"; /* User */
$password = ""; /* Password */
$dbname = "mydb"; /* Database name */
$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
$sql= "SELECT * FROM products";
$result = $con->query($sql);
$image = "SELECT image FROM products";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<img src='uploads/".$image."' width='200'> ";
echo "Title: " . $row["title"]. " Price: $" . $row["price"]. " <br> " . $row["description"]. " <br> " . $row["contact"] . " <br><br>";
}
} else {
echo "0 results";
}
$con->close();
?>
You need to use row same for image like:
$sql= "SELECT * FROM products";
$result = $con->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<img src='uploads/".$row["image"]."' width='200'> ";
echo "Title: " . $row["title"]. " Price: $" . $row["price"]. " <br> " . $row["description"]. " <br> " . $row["contact"] . " <br><br>";
}
} else {
echo "0 results";
}
$con->close();

How to transfer table data in another table of a certain ID

<?php
$host = 'localhost';
$user ='root';
$pass = '';
$db = 'sad';
$con =mysqli_connect($host,$user,$pass,$db);
?>
<!DOCTYPE html>
<html>
<head>
<title>ADMIN</title>
<style>
table,th,td
{
border: 1.5px solid black;
}
</style>
<script>
function approve()
{
<?php
$hide = $_POST['omg'];
$qwe = "INSERT INTO `enrolled` SELECT * FROM `new` WHERE `id` = '$hide' ";
?>
}
</script>
</head>
<body>
<form method='POST'>
<table width=500px >
<?php
$sql = "SELECT * FROM New";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "<form method='POST'><tr><td>ID: " . $row["id"]. "<br>FIRSTNAME: " . $row["fname"]. "<br>MIDDLENAME: " . $row["mname"]. "<br>LASTNAME : " . $row["lname"]. "<br>COURSE: " . $row["course"]. "<br>ADDRESS: " . $row["address"]. "<br>CONTACT: " . $row["contact"] . "<br><br><input type='text' name='omg' value=$row[id]> <input type='button' name= 'btn' value='Approve' onclick=approve()></td></tr></form>";
}
}
else
{
echo "No Request Sent!";
}
?>
</table>
</form>
</body>
</html>
Im having a problem on copying a certain data of a table and transfer it into another table. this code $qwe = "INSERT INTO enrolled SELECT * FROM new is working and outputs all data contains by table 'New' and if i place WHERE id = '$hide' ; the database 'enrolled' shows nothing.
Something like this should work. First, select your dataset and then build the new insert query and in your loop. It's not the best solution, but hopefully you will understand the concept behinde this. But maybe you need a update query ... when you want to "insert" data at a specfic point. If you want only to copy youre data. This should work.
<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$database = 'sad';
$connection = mysqli_connect($host, $user, $pass, $database);
$query = "SELECT * FROM New";
$result = mysqli_query($connection, $query);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$query = 'INSERT INTO `enrolled` (id, fname, mname, lname, course, address, contact)
VALUES
(\'' . $row['id'] . ' \', \'' . $row['fname'] . ' \', \'' . $row['mname'] . ' \', \'' . $row['lname'] . ' \', \'' . $row['course'] . ' \', \'' . $row['address'] . ' \', \'' . $row['contact'] . '\')';
mysqli_query($connection, $query);
}

Trying to put table around results from query?

I can't put a table around my results from my code and need some help as I've tried but it comes back with "Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\search_go.php on line 27". So could I get help with how to insert a table?
<?php
//capture search term and remove spaces at its both ends if the is any
$searchTerm = trim($_GET['keyname']);
//check whether the name parsed is empty
if($searchTerm == "")
{
echo "Enter name you are searching for.";
exit();
}
//database connection info
$host = "localhost"; //server
$db = "calendar"; //database name
$user = "root"; //dabases user name
$pwd = ""; //password
//connecting to server and creating link to database
$link = mysqli_connect($host, $user, $pwd, $db);
//MYSQL search statement
$query = "SELECT * FROM caltbl WHERE evtDate LIKE '%$searchTerm%'";
$results = mysqli_query($link, $query);
<table>
/* check whether there were matching records in the table
by counting the number of results returned */
if(mysqli_num_rows($results) >= 1)
{
$output = "";
while($row = mysqli_fetch_array($results))
{
<tr>
$output .= "date: " . $row['evtDate'] . "<br />";
$output .= "Name: " . $row['patient'] . "<br />";
$output .= "Course: " . $row['patientId'] . "<br />";
}
echo $output;
}
else
echo "There was no matching record for the name " . $searchTerm;
?>
You can't just insert a HTML tag inside PHP code:
You can however just use an echo to send it out directly:
echo "<table>";
while($row = mysqli_fetch_array($results))
{
$output = "<tr>";
// <tr> This is the problem line.
$output .= "<tr>";
$output .= "<td>date: " . $row['evtDate'] . "<br /></td>";
$output .= "<td>Name: " . $row['patient'] . "<br /></td>";
$output .= "<td>Course: " . $row['patientId'] . "<br /></td>";
$output .= "</tr>";
echo $output;
}
Additionally you didn't close your <tr>. I added some extra snippets to make each field a TD in the table and then closed the row.

Search function not displaying results.

I have developed a search function which finds patients by their forename and surname and displays the results. However, after implementing the PHP code, the search results are not displaying.
Please note: The error messages are not displaying either;
Does anyone have any idea's why it is not displaying the search results?
<html>
<h1>Search By Name</h1>
<form action="" method="get">
<label>Name:
<input type="text" name="keyname" />
</label>
<input type="submit" value="submit" />
</form>
</body>
</html>
<?php
//capture search term and remove spaces at its both ends if there is any
if(isset($_GET['submit'])){
if(!isset($_GET['keyname'])){
$_GET['keyname'] = "";
$keyname = $_GET['keyname'];
$searchTerm = trim($keyname);
//check whether the name parsed is empty
if($searchTerm == "")
{
echo "Enter name you are searching for.";
exit();
}
//database connection info
$host = "localhost"; //server
$db = "a&e"; //database name
$user = "root"; //dabases user name
$pwd = ""; //password
//connecting to server and creating link to database
$link = mysqli_connect($host, $user, $pwd, $db);
//MYSQL search statement
$query = "SELECT PatientID, Forename, Surname, Gender, Patient_History, Illness, Priority FROM patient WHERE 'Forename' = '$keyname' OR 'Surname' = '$keyname'";
$results = mysqli_query($link, $query);
/* check whethere there were matching records in the table
by counting the number of results returned */
if(mysqli_num_rows($results) >= 1)
{
$output = "";
while($row = mysqli_fetch_array($results))
{
$output .= "PatientID: " . $row['PatientID'] . "<br />";
$output .= "Forename: " . $row['Forename'] . "<br />";
$output .= "Surname: " . $row['Surname'] . "<br />";
$output .= "Gender: " . $row['Gender'] . "<br />";
$output .= "Illness: " . $row['Illness'] . "<br />";
$output .= "Priority: " . $row['Priority'] . "<br />";
$output .= "Patient History: " . $row['Patient_History'] . "<br /><br />";
}
echo $output;
}
else {
echo "There was no matching record for the name " . $searchTerm; }
}
}
?>
Tried to post this on your previous question. If you want people to answer these you'll have to leave them up long enough for people to answer.
<?php
$form = "<html>
<h1>Search By Name</h1>
<form method=\"get\">
<label>Name:
<input type=\"text\" name=\"keyname\" />
</label>
<input type=\"submit\" value=\"Search\" />
</form>
</body>
</html>";
//capture search term and remove spaces at its both ends if there is any
if(!empty($_GET['keyname'])){
$keyname = $_GET['keyname'];
$searchTerm = trim($keyname);
//database connection info
$host = "localhost"; //server
$db = "a&e"; //database name
$user = "root"; //dabases user name
$pwd = ""; //password
//connecting to server and creating link to database
$link = mysqli_connect($host, $user, $pwd, $db);
//MYSQL search statement
$query = "SELECT PatientID, Forename, Surname, Gender, Patient_History, Illness, Priority FROM patient WHERE Forename LIKE '%$searchTerm%' OR Surname LIKE '%$searchTerm%'";
$results = mysqli_query($link, $query);
/* check whethere there were matching records in the table
by counting the number of results returned */
if(mysqli_num_rows($results) >= 1){
$output = "";
while($row = mysqli_fetch_array($results))
{
$output .= "PatientID: " . $row['PatientID'] . "<br />";
$output .= "Forename: " . $row['Forename'] . "<br />";
$output .= "Surname: " . $row['Surname'] . "<br />";
$output .= "Gender: " . $row['Gender'] . "<br />";
$output .= "Illness: " . $row['Illness'] . "<br />";
$output .= "Priority: " . $row['Priority'] . "<br />";
$output .= "Patient History: " . $row['Patient_History'] . "<br /><br />";
}
}else{
$output = "There was no matching record for the name " . strip_tags($searchTerm);
}
} else {
$output = "Enter name you are searching for.";
}
echo "$form\n$output";
?>
You should put your search code into a block that gets run only if the search term has a value:
if( empty($searchTerm) )
{
echo "Enter name you are searching for.";
}
else
{
// run your search code here and display the result.
}

How can i get the names that i search for, and add a pagination to the results that are present

The list just stayed at ten items with no pagination to get the rest of the other list, this sucks, But thanks Guys, maybe i need more practice i really need this one to be working so i can learn from this one, any way the first code worked, the second works but stops at ten items, you said try the ceil i did with some code that i had already, the next and back and number of pages came up but no list, tried it again no next and back but ten items came up, i'm beginning to get a bit frustrated here.
<DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>Residential</title>
</head>
<body>
<section>
<form method="post" action="index.php?go" id="searchform">
<input type="text" id="sf" name="name" class="inputtext" >
<input type="submit" name="submit" id="sb" alt="Btn" value="GO">
</form>
</section>
<div class="results">
<?php
error_reporting(0);
if(isset($_POST['submit'])) {
if(isset($_GET['go'])) {
if(preg_match("/[A-Z | a-z]+/", $_POST['name'])){ //"/[A-Z | a-z]+/",
$name=$_POST['name'];
//connect to the database
$db=mysql_connect ("localhost", "root", "") or die ('I cannot connect to the database because: ' . mysql_error());
//-select the database to use
$mydb=mysql_select_db("residential");
This is the beginning of the changed code
//////////pagination////////////
$sql=mysql_query("SELECT userId, FirstName, LastName FROM residentialbackup WHERE FirstName ='" . $name . "' OR LastName = '" . $name ."'");
//-count results
$numrows=mysql_num_rows($sql);
// Get the value of the last page in the pagination result set
$lastPage = ceil($numrows / $itemsPerPage);
$itemsPerPage = 10;
$page = $_GET['page'];
if (!$page) $page = 1;
$offset = ($page - 1) * $itemsPerPage;
$sql2=mysql_query("SELECT userId, FirstName, LastName FROM residentialbackup WHERE FirstName = '" . $name . "' OR LastName = '" . $name ."' LIMIT ". $offset . ", ". $itemsPerPage);
$paginationDisplay = ""; // Initialize the pagination output variable
// This code runs only if the last page variable is ot equal to 1, if it is only 1 page we require no paginated links to display
if ($lastPage != "1") {
// This shows the user what page they are on, and the total number of pages
$paginationDisplay .= 'Page <strong>' . $page . '</strong> of ' . $lastPage. ' ';
// If we are not on page 1 we can place the Back button
if ($page != 1) {
$previous = $page - 1;
$paginationDisplay .= ' Back ';
}
// Lay in the clickable numbers display here between the Back and Next links
$paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>';
// If we are not on the very last page we can place the Next button
if ($page != $lastPage) {
$nextPage = $page + 1;
$paginationDisplay .= ' Next ';
}
}
echo $paginationDisplay;
///////////////pagination end////////////
i'm stuck up to here!
//echo "<p>" .$numrows . " results found for " . stripslashes($name) . "</p>";
//-create while loop and loop through result set
while($row=mysql_fetch_array($sql2)) {
$FirstName =$row['FirstName'];
$LastName=$row['LastName'];
$userId=$row['userId'];
//-display the result of the array
echo "<ul>\n";
echo "<li>" . "" .$FirstName . " " . $LastName . " </li>\n";
echo "</ul>";
}
}
else {
echo "<p>Please enter a search query</p>";
}
}
/*
if(isset($_GET['by'])){
$name=$_GET['by'];
include "connectres.php";
//-query the database table
$sql="SELECT userId, FirstName, LastName FROM residentialbackup WHERE FirstName LIKE '%" . $name . "%' OR LastName LIKE '%" . $name ."%'";
//-run the query against the mysql query function
$result=mysql_query($sql);
//-count results
$numrows=mysql_num_rows($result);
echo "<p>" .$numrows . " results found for " . $name . "</p>";
//-create while loop and loop through result set
while($row=mysql_fetch_array($result)){
$FirstName =$row['FirstName'];
$LastName=$row['LastName'];
$userId=$row['userId'];
//-display the result of the array
echo "<ul>\n";
echo "<li>" . " " . $LastName . " " . $FirstName . " </li>\n";
echo "</ul>";
}
}
*/
if(isset($_GET['id'])) {
$userId=$_GET['id'];
//connect to the database
include "connectres.php";
//-query the database table
$sql="SELECT * FROM residentialbackup WHERE userId=" . $userId;
//-run the query against the mysql query function
$result=mysql_query($sql);
//-create while loop and loop through result set
while($row=mysql_fetch_array($result)) {
$FirstName =$row['FirstName'];
$LastName=$row['LastName'];
$PhoneNumber=$row['PhoneNumber'];
$Address=$row['Address'];
//-display the result of the array
echo "<ul>\n";
echo "<li>" . "<img src=\"../images/person.png\"/>" . $FirstName . " " . $LastName . " </li>\n";
echo "<li>" . "<img src=\"../images/tell.png\"/>" . " " . "" . $PhoneNumber . "</li>\n";
echo "<li>" . "<img src=\"../images/house.png\"/>" . " " . $Address . "</li>\n";
echo "</ul>";
}
}
?>
</div>
</div>
</body>
</html>
Your query has wildcards in it, remove them to only fetch exact matches
$sql="SELECT userId, FirstName, LastName FROM residentialbackup WHERE FirstName ='" . $name . "' OR LastName = '" . $name ."'";
You can make a pagination like this:
$itemsPerPage = 10;
$page = $_GET['page'];
if (!$page) $page = 1;
$offset = ($page - 1) * $itemsPerPage;
$sql="SELECT userId, FirstName, LastName FROM residentialbackup WHERE FirstName = '" . $name . "' OR LastName = '" . $name ."' LIMIT ". $offset . ", ". $itemsPerPage;

Categories