why PHP search page show blank box with no result? - php

I'm trying to do a search bar here.
I have done my database, filled it with data, connected it to my php file, did the search bar code everything is fine but when i tried to search for a word its showing me blank box with zero result
This is my db connect code
<?php
$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "root";
$dbName = "product_list";
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
and this is search page code
<?php
include '/Applications/MAMP/htdocs/Pharmacy Project/db.php';
?>
<?php include_once '/Applications/MAMP/htdocs/Pharmacy Project/Styles/TopNav/topnav.html'; ?>
<style>
<?php include '/Applications/MAMP/htdocs/Pharmacy Project/Styles/TopNav/topnav.css';
?>
</style>
<?php ?>
<style>
<?php include '/Applications/MAMP/htdocs/Pharmacy Project/Styles/SearchBar/searchbar.css'; ?>
</style>
<style>
<?php include_once '/Applications/MAMP/htdocs/Pharmacy Project/Styles/Result/result.css' ?>
</style>
<div class="result-container">
<?php
if (isset($_POST['Search'])) {
$search = mysqli_real_escape_string($conn, $_POST['search']);
$sql = "SELECT * FROM product_list WHERE a_Name LIKE '%$search%' OR Pharmacy LIKE '%$search%'";
$result = mysqli_query($conn, $sql);
$queryresult = mysqli_num_rows($result);
echo 'There are' . $queryresult . 'results!';
if ($queryresult > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<div class='result-box'>
<h3>" . $row['a_Name'] . "</h3>
<p>" . $row['Price'] . "</p>
<p>" . $row['Pharmacy'] . "</p>
</div>";
}
} else {
echo "Sorry No Result :(!";
}
}
?>
</div>
this is my form page
<?php
include '/Applications/MAMP/htdocs/Pharmacy Project/db.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="/Applications/MAMP/htdocs/Pharmacy Project/Styles/Result/result.css">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home Screen</title>
</head>
<body>
<?php include_once '/Applications/MAMP/htdocs/Pharmacy Project/Styles/TopNav/topnav.html'; ?>
<style>
<?php include '/Applications/MAMP/htdocs/Pharmacy Project/Styles/TopNav/topnav.css';
?>
</style>
<?php ?>
<style>
<?php include '/Applications/MAMP/htdocs/Pharmacy Project/Styles/SearchBar/searchbar.css'; ?>
</style>
<style>
<?php include_once '/Applications/MAMP/htdocs/Pharmacy Project/Styles/Result/result.css' ?>
</style>
<form action="search.php" method="POST">
<div id="searchbar">
<input type="text" name="search" placeholder="Search">
<button id='sbutton'>Search</button>
</div>
</form>
<h2 class="allresult">All Result</h2>
<div class="result-container">
<?php
$sql = "SELECT * FROM Product";
$result = mysqli_query($conn, $sql);
$queryresults = mysqli_num_rows($result);
if ($queryresults > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<div class='result-box'>
<h3>" . $row['a_Name'] . "</h3>
<p>" . $row['Price'] . "</p>
<p>" . $row['Pharmacy'] . "</p>
</div>";
}
}
?>
</div>
</body>
</html>
I'm new to PHP

Your search field name is search in lowercase but you are checking for Search in $_POST which is capitalized. Make sure they match and it will fix the issue

Related

Filling boostrap cards with data from a mySQL database

