Search function not displaying results. - php

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

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

Site search displaying whole table instead of search results?

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

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.

Why am I getting white spaces from this function?

I made a function that loops through all the posts in database and echo them. But there's a slight problem. I am getting whitespace between the name (title of the post) and the content, its like two lines or so. I want reason for this and solution. Thanks.
function read_all_posts(){
$host = 'localhost';
$username = 'root';
$password = '';
$database = 'website';
$con = mysqli_connect($host, $username, $password, $database);
$query = "SELECT * FROM posts";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_array($result))
{
echo "<h1>" . $row['name'] . "</h1>" . "<br>" . $row['content'] . "<br />" . "<i>" . $row['author'] . "</i>";
echo "<br>";
}
}
try,
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_array($result))
{
echo "<h1>" . $row['name'] . "</h1>" . "<p>" . $row['content'] . "</p>" . "<p><i>" . $row['author'] . "</i></p>";
}

why does the error message not show up?

I am very new to PHP (only been doing it since September so I apologise if this seems like a silly question, I'm very stuck and can't work out the answer!) and cannot work out why my error message does not display when a user submits the form when it is empty.
This is my code:
<?php
$salonid = "";
if (!$db_server){
die("Unable to connect to MySQL: " . mysqli_connect_error($db_server));
$db_status = "not connected";
}else{
//Capture form data, if anything was submitted
if (isset($_GET['salonid']) and ($_GET['salonid'] != '')){
$salonid = clean_string($db_server, $_GET['salonid']);
//If connected, get Salons from database and write out
mysqli_select_db($db_server, $db_database);
$query = "SELECT ID, salon_name, address, postcode, telephone, email, website FROM salon WHERE ID=$salonid";
$result = mysqli_query($db_server, $query);
if (!$result) die("Query failed: " . mysqli_error($db_server));
while($row = mysqli_fetch_array($result)){
$str_result .= "<h2>" . $row[ 'salon_name'] . "</h2>";
$str_result .= "<p>" . $row['address'] . "</p>";
$str_result .= "<p>" . $row['postcode'] . "</p>";
$str_result .= "<p>" . $row['telephone'] . "</p>";
$str_result .= "<p>" . $row['email'] . "</p>";
$str_result .= "<p>" . $row['website'] . "</p>";
}
mysqli_free_result($result);
}else{
$str_result = "<h2>No salon selected</h2>";
}
}
echo $str_result;
?>
<?php
if(trim($_POST['submit']) == "Submit comment"){
//Get any submitted comments and insert
$comment = clean_string($db_server, $_POST['comment']);
if ($comment != '') {
$name=$_FILES['photo']['name'];
if ($name = "") $error .= "<p class='error'>You must upload an image!</p>";
$originalname=$_FILES['photo']['name'];
$type=$_FILES['photo']['type'];
if ($type=="image/jpeg") $type=".jpeg"; //if true change
else if ($type=="image/jpg") $type=".jpg";// if not true check this one
else if ($type=="image/png") $type=".png";
$name=uniqid() . $type;
$path="images/" . $name;
$tempname=$_FILES['photo']['tmp_name'];
$size=$_FILES['photo']['size'];
//Error checking
if ($size >1000000) $error .= "<p class='error'>Your image file is to big, it have to be less than 200 mb</p>";
if ($error=="") {
if (move_uploaded_file($tempname, $path)){
$uploadquery="INSERT INTO comments (comment, imagename, salonID, userID) VALUES ('$comment', '$path', $salonid, ". $_SESSION['userID'].")";
mysqli_query($db_server,$uploadquery) or die ("Insert failed " . mysqli_error($db_server) . " " . $uploadquery);
$message= "<h2>Thanks for your comment!</h2><p>Your upload was succesful</p>";
}
}
}
}
//Print out existing comment
$query = "SELECT * FROM comments JOIN users ON comments.userID = users.ID WHERE salonID=$salonid";
$result = mysqli_query($db_server, $query);
if (!$result) die("Database access failed: " . mysqli_error($db_server));
while ($row = mysqli_fetch_array($result)){
$str_comments .="<h2>" . $row['Username'] ."</h2>";
$str_comments .= "<p>" . $row['comment'] . "</p>";
$str_comments .="<img src='" . $row['imagename'] ."' />";
}
mysqli_free_result($result);
?>
<div id="form">
<table><form id='review' action='salonpage.php?salonid=<?php echo $salonid; ?>' method='post' enctype='multipart/form-data'>
<th><h2> Do you want to review the service you recieved?</h2></th>
<tr><td><textarea name="comment" rows="6" cols="40">Write something here!</textarea></td></tr>
<tr><td><input type='file' name='photo' accept='image/jpg, image/jpeg, image/png'/></td></tr>
<br/>
<tr><td><input type='submit' id='submit' name='submit' value='Submit comment' /></td></tr>
</form></table>
<?php echo $message;
echo $str_comments; ?>
</div>
<?php mysqli_close($db_server); ?>
if ($comment != '') {
$name=$_FILES['photo']['name'];
if ($name = "") $error .= "<p class='error'>You must upload an image!</p>";
$originalname=$_FILES['photo']['name'];
in this code you are using
$name = ""
which is an assignment operator you need to use comparison operator within if condition either ==or ===
I think, you are talking about $error, if i am correct then you did not echo $error variable in your above mention code. One more thing add else part on if block "if ($comment != '')" else {$error.="no comment entered" }

Categories