mysql php search engine displaying nothing - php

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>

Related

PHP Search is not working

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.

PHP not returning mysql results

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
}

edit search result on PHP

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']);
}
}
`

User name won't display

I try to display the user log in to the memberadd.php where i want to add other members, however, it doesn't display, instead it is blank. I post both the member add code and the log in code, it has something to do how the memberadd php didn't received the variable from the other files?
this is the memberadd code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/chtml-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en" >
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="description" content="Web Programming :: Assignment 2" />
<meta name="Keywords" content="Web, programming" />
<title>Member Login</title>
</head>
<body>
<h1>My Member System</h1>
</body>
</html>
<?php
session_start();
require_once('sqlconnect.inc.php');
if(isset($_SESSION['membername']))
{
echo "<p>Welcome back". "<br />".$_SESSION['membername']."</p>";
$conn = #mysqli_connect($host,
$user,
$pswd,
$dbnm);
if (!$conn) {
echo "<p>Database connection failure</p>";
} else {
#mysqli_select_db($conn, $dbnm)
or die ("Database not available");
}
$query = "SELECT member_name FROM team";
$result = mysqli_query($conn, $query);
if(!$result) {
echo "<p>Error with: ", $query, "</p>";
} else {
echo "<table width='10%' border='1'>";
echo "<tr><th>Member</th></tr>";
while ($row = mysqli_fetch_assoc($result)){
echo "<tr>";
echo "<td>",$row["member_name"],"</td>";
echo "</tr>";
}
echo "</table>";
mysqli_free_result($result);
}
}
//
?>
below is the login page
<?php
session_start();
require_once('sqlconnect.inc.php');
if (isset($_POST["login"]))
{
$conn = #mysqli_connect($host, $user, $pswd);
if (!$conn) {
echo "<p>Database connection failure</p>";
} else {
$selectDatabase = #mysqli_select_db($conn,$dbnm)
or die("<p>The database is not available.</p>");
}
$email = $_POST['email'];
$passw = $_POST['password'];
$query = "SELECT member_email FROM team WHERE member_email = '$email' AND password = '$passw'";
$queryResult = #mysqli_query($conn,$query)
or die ("<p>Unable to execute query.</p>". "<p>Error code" . mysqli_errno($conn) .":" . mysqli_error($conn))."</p>";
if(mysqli_num_rows($queryResult) == 0) //user is not found
{
header('Location: login.php');
}else{
if(mysqli_num_rows($queryResult) == 1)
{
echo ("<p>User is found, Successful login!</p>");
echo('<p>member add </p>');
echo('<p>List/Remove member </p>');
echo('<p>Log out </p>');
exit();
$query2 = "SELECT member_name FROM team WHERE member_email = '$email' AND password= '$passw'";
$queryResult2 = #mysqli_query($conn, $query2)
or die ("<p>Unable to execute query.</p>". "<p>Error code" . mysqli_errno($conn) .":" . mysqli_error($conn))."</p>";
$array = mysqli_fetch_row($queryResult2);
$_SESSION['membername'] = $array[0];
}
else
{
echo"<p>Email and password do not match</p>";
echo'<p>Home page </p>';
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/chtml-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en" >
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="description" content="Web Programming :: Assignment 2" />
<meta name="Keywords" content="Web, programming" />
<title>Member Login</title>
</head>
<body>
<form id='login' action='login.php' method='POST'>
<fieldset >
<legend><h1>My Team System Log in Page</h1></legend>
<?php $email = isset($_POST['email']) ? filter_var($_POST['email'], FILTER_SANITIZE_STRING) : ''; ?>
<label for='email' >Email:</label>
<input type='text' name='email' id='email' maxlength="50" value="<?php echo $email; ?>" />
</div>
<br />
<div class="elements">
<label for='password' >Password:</label>
<input type='password' name='password' id='password' maxlength="50" />
</div>
<br />
<div class="submit">
<input type='submit' name='login' value='Login' />
<input type='reset' name='Submit' value='Clear' />
<br />
<div class="elements">
Home
</fieldset>
</form>
</body>
</html>
You set the session as membername but you then try to call name. As in, you should correct the bit where you display the user's name into $_SESSION['membername'].
Blank screen is usually a parse error...
echo "<p>Error with: ", $query, "</p>";
Should be
echo "<p>Error with: ".$query."</p>";
echo "<td>",$row["member_name"],"</td>";
Should be
echo "<td>".$row["member_name"]."</td>";
Well I can tell you about what I see:
You set the session of the member name like this
$_SESSION['membername'] = $array[0];
But in memberadd.php you retrieve it like this:
echo "<p>Welcome back". "<br />".$_SESSION['name']."</p>";
So you should change to the same name you set it to :
echo "<p>Welcome back". "<br />".$_SESSION['membername']."</p>";
3 errors I see -
1. your session_start() is not at the top/start of your code.
2. you echo before setting your session value
3. you use exit(); after your echo and before setting your session value.
also, you are wide open to sql injection, and you should hash your passwords.
I am sorry guys, I've made a mistake on the early file I did, which is the signup file where people registered, and I set the session name there ['name'] instead of ['membername']. Is all fine now.

Undefined index for ID with $_GET

Well, yet another undefined index appears :
I am trying to change a select row in a database, but so far it doesn't seem to work, I only get
Notice: Undefined index: EierID in C:\WampServer\www\Hundeklubben\ChangeO.php on line 19.
I have tried some fixes, but none worked.
<?php require_once('Connections/hundeklubb.php'); ?>
<!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>Endring av eier</title>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<?php
if(isset($_GET['EierID'])){ $name = $_GET['EierID']; }
//Tried with both $_GET and $_POST
?>
<?php
$UID = (int)$_GET['EierID'];
$query = mysql_query("SELECT * FROM eiere WHERE EierID = '$UID'") or die(mysql_error());
if(mysql_num_rows($query)>=1){
while($row = mysql_fetch_array($query)) {
$navn = $row['Navn'];
$bosted = $row['Bosted'];
}
?>
<form name="form1" action="update.php" method="POST" id="form1">
<input type="hidden" name="ID" value="<?=$UID;?>">
Navn: <input type="text" name="ud_navn" value="<?=$navn?>"><br>
Bosted: <input type="text" name="ud_bosted" value="<?=$bosted?>"><br>
<input type="Submit" value="Oppdater">
</form>
<?php
}else{
echo 'No entry found. Go back';
}
?>
<?php var_dump($UID); ?>
</body>
</html>
The var_dump gives me int 0. I'm not sure what it is supposed to be.
update.php
<?php require_once('Connections/hundeklubb.php'); ?>
<?php
$ud_ID = (int)$_POST["ID"];
$ud_navn = mysql_real_escape_string($_POST["ud_navn"]);
$ud_bosted = mysql_real_escape_string($_POST["ud_bosted"]);
$query="UPDATE eiere
SET navn = '$ud_navn', bosted = '$ud_bosted'
WHERE ID='$ud_ID'";
mysql_query($query)or die(mysql_error());
if(mysql_affected_rows()>=1){
echo "<p>($ud_ID) Record Updated<p>";
}else{
echo "<p>($ud_ID) Not Updated<p>";
}
?>
It is because $_GET['EierID'] is not set.
Try this :
$UID = isset($_GET['EierID'])?$_GET['EierID']:"";
In update.php also do the same thing : $ud_ID = isset($_POST["ID"])?$_POST["ID"]:"";
If your variable doesn't exist you will get an error trying to cast that int.
<?php
if(isset($_GET['EierID'])){
$name = $_GET['EierID'];
$UID = (int)$_GET['EierID'];
}else{
//set to 0 or any default value you want to set when EierID doesn't exists
$UID = 0;
}
?>

Categories