How to display a certain details from database - php

I have this code for search in mysql and It is working but when I run and entered a account number it display all accounts in my database
I just want to display details for the account number that I inquire on.
what should I change or add? I don't know
<?php
echo "<h2>Search Results:</h2><p>";
//If they did not enter a search term we give them an error
if ($find == "Account_Number")
{
echo "<p>You forgot to enter a search term!!!";
exit;
}
// Otherwise we connect to our Database
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());
// We perform a bit of filtering
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);
//Now we search for our search term, in the field the user specified
$data = mysql_query("SELECT Account_Number, Name, Balance FROM memaccounts WHERE ID
LIKE'%$find%'");
//And we display the results
while($result = mysql_fetch_array( $data ))
{
echo $result['Account_Number'];
echo " ";
echo $result['Name'];
echo "<br>";
echo $result['Balance'];
echo "<br>";
echo "<br>";
}
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query...<br><br>";
}
//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
//}
?>

$data = mysql_query("SELECT Account_Number, Name, Balance FROM
memaccounts WHERE Account_Number = '" . trim($find) . "' ");
1.where with Ac no.
Suggestion : Use mysqli_*

Related

Search Using PHP displays all Database Data

I am trying to search some data from a database. The search works fine, however if I click on search without entering anything into the form, it displays all the data on the database. Anyway I can fix this?
This is my php code.
$link=mysqli_connect("localhost","root","");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$db_selected = mysqli_select_db($link,"AnimalTracker1");
if (!$db_selected)
{
die ("Can\'t use test_db : " . mysqli_error($link));
}
$searchKeyword = $_POST['find']; // Sanitize this value first !!
$sql=mysqli_query($link, "Select * FROM Locations WHERE `Animal_Type` LIKE '%$searchKeyword%' ");
if ($sql == FALSE)
{
die($sql." Error on query: ".mysqli_error($link));
}
while($result = mysqli_fetch_array($sql))
{
echo $result ['Animal_Type'];
echo "<br>";
echo $result ['Latitude'];
echo "<br> ";
echo $result ['Longitude'];
echo " <br>";
echo $result ['Seen'];
echo " <br> ";
echo $result ['Time'];
echo "<br> ";
echo "<br> ";
}
//}
?>
Just make sure $searchKeyword has a (valid) value
$link=mysqli_connect("localhost","root","");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$db_selected = mysqli_select_db($link,"AnimalTracker1");
if (!$db_selected)
{
die ("Can\'t use test_db : " . mysqli_error($link));
}
// checks to see if $_POST['find'] is actually set.
if ( array_key_exists('find',$_POST) )
{
$searchKeyword = $_POST['find']; // Sanitize this value first !!
// sanitize $searchKeyword here
}
// checks to see if $searchKeyword has no value, or just contains empty space
if ( empty(trim($searchKeyword)) )
{
echo "You must enter a search term";
}
else
{
$sql=mysqli_query($link, "Select * FROM Locations WHERE `Animal_Type` LIKE '%$searchKeyword%' ");
if ($sql == FALSE)
{
die($sql." Error on query: ".mysqli_error($link));
}
while($result = mysqli_fetch_array($sql))
{
echo $result ['Animal_Type'];
echo "<br>";
echo $result ['Latitude'];
echo "<br> ";
echo $result ['Longitude'];
echo " <br>";
echo $result ['Seen'];
echo " <br> ";
echo $result ['Time'];
echo "<br> ";
echo "<br> ";
}
}
?>
Try removing the "%" from either the back or the front of the $searchKeyword in the query and I guess it should do the work.
"%" is used when match all or none. So if you send an empty string it will return the whole database.

Search results displaying all information in table

I am trying to search some data from a database and display it. However whenever I click on 'search' all the results in the table are displayed. Any way I can display only the information the user is searching?
This is the html code for the form.
<h2> Search </h2>
<form action = "search.php" method = "post" >
Search for: <input type = "text" name ="find" placeholder="Animal Type/Date"><span class="required"</span> in
<select NAME = "field">
<Option VALUE = "Animal Type"> Animal Type</option>
<Option VALUE = "dateseen"> Date Required</option>
</Select>
<input type= "hidden" name = "searching" value ="yes"/>
<input type= "submit" name = "search" value ="Search"/>
</form>
This is the PHP code I'm using.
$link=mysqli_connect("localhost","root","");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$db_selected = mysqli_select_db($link,"AnimalTracker1");
if (!$db_selected)
{
die ("Can\'t use test_db : " . mysqli_error($link));
}
//$find = strtoupper($find);
//$find = strip_tags($find);
//$find = trim($find);
$sql=mysqli_query($link, "Select * FROM Locations ");
if ($sql == FALSE)
{
die($sql." Error on query: ".mysqli_error($link));
}
while($result = mysqli_fetch_array($sql))
{
echo $result ['Animal Type'];
echo "<br>";
echo $result ['Latitude'];
echo "<br> ";
echo $result ['Longitude'];
echo " <br>";
echo $result ['Seen'];
echo " <br> ";
echo $result ['Time'];
echo "<br> ";
echo "<br> ";
}
//}
?>
For that you need to first grab the find field of your text box like this..
$searchKeyword = $_POST['find']; // Sanitize this value first !!
Next you need to pass it to your query... Change your yourcolumn to suit your column name.
$sql=mysqli_query($link, "Select * FROM Locations WHERE `yourcolumn` LIKE '%$searchKeyword%' ");
EDIT :
You could grab both fields and do a check..
if(!empty($_POST['find']) && !empty($_POST['field']))
{
//do your query like..
$searchKeyword = $_POST['find']; // Sanitize this value first !!
$searchKeyword2 = $_POST['field']; // Sanitize this value first !!
$sql=mysqli_query($link, "Select * FROM Locations WHERE `yourcolumn` LIKE '%$searchKeyword%' AND `yourcolumn2` LIKE '%$searchKeyword2%' ");
}
else
{
echo "The search criteria cannot be empty !";
}

Basic search engine not working

Hello i managed to make a simple search engine its basic so i started working on that..
But i get Access Forbidden and dont know why can someone help me?
with a form i give the $find the word i search and it should search for it..
thx for your time!`
<?php
if ($searching =="yes")
{
echo "Results";
if ($find == "")
{
echo "You forgot to enter a search term";
exit;
}
mysql_connect("localhost","Anton","zouzou13") or die(mysql_error()); mysql_select_db("Ptyxiakh") or die(mysql_error());
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);
$data =mysql_query("SELECT * FROM documents WHERE keywords LIKE'%$find%'");
while($result = mysql_fetch_array( $data ))
{
echo $result['document_name'];
echo " ";
echo $result['first_paragraph'];
echo "<br>";
}
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>"; }
echo "<b>Searched For:</b> " .$find; } ?>
ok i made it simpler to see if it works like this:
<?php
error_reporting(E_ALL);
mysql_connect("localhost","Anton","zouzou13") or die(mysql_error());
mysql_select_db("Ptyxiakh") or die(mysql_error());
//Now we search for our search term, in the field the user specified
$data = mysql_query("SELECT * FROM documents WHERE keywords LIKE 'helmets'");
while($result = mysql_fetch_array( $data ))
{
echo $result['document_name'];
echo " ";
echo $result['first_paragraph'];
echo "<br>";
}
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
?>
But i get the "Sorry, but we can not find an entry to match your query<br><br>"; so i cant connect to database?its just wont work :(
With PDO it should be like this??:
<?php
//This is only displayed if they have submitted the form
if ($searching =="yes")
{
echo "<h2>Results</h2><p>";
//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}
$pdo = new PDO('mysql:host=localhost;dbname=ptyxiakh', 'Anton', 'zouzou13');
// Otherwise we connect to our Database
//mysql_connect("localhost","Anton","zouzou13") or die(mysql_error());
//mysql_select_db("Ptyxiakh") or die(mysql_error());
// We preform a bit of filtering
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);
//Now we search for our search term, in the field the user specified
//$data = mysql_query("SELECT * FROM documents WHERE keywords LIKE'%$find%'");
$data= $pdo->query("SELECT * FROM documents WHERE keywords LIKE'%$find%'");
//And we display the results
while($row = $statement->fetch(PDO::FETCH_ASSOC))
{
echo htmlentities($row['document_name']);
echo " ";
echo htmlentities($row['first_paragraph']);
echo "<br>";
}
//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
}
?>
i made it work its pretty basic and i guess it has deprecated thingys but il upload for ppl that search for things...
just make a simple form and then make a php file
<?php
error_reporting(E_ALL);
//This is only displayed if they have submitted the form
echo "<h2>Results</h2><p>";
$find =$_POST["find"];
//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}
// Otherwise we connect to our Database
mysql_connect("localhost","Anton","zouzou13") or die(mysql_error());
mysql_select_db("Ptyxiakh") or die(mysql_error());
// We preform a bit of filtering
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);
//Now we search for our search term, in the field the user specified
$data = mysql_query("SELECT * FROM documents WHERE keywords LIKE'%$find%'");
//And we display the results
while($result = mysql_fetch_array( $data ))
{
echo '<p> <strong>',$result['document_name'], '</strong> <br> ', $result['first_paragraph'],'... <br> </p>';
}
//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
?>
This worked for me :) thx!

creating a page that displays ID info on a template

Updated with suggestion by others but still seem to be stuck.
I'm using this php code here to display info from my database using the ID. I created a link on my main page that looks like this.
<h1><?php echo $row_getDisplay['title']; ?></a></h1>
I have so when they click on the title of the article that it takes them to my php fiel which I named fetch.php and the code below is what is in there. I have built this around someone else's work. For some reason I can't get passed the first "else" statement. so I keep getting "you must select a valid location" I'm fairly new to php so I don't really understand why the code is failing.
<?php require_once('Connections/XXXXXX.php'); ?>
<?php
if (isset($_GET['id']) == false) // check if id has been set
{
echo "You must select a location"; // if not, display this error
exit;
} else {
$id = (int) $_GET['id'];
if (is_numeric($id) == false)
**{
echo "You must select a valid location.";
} else {**
mysql_select_db($database_XXXXXX, $XXXXXX);
$query = MYSQL_QUERY("SELECT * FROM news WHERE post_id ");
if (MYSQL_NUM_ROWS($query) == "1")
{
$fetch = MYSQL_FETCH_ARRAY($query); // set $fetch to have the values from the table
echo "Title: " . $fetch['title'] . "<BR>"; // output the info
echo "Blog: " . $fetch['blog_entry'] . "<BR>"; // etc...
echo "Author: " . $fetch['author'] . "<BR>"; // etc...
} else {
echo "No match in database found."; // if no match is found, display this error
}
}
}
Any help is appreciated. If you are able to find a better solution for me that would be great.
You shouldnt use $HTTP_GET_VARS its deprecated and unless its turned on it wont be populated. use $_GET instead.
if (isset($_GET['id']) == false)
Use $_GET for your if statement:
if (isset($_GET['id']) == false)
Also, you need to convert your $_GET value to an integer, because it is currently a string.
Right after that if statement above, in the else, put this:
$id = (int) $_GET['id'];
That way your is_numeric() will work properly.
Try this;
<?php
require_once('Connections/XXXXXX.php');
if (isset($_GET['id'])) // check if id has been set
{
$id = $_GET['id'];
if (is_numeric($id) == false)
{
echo "You must select a valid location.";
} else {
mysql_select_db($database_XXXXXX, $XXXXXX);
$query = MYSQL_QUERY("SELECT * FROM news WHERE locationid = 'news.post_id' ");
if (MYSQL_NUM_ROWS($query) == "1")
{
$fetch = MYSQL_FETCH_ARRAY($query); // set $fetch to have the values from the table
echo "Title: " . $fetch['title'] . "<BR>"; // output the info
echo "Blog: " . $fetch['blog_entry'] . "<BR>"; // etc...
echo "Author: " . $fetch['author'] . "<BR>"; // etc...
} else {
echo "No match in database found."; // if no match is found, display this error
}
}
}
else{
echo "You must select a location"; // if not, display this error
exit;
}
?>
Also, I need a clarification about news.post_id, from where are you grabbing this?

PHP code not working in IE8

I've got a baffling IE8 PHP problem. The code below works fine in chrome and FF but not IE8. The form is submitting, errors are displayed if no search term or check-boxes selected, and if both db searches fail the No Actors and No Movies found echo's are both displayed. But if either one of the searches is successful nothing is displayed, not even the Actors/Movies found echo which has me stumped.
Here's the code:
<?php
if($_POST[submitbutton]){
$search = trim(mysql_real_escape_string($_POST[search]));
if(!$search){
echo "Please enter a search term!";
}else if(!$_POST['checkbox']){
echo "Please select at least one database to search!";
}else{
//search names
if(in_array("actors", $_POST['checkbox'])){
$query = mysql_query("SELECT name_id, realname, mainalias FROM names WHERE realname LIKE '%$search%' OR mainalias LIKE '%$search%'");
if(mysql_num_rows($query)==0){
echo "<h2>No Actors by that name found!</h2>", "<p>";
}else{
echo "<h2>Actors Found:</h2>";
while ($record = mysql_fetch_assoc($query)){
$realname = $record['realname'];
$name_id = $record['name_id'];
echo "<a href='index.php?page=name&id=$name_id'>", $realname, "</a><hr>";
}
}
}
//search titles
if(in_array("movies", $_POST['checkbox'])){
$query = mysql_query("SELECT title_id, title FROM titles WHERE title LIKE '%$search%'");
if(mysql_num_rows($query)==0){
echo "<h2>No Movies by that name found!</h2>", "<p>";
}else{
echo "<h2>Movies Found:</h2>";
while ($record = mysql_fetch_assoc($query)){
$title = $record['title'];
$title_id = $record['title_id'];
echo "<a href='index.php?page=title&id=$title_id'>", $title, "</a><br>";
echo "<hr>";
}
}
}
}
} //end post submitbutton
?>
Should
$search = trim(mysql_real_escape_string($_POST[search]));
be
$search = trim(mysql_real_escape_string($_POST['search']));
I don't know if that has anything to do with your problem, but it jumper out at me.
<?php
if(isset($_POST['submitbutton'], $_POST['search'])){ //use isset for check exists vars
$search = trim(mysql_real_escape_string($_POST['search'])); // ['POST vars']
if(!$search){
echo "Please enter a search term!";
}else if(!isset($_POST['checkbox'])){
echo "Please select at least one database to search!";
}else{
//search names
if(in_array("actors", $_POST['checkbox'])){
$query = mysql_query("SELECT name_id, realname, mainalias FROM names WHERE realname LIKE '%$search%' OR mainalias LIKE '%$search%'");
if(mysql_num_rows($query)==0){
echo "<h2>No Actors by that name found!</h2>", "<p>";
}else{
echo "<h2>Actors Found:</h2>";
while ($record = mysql_fetch_assoc($query)){
$realname = $record['realname'];
$name_id = $record['name_id'];
echo "<a href='index.php?page=name&id=$name_id'>", $realname, "</a><hr>";
}
}
}
//search titles
if(in_array("movies", $_POST['checkbox'])){
$query = mysql_query("SELECT title_id, title FROM titles WHERE title LIKE '%$search%'");
if(mysql_num_rows($query)==0){
echo "<h2>No Movies by that name found!</h2>", "<p>";
}else{
echo "<h2>Movies Found:</h2>";
while ($record = mysql_fetch_assoc($query)){
$title = $record['title'];
$title_id = $record['title_id'];
echo "<a href='index.php?page=title&id=$title_id'>", $title, "</a><br>";
echo "<hr>";
}
}
}
}
} //end post submitbutton
?>

Categories