I don't understand what's the problem with the code. It is supposed to work. There is data in table but the search is still not producing any results. The Search bar remaining still and no changes before or, after entering any data in the search bar.
Here is the code :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Search Example</title>
</head>
<body>
<form action="index.php" method="post">
<input type="text" name="search" placeholder="Search" />
<input type="submit" value="submit" />
</form>
<?php
$con = mysqli_connect("localhost","root","") or die("Could not connect");
mysqli_select_db($con ,"project") or die(mysqli_error());
if(isset($_POST['search']))
{
$search =$_POST['search']; // searchq contains characters which is typed
in the search
$search = preg_replace("#[^0-9a-z]#i","",$search); //filtering the
conditions */
$query = mysqli_query($con, "SELECT * FROM admin WHERE name LIKE
'%$search%'");
//most important condition line for the search
$count = mysqli_num_rows($query); // To count the selected Rows
if($count==0)
{
echo"<h2>"."No Data Found"."</h2>";
}
else
{
while($row = mysqli_fetch_array($query))
{
echo "<tr>".
"<td>".$row['username'] ."</td>".
"<td>".$row['password'] ."</td>".
"</tr>";
}
}
}
?>
</body>
</html>
Database name is project and table name is admin with Id, username and password as columns.
Use like this
$query = mysqli_query($con, "SELECT * FROM `admin` WHERE `username` LIKE
'%{$search}%'");
Do it like this:
$query = mysqli_query($con, "SELECT * FROM admin WHERE name LIKE
'%{$search}%'");
By surrounding variable in {} you can specify that only $title is a variable and the double-quote string will ensure that this variable gets expanded to its value.
Related
This question already has answers here:
search data from html input in mysql
(2 answers)
Closed 1 year ago.
I am trying to search the username from table by using form method in HTML with submit button, and what i really want is that when user write his email address in input box and press submit, the query should echo username associated with that email address.
But the problem is that when I press search button, it is showing all the usernames on that table instead of only one. My table "payments" containing the following values: id, product id, payer_email, username, password.
My code is as under. Thanks in advance.
<?php
// Database Connection String
$con = mysql_connect($db_hostname,$db_username,$db_password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($db_database, $con);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form action="" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" value="Submit" />
</form>
<?php
if (!empty($_REQUEST['term'])) {
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM payments WHERE payer_email LIKE '%".$term."%'";
$r_query = mysql_query($sql);
while ($row = mysql_fetch_array($r_query)){
echo 'Username: ' .$row['username'];
}
}
?>
</body>
</html>
try this
<?php
if (!empty($_REQUEST['payer_email'])) {
$term = mysql_real_escape_string($_REQUEST['payer_email']);
$sql = "SELECT * FROM payments WHERE payer_email ='{$term}'";
$r_query = mysql_query($sql);
$row = mysql_fetch_assoc($r_query)
echo 'Username: ' .$row['username'];
}
?>
I am brand new to PHP, or database programming in general. For a project I have to query a bookstore database for book info (a very small database) and display it on the following page. Below is the code for my bookstore search page:
<?php
$con = mysqli_connect("localhost", "root", "root") or die("Error connecting to database: ".mysqli_error());
mysqli_select_db($con, "bookstore") or die(mysqli_error());
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Book Store</title>
</head>
<body>
<td><h1>Book Search</h1> </td>
<table width="100%" border="0">
<tr>
<form method="post" action="search.php?go" id="searchform">
<input type="text" name="name">
<input type="submit" name="submit" value="Search By Title">
</form>
<form method="post" action="search.php?go" id="searchform">
<input type="text" name="category">
<input type="submit" name="submit" value="Search By Category">
</form>
</tr>
</table>
</body>
</html>
And the following is a simple search.php code that query's my database and returns results. However I am unable to see any results. The only thing that shows up is "Book Title Search Results" with nothing below. Which obviously means my problem is in my while loop.
<?php
$con = mysqli_connect("localhost", "root", "root") or die("Error connecting to database: ".mysqli_error());
mysqli_select_db($con, "bookstore") or die(mysqli_error());
?>
<!DOCTYPE html>
<html>
<head>
<title>Search Results</title>
<meta http-equic="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
if (isset($_POST['name'])){
$query = $_POST['name'];
$sql = mysqli_query($con, "SELECT * FROM books
WHERE (`title` LIKE '%".$query."%')") or die(mysqli_error($con));
if (mysqli_num_rows($sql) > 0) {
echo "</br> Book Title Search Results </br>";
while ($row = mysqli_fetch_array($sql, MYSQL_ASSOC)) {
echo "</br>Title: " .$row['title']. ", Author: " .$row['author'].", Year: " .$row['year'] . ", Price: $" .$row['price'] ."</br>";
echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['cover'] ).'"/>';
}
}else{ // if there is no matching rows do following
echo "No results";
}
}
?>
</body>
</html>
I have 6 columns in my database: title, author, year, price, category, image (BLOB FILE), and I have checked naming in my query functions but cannot figure anything out. Can anyone push me in the right direction or show me what I'm doing wrong? I'm using MAMP web server.
There is a typo in your code. Use MYSQLI_ASSOC instead of MYSQL_ASSOC. The rest of the code is correct.
try
$con = mysqli_connect("localhost", "root", "root", "bookstore") or die("Error connecting to database: ".mysqli_error());
In your query, remove the braces after the where:
$sql = mysqli_query($con, "SELECT * FROM books WHERE title LIKE '%$query%'") or die(mysqli_error($con));
Then fetch the results with:
while ($row = mysqli_fetch_assoc($sql)) {
//code
}
I am trying to input and display the data on same page through mysqli but it shows an error "Couldn't fetch mysqli on line" and not unable to display the records.
<?php
$db=mysqli_connect("localhost","root","","abc") or die("Not connected".mysqli_error());
$database=mysqli_select_db($db,'abc') or die("Database not found".mysqli_error());
if(isset($_POST['submit'])){
$roll=$_POST['roll'];
$name=$_POST['name'];
$ins=mysqli_query($db,"insert into abc1 (roll,name)values('$roll','$name')");
}
mysqli_close($db);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form method="post">
Roll No <input type="text" name="roll" />
Name <input type="text" name="name" />
<input type="submit" name="submit" value="Submit" />
</form>
<table><tr>
<td>Roll No.</td>
<td>Name</td>
</tr>
<?php $query=mysqli_query($db,"select * from abc1");
$result=mysqli_query($db,$query);
$id=0;
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC))
{
?>
<tr><td><?php echo $row['roll']; ?></td>
<td><?php echo $row['name']; ?></td>
<?php } ?>
</tr></table>
</body>
</html>
Any help would be appreciated.
Thanks in Advance
You are doing mysqli_query twice.
Try this.
$query = "select * from abc1";
$result = mysqli_query($db, $query);
You are calling mysqli_query function twice:
$query = mysqli_query($db,"select * from abc1");
$result = mysqli_query($db,$query);
Error: Couldn't fetch mysqli on line
Means, you are passing a a resource id in mysqli_fetch_array function instead of Query Statement. so you just need to use this as:
$query = "select * from abc1";
$result = mysqli_query($db,$query);
Side Note:
There is no need to connect db again:
$database=mysqli_select_db($db,'abc')
mysqli_connect() already connected your db.
im just playing around when creating a mini search engine, i have results in my sql database, i have added the corrent host user and password with the correct db selected, but when i search something on the site i just get nothing, just a white screen with the search bar at the top. I am not even getting an error saying nothing could be found in the data base?
If i enter the wrong details i get an error, so i know the details are correct.
Any ideas?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Search Engine - Search</title>
<body>
<h2>Search Engine</h2>
<form action='./search.php' method='get'>
<input type='text' name='k' size='50' value='<?php echo $_GET['k']; ?>' />
<input type='submit' value='Search'>
</form>
<hr />
<?php
$k = $_GET['k'];
$terms = explode(" ", $k);
$query = "SELECT * FROM search WHERE ";
foreach ($terms as $each){
$i++;
if ($i == 1)
$query .= "keywords LIKE '%$each%' ";
else
$query .= "OR keywords LIKE '%$each%' ";
}
//connect
mysql_connect('localhost', 'user', 'password');
mysql_select_db('search');
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0) {
while ($row = mysql_fetch_assoc($query)){
$url = $row['url'];
$title = $row['title'];
$keywords = $row['keywords'];
echo "<h1><a href='$url'>$title</a></h1>
$keywords<br /><br />";
}
}
?>
</body>
</head>
I am trying to allow admin side of my website to edit member detail.
I have created search for admin so s/he can search users via their name or surname,
and I want to have edit link for each search result that it will come back.
just to let you know I really don't know how to do it.
This is the code I have
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>search</title>
<script src="jquery.js"></script>
<script type="text/javascript">
function serachq ()
{
var searchTxt= $("input[name='search']").val();
$.post("search.php", {searchVal: searchTxt}, function(output){
$("#output").html(output);
});
}
</script>
</head>
<body>
<form action="index.php" method="post">
<input type="text" name="search" placeholder="search for members..." onkeydown="serachq();"/>
<input type="submit" value=">>" />
<br/>
<br/>
<div id="output">
</div>
</body>
</html>
and this is PHP code
<?php require_once("db_connection.php"); ?>
<?php
$output="";
if (isset($_POST['searchVal']))
{
$searchq=$_POST['searchVal'];
$searchq= preg_replace("#[^0-9a-z]#i","",$searchq);
$query= mysql_query("SELECT * FROM member WHERE first_name LIKE '%$searchq%' OR last_name LIKE'%$searchq%'") or die ("could not search");
$count = mysql_num_rows($query);
if($count == 0){
$output="there was no serach results!";
}
else
{
while ($row= mysql_fetch_array($query))
{
$fname=$row ['first_name'];
$lname=$row['last_name'];
$id= $row['member_id'];
$output.='<div>'.$fname.' '.$lname.' '.$id.'Edit Subject </div>';
}
}
}
echo ($output);
?>
Really you need to write the question's better in the future. Specifically, if you are getting an error provide the error. If you expect a certian result, tell us what you get instead. That said the thing that stands out is:
<?php echo $query ['$id'];
Where I imagine you meant:
<?php echo $query[$id];
Here is that part of the code cleaned up:
while ($row = mysql_fetch_array($query))
{
$fname = $row['first_name'];
$lname = $row['last_name'];
$id = $row['member_id'];
$output .= "<div>{$fname} {$lname} {$id} Edit Subject</div>";
}
If i understand your question properly, you can do using session, put $searchq in session and when you come back you can make the query form session
if (isset($_POST['searchVal']) || isset($_SESSION['serachq'])) {
if (isset($_POST['searchVal'])) {
$searchq = $_POST['searchVal'];
$_SESSION['serachq'] = $searchq;
} else {
$searchq = $_SESSION['serachq'];
}
}
or you can modify your link add-member.php?edit=<?php echo $query ['$id']; ?>&q=<?php echo urlencode()?>
and your back url should have the &q=<?php echo urlencode()?> part too then
if (isset($_POST['searchVal']) || isset($_GET['q'])) {
if (isset($_POST['searchVal'])) {
$searchq = $_POST['searchVal'];
} else {
$searchq = urldecode($_GET['q']);
}
}
`