I am trying to fill bootstrap cards with data from a database.
I am very close.
I have the mainphp file called project.php
A file with a function called component to fill them, called component.php.
And a connectDB.php file, that connect the DB and extracts the information from a table.
I am aware this is alot of code so I highlighted 2 areas the problem is definitely in...Apologies if too much, I can remove the function if necessary as it works..
here is an extract from project.php
<?php
require_once('connect/connectDB.php');
require_once('component.php');
?>
<!doctype html>
<html lang="en">
<head>
<title>The Wild Music Shop</title>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!--Font awesome-->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" integrity="sha512-iBBXm8fW90+nuLcSKlbmrPcLa0OT92xO1BIsZ+ywDWZCvqsWgccV3gFoRBv0z+8dLJgyAHIhR35VZc2oM/gI1w==" crossorigin="anonymous" />
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
<!-- i have no idea why it won't link -->
<!-- <link type="text/css" rel="stylesheet" href="myCSS/somecss.css"> -->
<link rel="stylesheet" href="myCSS/somecss.css">
</head>
<body>
<!-- the cards -->
<div class = "container" id="thecards">
<div class = "row text-center py-5">
//Definite problem here
<?php
$result = getData();
while ($row = mysqli_fetch_assoc($result)){
component($row['InstName'], $row['Price'], $row['ProductImg'], $row['description']);
}
?>
</div>
</div>
</html>
This component definitely works as I tested it
<?php
function component($pInstName, $pPrice, $pProductImg, $pdescription){
$element = "
<div class=\"col-md-3 col-sm-6 my-3 my-md-0\">
<form action=\"index.php\" method=\"post\">
<div class=\"card shadow\">
<div>
<img src=\ $pProductImg\" alt=\"Image1\" class=\"img-fluid card-img-top\">
</div>
<div class=\"card-body\">
<h5 class=\"card-title\">Our $pInstName</h5>
<h6>
<i class=\"fas fa-star\"></i>
<i class=\"fas fa-star\"></i>
<i class=\"fas fa-star\"></i>
<i class=\"fas fa-star\"></i>
<i class=\"far fa-star\"></i>
</h6>
<p class=\"card-text\">
$pdescription
</p>
<h5>
<span class=\"price\">$$pPrice</span>
</h5>
<button type=\"submit\" class=\"btn btn-warning my-3\" name=\"add\">Add to Cart </button>
</div>
</div>
</form>
</div>
";
echo $element;
}
?>
and finally here is the connectDB.php
<?php
//Create a database connection
$dbhost = "localhost";
$dbuser = "root";
$dbpassword = "";
$dbname = "g00398295";
$connection = mysqli_connect($dbhost,$dbuser,$dbpassword,$dbname);
//Test if connection occoured
if(mysqli_connect_errno()){
die("DB connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
if (!$connection)
{
die('Could not connect: ' . mysqli_error());
}
$sql = "select * from Instruments where itemno = 1; ";
$result = mysqli_query($connection,$sql);
$row=mysqli_fetch_assoc($result);
echo "Pls work".$row['InstName'];
mysqli_close($connection);
//get product from Database
//Definite problem here
function getData(){
$sql = "select * from $this->tablename";
$result = mysqli_query($this->con,$sql);
if(mysqli_num_rows($result) > 0){
return $result;
}
}
?>
I am awar the problem is somewhere in the connection step. I am very new to php in general and have been at this very long, I would appreciate help!
You have a function getData() that has "$this->table". Dont copy paste the codes without understanding what it does.
Clean up your connectDB.php:
<?php
//Create a database connection
$dbhost = "localhost";
$dbuser = "root";
$dbpassword = "";
$dbname = "g00398295";
$connection = mysqli_connect($dbhost, $dbuser, $dbpassword, $dbname);
//Test if connection occoured
if(mysqli_connect_errno()){
die("DB connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
?>
In project.php:
<div class = "row text-center py-5">
//Definite problem here
<?php
$sql = "select * from Instruments where itemno = 1; ";
$result = mysqli_query($connection,$sql);
while ($row = mysqli_fetch_assoc($result)){
component($row['InstName'], $row['Price'], $row['ProductImg'], $row['description']);
}
?>
</div>

Can't call dynamic page with $_GET

I'm trying to use $_GET to call a dynamic page, but it throws "Error". Do I need to call page 1 from page 2 or why can't they talk to each other?
Thanks in advance for your help, supposingly there's an easy solution.
/Jimmy
This is page 1:
<!DOCTYPE html>
<html>
<head>
<title>Players</title>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
<?php include ("header.php"); ?>
<?php
$servername = "localhost";
$username = "jim";
$password = "pass";
$dbname = "jim";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// SQL search
$sql = "SELECT PlayerID, Person FROM People where PlayerID is not null ORDER BY Person";
$result = $conn->query($sql);
// Condition
if ($result->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Player</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>"."<a href='spelarfakta.php?=id{$row['PlayerID']}'>{$row['PlayerID']}</a>"."</td>"."<td>".$row["Person"]."</td>";
}
echo "</table>";
} else {
echo "No hits";
}
$conn->close();
?>
</body>
</html>
This is page 2:
<!DOCTYPE html>
<html>
<head>
<title>Player Stats</title>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
<?php include ("header.php");
if (isset($_GET['id'])) {
echo '<p>'. $_GET['id'].'</p>';}
else {echo 'Error';
}
?>
</body>
</html>
Correct solution as a comment:
=id should be id=

PHP Custom blog system, Duplicates message for every user

so I'm making a blog system on https://aindrigo.com/blog.php?bid=1 and for some reason the post duplicates for every user that exists.. any help?
<?php
$con = mysqli_connect("localhost","root","don't try hacking my db","data");
$bid = $_GET["bid"];
$queryb = $con->query("SELECT bid,title,content FROM blogposts WHERE bid=$bid");
$queryu = $con->query("SELECT id,username,password,avatarurl FROM accounts");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="description" content="aindev's Website">
<link rel="stylesheet" href="style.css?v=2222">
<link href="https://fonts.googleapis.com/css?family=Inter&display=swap" rel="stylesheet">
<title>Adam Indrigo</title>
</head>
<body>
<div class="start">
<h1>aindev</h1>
</div>
<div class="info">
<div class="inner">
<h1>aindev's Blog</h1>
<?php
if(!$bid){
echo "<h1>Please enter a Blog ID (bid) in the url</h1>";
}
while($row = mysqli_fetch_assoc($queryb)){
while($row2 = mysqli_fetch_assoc($queryu)){
echo "<img src='" . $row2["avatarurl"] . "?v=22222' style='max-width: 60px; max-height: 60px; border-radius: 6px;'>";
echo "<h2>" . $row["title"] . " by " . $row2["username"] . "</h2>";
echo "<p>" . $row["content"] . "</p>";
}
}
?>
</div>
</div>
</body>
</html>
I don't know the problem. I tried adding it so that it only shows it for the 1 user that created it. Even adding a cid tag, but that didn't work..
So apparently I made a mistake when I added the CID tag. I added the cid to the user query and not the blog query.

How to take out the anchor element from echo (functions.php) and place it to body (index.php)?

How to take out the anchor element from echo (functions.php) and place it to body (index.php)?
PS: Newbie here guys, just trying to understand about HTML, CSS, Javascript and PHP about 2 weeks ago.. just imagine what i learned so far, please don't make it difficult to understand.. just write it :-)
Thanks.
<!--index.php-->
<!doctype html>
<?php include ("functions/functions.php"); ?>
<html>
<head>
<link rel="stylesheet" href="styles/style.css" type="text/css" />
</head>
<body>
<ul id="cats">
<?php getCats(); ?>
</ul>
</body>
</html>
//functions.php
<?php
$con = mysqli_connect("localhost","root","","justLearning");
if (mysqli_connect_errno())
{
echo "The connection was not established: " . mysqli_connect_error();
}
function getCats(){
global $con;
$get_cats = "select * from categories";
$run_cats = mysqli_query($con, $get_cats);
while ($row_cats=mysqli_fetch_array($run_cats)){
$cat_id = $row_cats['cat_id'];
$cat_title = $row_cats['cat_title'];
echo "<a href='index.php?cat=$cat_id'>$cat_title</a>";
}
}
?>
You forgot <li></li> in your code
You need to learn to return an array and to use foreach():
<!doctype html>
<?php include_once("functions/functions.php"); ?>
<html>
<head>
<link rel="stylesheet" href="styles/style.css" type="text/css" />
</head>
<body>
<ul id="cats">
<?
$cats = getCats();
foreach($cats as $cat){
echo "<li><a href='index.php?cat=$cat[cat_id]'>$cat[cat_title]</a></li>";
}
?>
<ul>
</body>
</html>
function getCats(){
global $con;
$get_cats = "select * from categories";
$run_cats = mysqli_query($con, $get_cats);
while ($row_cats=mysqli_fetch_array($run_cats)){
$cats[]=$row_cats;
}
return $cats;
}
I guess you can try something like this, if you really want to move the < a > elements to the body.However I think that the way you did it was better (there is no reason to move them to the body):
<!--index.php-->
<!doctype html>
<?php include_once("functions/functions.php"); ?>
<html>
<head>
<link rel="stylesheet" href="styles/style.css" type="text/css" />
</head>
<body>
<ul id="cats">
<?php
$get_cats = "select * from categories";
$run_cats = mysqli_query($con, $get_cats);
while ($row_cats=mysqli_fetch_array($run_cats))
{
$cat_id = $row_cats['cat_id'];
$cat_title = $row_cats['cat_title'];
?>
<a href='index.php?cat=<?php echo $cat_id;?>'><?php echo $cat_title;?></a>
<?php
}
?>
</ul>
</body>
And in functions.php you just need to leave the mysql connection object:
$con = mysqli_connect("localhost","root","","justLearning");
if (mysqli_connect_errno())
{
echo "The connection was not established: " . mysqli_connect_error();
}
I'm guessing here but I reckon the problem is that the includes_path is set to the default and the included file isn't being found???
<?php
set_include_path( $_SERVER['DOCUMENT_ROOT'] . '/functions/' );
include_once( "functions.php" );
?>
<!--index.php-->
<!doctype html>
<html>
<head>
<title>There must be a title to be considered VALID html</title>
<link rel="stylesheet" href="styles/style.css" type="text/css" />
</head>
<body>
<ul id="cats">
<?php
getCats();
?>
</ul>
</body>
</html>

trouble getting content from my database to a form in php

I have been follwing a series of tutorials over at [thedigitalcraft.com][1] building my first ever dynamic website. I recently followed his 15th, and thought I followed along greatly, but for some reason the content that should be showing up isn't when I run the page. No errors, its almost like one of my sql statements is wrong, or it can't connect to my phpMyAdmin database.I'm building a user interface for editing each of the pages, a control panel. I'm running on XAMPP localhost btw, working in dreamweaver. Why is my content not showing up in the form? I know that I am connected to the database.. I've pasted my code from my pages.php and index.php
1. pages.php:
<?php ## Page Manager ?>
<h2>Page Manager</h2>
<div class="col sidebar">
<ul>
<?php
$q = "SELECT * FROM pages ORDER BY name ASC";
$r = mysqli_query($dbc, $q);
if ($r)
{
while($link = mysqli_fetch_assoc($r))
{
echo '<li>'.$link['name'].'</li>';
}
}
?>
</ul>
</div>
<div class="col editor">
<?php if (isset($_GET['id'])) {
$q = "SELECT * FROM pages WHERE id = '$_GET(id)' LIMIT 1";
// the database connection, our query
$r = mysqli_query($dbc, $q);
$opened = mysqli_fetch_assoc($r);
?>
<form action="#" method="post">
<p><label>Page title: </label><input type="text" size="30" name="title" value="<?php echo $opened['title']?>"></p>
<p><label>Page name:</label> <input type="text" size="30" name="name" value="<?php echo $opened['name']?>"></p>
<label>Page body:</label><br>
<textarea name="body" cols="30" rows="8"><?php echo $opened['body'] ?></textarea>
</form>
<?php } ?>
</div>
index.php:
<?php
error_reporting(0);
// Setup document:
include('config/setup.php');
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title><?php //echo $page_title; ?>JakeForDesign - Admin Panel</title>
<link rel="stylesheet" type="text/css" href="css/styles.css">
</head>
<body>
<div class="wrap_overall">
<div class="header"> <?php head(); ?> </div>
<div class="nav_main"> <?php nav_main(); ?> </div>
<div class="content"> <?php include('content/'.$pg.'.php'); ?> </div>
<div class="footer"> <?php footer(); ?> </div>
</div>
</body>
</html>
2. index.php:
<?php
error_reporting(0);
// Setup document:
include('config/setup.php');
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title><?php //echo $page_title; ?>JakeForDesign - Admin Panel</title>
<link rel="stylesheet" type="text/css" href="css/styles.css">
</head>
<body>
<div class="wrap_overall">
<div class="header"> <?php head(); ?> </div>
<div class="nav_main"> <?php nav_main(); ?> </div>
<div class="content"> <?php include('content/'.$pg.'.php'); ?> </div>
<div class="footer"> <?php footer(); ?> </div>
</div>
</body>
</html>
3. setup.php(for connection to database)
<?php
## Setup Document
// host(or location of the database), username, //password, database name
$dbc = #mysqli_connect('localhost', 'root', 'password', 'database') OR die ('Could not connect to the database because: '. mysqli_connect_error() );
include('Functions/sandbox.php');
include('Functions/template.php');
if ($_GET['page'] == '')
{
$pg = 'home';
}
else
{
$pg = $_GET['page'];
}
$page_title = get_page_title($dbc, $pg);
?>
$q = "SELECT * FROM pages WHERE id = '$_GET(id)' LIMIT 1";
This is wrong
$q = "SELECT * FROM pages WHERE id = '" . $_GET['id'] . "' LIMIT 1";
This is good
Don't forget to secure it with intval() if it's a numeric value!

Categories