I am trying to make an error message appear if when a user searches, no results are found; however I am having an issue getting the error message to display.
Here is my 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=UTF-8" />
<title>PHP Test File</title>
<link href="phptestcss.css" rel="stylesheet" type="text/css" />
<link href="lightbox-style/lightbox.css" rel="stylesheet" type="text/css" media="screen"/>
<script src="js/prototype.js" type="text/javascript"></script>
<script src="js/scriptaculous.js?load=effects,builder" type="text/javascript"></script>
<script src="js/lightbox.js" type="text/javascript"></script>
</head>
<body>
<?php include 'header.html'; ?>
<?php
include 'databaseconnect.php';
//- Queries the table to get content from rows -
$QuerySelect = "SELECT thumbnail,image,title FROM homepage WHERE title LIKE '%".mysql_real_escape_string($_POST['search'])."%' ";
//----------------- QUERY the TABLE, store THE CONTENT IN A PHP VARIABLE -----------------
$result = mysql_query($QuerySelect);
//-------------- STORE THE CONTENT WITH AN ID = 1 IN A ARRAY -------------- -------------------
include 'logo.html';
?>
<div class="searchcontainer">
<form method="post" action="index.php">
<input type="text" class="input" name="search" size="40" placeholder="Search...">
<input type="submit" class="button" name="Submit" value="Search" id="Submit" >
</form>
</div>
<?php
$numrows = mysql_num_rows ($result );
if($numrows = 0 and isset($_POST['search'])){
echo '<div class="errorcontainer">';
echo '<div class="erroricon">';
echo "!";
echo '</div>';
echo "Your search returned no results, try searching for a website category e.g Sports or Cars";
echo '</div>';
}
else{
while ($row =mysql_fetch_object ($result)) {
//- Main Content -
echo '<div class="wrapper">';
?>
<?php
//--------- GET THE POST HTML VARIABLES. PUT INTO PHP VARIABLES -----------------------------
$search = $_POST['search' ];
//--------------- IF THE ROW COUNT IS NOT = 0, WHILE THERE ARE ROWS WITH CONTENT, OUTPUT HTML ----------
?>
<?php
echo '<div class="imagecontainer">';
echo "<img src=\"".$row->thumbnail."\" width=\"300\" height=\"300\"border=\"0\"/>";
echo '<div class="titlecontainer">';
echo "".$row->title."";
echo'</div>';
echo'</div>';
echo'</div>';
}
}
?>
</body>
</html>
At the minute, the content displays when you search for something that exists, however if you search for something that doesn't exist - the error message that I have set does not display, and I am unsure why.
You have misplaced following code
$numrows = mysql_num_rows ($result );
should be before following line of code
if($numrows = 0 and isset($_POST['search'])){
Try this one it should work,I have not included the HTML tags but the structure structure should be right.
$querySelect = "SELECT thumbnail,image,title FROM homepage WHERE title LIKE '%".mysql_real_escape_string($_POST['search'])."%' ";
$result = mysql_query($querySelect);
$numrows = mysql_num_rows($result); //this should be on before the if statement
if($numrows == 0 && isset($_POST['search'])){ //replaced and with &&
echo '<div class="errorcontainer">';
echo '<div class="erroricon">';
echo "!";
echo '</div>';
echo "Your search returned no results, try searching for a website category e.g Sports or Cars";
echo '</div>';
} else{//The rest of the content.}
Related
i am trying to buid a Php site that show data from my MySQl database.
and i think im almost there, everything works except the Pictures.
i cant get my php site to show the pictures with the picture reference from Sql database.
<!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">
<?php
$con=mysqli_connect("localhost","root","","db1");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<head>
<link rel="stylesheet" href="styles.css">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>untitled</title>
</head>
<body>
<div class="content">
<?php
$sql = "SELECT id, Producent, Model, kategori FROM tb1";
$result = mysqli_query($con,"SELECT * FROM `tb1");
echo "<table>";
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>";?> <img scr="<?php echo $row["Billedurl"]; ?>"/> <?php echo " </td>";
echo "<td>" .$row["Producent"] .$row["Model"]; echo "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
<!-- end .content --></div>
<!-- end .container --></div>
<div class="footer"><br>
<!-- end .footer --></div>
</body>
</html>
You need to select all the columns you are going to use, so if you need Billedurl, you should change:
$sql = "SELECT id, Producent, Model, kategori FROM tb1";
to:
$sql = "SELECT id, Producent, Model, kategori, Billedurl FROM tb1";
Now the value of that column will be available in $row["Billedurl"].
Edit: It seems that now you have the correct value in your html, but the path to the image is not correct as it is a relative path.
You should prefix your variable with the correct folder so that the image is found by the browser. That can be as simple as just using an absolute path but that depends on where the pic/ directory is located.
So if your variable contains pic/l_jabra_evolve80.jpg" and the pic/ folder is on the root of the web-server, you can do something like:
# before the loop
$imagePrefix = '/';
# in the loop
... <img scr="<?php echo $imagePrefix . $row["Billedurl"]; ?>"/> ...
Now the browser will try to fetch the image from /pic/l_jabra_evolve80.jpg.
You need to do little bit of debugging:
first change little bit in your query
$sql = "select * FROM tb1";
$result = mysqli_query($con, $sql);
Then try to check print_r($result) and check if you are getting everything here or not.
My knowledge of php is very limited, although I am aware that mysql functions are depreciated but it doesn't matter for the purpose of this project.
I have a table (enisatquestion) with training questions, and file paths to 14 videos stored on my pc which I want to display using localhost.
My table structure and an example of one of the rows in my table are as follows:
Columns are:
eNISATID (Auto-increment)
eNISATQuestion (Training question)
eNISATVideo (File path to video)
An example of a Row:
1
Can you login?
http://localhost\Tna\eNISAT\LoginTutorial.wmv
Here is my code, I am getting an error;
mysql_fetch_assoc() expects at least 1 parameter, 0 given in C:\wamp\www\Tna\eNISATVids.php on line 20
Can anyone please help me with this, I have researched a lot of ways to display this, but I am struggling with it as I have never worked with videos in php before. The videos are also wmv format. Or can anyone give me a more suitable example
Many Thanks
<?php
session_start();
include_once 'dbconnect.php';
if(!isset($_SESSION['user']))
{
header("Location: index.php");
}
//maintain SESSION user_id
$res=mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);
//Select video name and question
$query = "SELECT eNISATQuestion, eNISATVideo FROM enisatquestion";
$result = mysql_query($query);
if($result === FALSE) {
die(mysql_error()); // TODO: better error handling
}
$enisatquestion = "<table >";
while ( $row = mysql_fetch_assoc($result) ) {
$enisatquestion .= "<tr><td><a href='{$row['eNISATVideo']}'>{$row['eNISATQuestion']}</a></td></tr>";
}
$enisatquestion .= "</table>";
echo $enisatquestion;
?>
<!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>Welcome - <?php echo $userRow['username']; ?></title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header">
<div id="left">
<label>NHSCT eNISAT Tutorials</label>
</div>
<div id="right">
<div id="content">
Welcome <?php echo $userRow['forename']; ?> Sign Out
</div>
</div>
</div>
</body>
</html>
The answer is simple:
$query = "SELECT eNISATName, eNISATVideo FROM enisatquestion";
$result = mysql_query($query);
$enisatquestion = "<table>";
while ( $r = mysql_fetch_assoc($result) ) {
$enisatquestion .= "<tr><td><a href='{$r['eNISATVideo']}'>{$r['eNISATName']}</a></td></tr>";
}
$enisatquestion .= "</table>";
echo $enisatquestion;
?>
Notice the new $result variable that is passed to mysql_fetch_assoc.
First time posting here (so please be gentle, as I am a relative PHP newbie).
I am building an intranet for our company and one of the things I need to do is to create a form that lists all outstanding sales orders (pulled from our accounting database) and provides a "submit" button beside each one to create the relevant work order.
Here is the code:
<div class="report_column">
<div class="report_header">
<div class="report_column_title" style="width:150px;margin-left:5px">ship date</div>
<div class="report_column_title" style="width:200px">customer</div>
<div class="report_column_title" style="width:140px;text-align:right">item</div>
<div class="report_column_title" style="width:120px;text-align:right">quantity</div>
</div>
<?php
// Open connection
include 'includes/dbconnect.php';
// Perform query
$result = mysqli_query($con,"SELECT * FROM tSalOrdr ORDER BY dtShipDate ASC");
// Retrieve results
while($row = mysqli_fetch_array($result)) {
$order = $row['lId'];
if ($row['bCleared'] == 0) {
$shipdate = substr($row['dtShipDate'], 0,10);
$customer = $row['sName'];
$po = $row['sComment'];
echo '<div class="report_item" style="width:750px";>';
echo '<form class="form" action="index.php?page=form&item=create_work_order" method="POST">';
echo '<div class="report_item_date" style="width:120px">'.$shipdate.'</div>';
echo '<div class="report_item_name" style="width:530px">'.$customer;
echo '<input type="hidden" name="po" value="'.$po.'" />';
echo '<input type="submit" class="submit" style="height: 25px;width:100px;margin:0px;padding:0px;margin:0px" value="work order"/>';
echo '</div>';
$result2 = mysqli_query($con,"SELECT * FROM tSOLine WHERE lSOId=$order ORDER BY sDesc ASC");
while($row = mysqli_fetch_array($result2)) {
if ($row['dRemaining'] <> 0) {
echo '<div class="report_item_details">';
echo '<div class="report_item_item">'.$row['sDesc'].'</div>';
echo '<div class="report_item_quantity">'.$row['dRemaining'].'</div>';
echo '</div>';
}
}
echo '</form>';
echo '</div>';
}
}
// Close connection
mysqli_close($con);
?>
</div>
What happens when I do this is that the first "submit" button will, for some reason, send me back to "index.php". The other buttons will load the correct page, however, they do not POST the required value.
Is there something I am doing wrong or is this something that needs different methodology than what I am currently using? My research on this seems to indicate that perhaps I should use javascript or an array to deal with this, but, having never dealt with either, I am not sure how to proceed. Any pointers would be appreciated.
Thanks.
#maniteja: The index.php is as follows:
<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="language" content="en" />
<title></title>
<link href="css/main.css" rel="stylesheet" type="text/css" />
<link href="css/forms.css" rel="stylesheet" type="text/css" />
<link rel="icon" type="image/ico" href="favicon.ico" />
<script type="text/javascript" src="scripts/jquery-2.1.0.min.js" ></script>
<script type="text/javascript" src="scripts/tabcontent.js" ></script>
</head>
<body>
<!-- BEGIN MAIN CONTENT -->
<div id="wrapper">
<!-- BEGIN HEADER -->
<div id="header">
<img src="images/logo.jpg">
</div>
<!-- END HEADER -->
<!-- BEGIN MAIN MENU -->
<div id="leftcolumn">
<?php include 'includes/menu.php'; ?>
</div>
<!-- END MAIN MENU -->
<!-- BEGIN CONTENT FRAME -->
<div id="rightcolumn">
<div id="content_area">
<?php
if (isset($_GET['page']))
{$page = $_GET['page'];
include('pages/' . $page . '.php');}
else {include('pages/home.php');}
?>
</div>
</div>
<!-- END MAIN CONTENT -->
</body>
</html>
I've made seventeen other forms with it, so I don't think that it is the problem. I'm hoping that this is just a typo or a logic error on my part.
u can use
<button type='submit' class='' name='' onclick=''>Submit</button>
Try to do onclick. See my example below.
input type="button" name="submit" onclick='location.href="index.php?id=$id"'
AS far as I understand, you are making your forms go to index.php:
echo '<form class="form" action="index.php?page=form&item=create_work_order" method="POST">';
Can you explain what is your expected behavior when a button is pushed?
So here's an example code I use to show all the categories that are linked to a certain postID.
For example post 1 has the categories: Apple, Green and Yellow linked to it.
But I can't seem to fetch the data correctly since it has already been fetched once at the top i can't do a proper while loop at the Categories: part of my code where I try to do a while loop. The while loop works and fetches all the categories except the First one and also when I place the while loop the
$row['postTitle']
and
$row['postCont']
won't appear anymore because it's being skipped. How would I fix something like this? Thanks.
<?php require('includes/config.php');
$stmt = $db->prepare(" SELECT *
FROM blog_posts
LEFT JOIN blog_posts_categories ON blog_posts.postID=blog_posts_categories.postID
INNER JOIN blog_categories ON blog_posts_categories.catID=blog_categories.catID
WHERE blog_posts.postID = :postID");
$stmt->execute(array(':postID' => $_GET['id']));
$row = $stmt->fetch();
//if post does not exists redirect user to homepage.
if($row['postID'] == ''){
header('Location: ./');
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title><?php echo $row['postTitle'];?> | Website</title>
<link rel="stylesheet" href="style/normalize.css">
<link rel="stylesheet" href="style/main.css">
</head>
<body>
<div id="wrapper">
<h1>Single Post Page</h1>
<hr />
<p>Home |
Categories:
<?php while($row = $stmt->fetch())
{
//the first category is being skipped? How to fix?
echo $row['catName'];
} ?>
</p>
<div>
<?php
//these won't appear because of the while loop. It's being skipped.
echo '<h1>'.$row['postTitle'].'</h1>';
echo '<p>'.$row['postCont'].'</p>';
?>
</div>
</div>
</body>
</html>
Replace:
while($row = $stmt->fetch())
{
echo $row['catName'];
}
With:
do {
echo $row['catName'];
} while($row = $stmt->fetch());
As for the other items - put them inside the loop obviously instead of afterwards.
As the title suggests, I am trying to display a list of search results using PDO and MySQL...I have a table of recipes, which have recipe_id, name & description. I would like to have a search box which can find a key word IN the name or description, i.e. "salad" or "carrots", and return a list of all matching recipes by displaying only their names. Before I switched to using PDO, I had the following code which did exactly what I needed:
<?php
include ("dbconnect.php");
if (!isset($_POST['search'])) {
header("Location:index.php");
}
$search_sql="SELECT * FROM Recipe WHERE name LIKE '%".$_POST['search']."%' OR description LIKE '%".$_POST['search']."%'";
$search_query=mysql_query($search_sql);
if (mysql_num_rows($search_query)!=0) {
$search_rs=mysql_fetch_assoc($search_query); }
?>
<!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>
<h3>Search results</h3>
<?php
if (mysql_num_rows($search_query)!=0) {
do { ?>
<p><?php echo $search_rs['name']; ?></p>
<?php }
while ($search_rs=mysql_fetch_assoc($search_query));
}
else {
echo "No results found";
}
?>
</body>
</html>
However, I am having difficulties doing the same with PDO...I have come up with the following code so far, but I suspect I am doing it wrong and furthermore I do not know how to display the actual results...I would be very grateful if anyone can provide some assistance, and please excuse my insufficient knowledge on the matter, I am still a newbie.
<?php
include ("dbconnect.php");
if (!isset($_POST['search'])) {
header("Location:index.php");
}
// keep track post values
$name = "%".$_POST['search']."%";
$description = "%".$_POST['search']."%";
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql ='SELECT * FROM recipe WHERE name LIKE ? OR description LIKE ?';
$q = $pdo->prepare($sql);
$q->execute(array($name,$description));
$data = $q->fetchAll(PDO::FETCH_ASSOC);
Database::disconnect();
?>
<!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 charset="utf-8">
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="js/bootstrap.min.js"></script>
<title>Untitled Document</title>
</head>
<body>
<div class="container">
<div class="row">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Search Results</th>
</tr>
</thead>
<tbody>
<?php
if ($data != null) {
foreach($data as $row)
{
echo '<tr>';
echo '<td>'. $row['name'] . '</td>';
echo '</tr>';
}
}else
{
echo '<td>'. "No results found" .'</td>';
}?>
</tbody>
</table>
</div>
</div>
</body>
</html>
You need to put the % on your parameters:
// keep track post values
$name = "%".$_POST['search']."%";
$description = "%".$_POST['search']."%";
Note, this in general is going to perform horribly as starting your like with a % will kill any index you have on name or description. As you're data grows, you'll start to see the slowdown.
Instead, you can take a look at full text searching options: http://blog.marceloaltmann.com/en-using-the-mysql-fulltext-index-search-pt-utilizando-mysql-fulltext/