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>
Related
I'm trying to make an online shop for my school canteen (this is a school assignment) and I'm really struggling with linking items from the database I've created into my PHP document.
This is the page I'm using for the store called canteen_ordering.php
<!--MADE BY MIKE YAZDAN, NARRABUNDAH COLLEGE YEAR 12 - DYNAMIC WEB-->
<?php
require_once('php/create_db.php');
require_once('./php/component.php');
$database = new CreateDb("productdb", "producttb");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="A Look at the IT Department">
<meta name="keywords" content="Narrabundah, College, IT, Eagle">
<meta name="author" content="Mike Yazdan">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="js/jquery-ui.theme.css">
<link rel="stylesheet" href="js/jquery-ui.theme.min.css">
<script type="text/javascript" src="js/script.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://kit.fontawesome.com/acd14a985b.js" crossorigin="anonymous"></script>
<link rel="stylesheet" type="text/css" href="css/style.css">
<title>Image Showcase</title>
</head>
<body>
<section class="page8"> <!--START PAGE 8-->
<nav class="navBar">
<img src="images/png/badge3.png" alt="Logo" class="ncLogo">
<ul class="navContainer"> <!--NAV SELECTION START-->
<li>
Canteen
<ul class="child active">
<li>HNF</li>
<li>SHDF</li>
</ul>
</li>
<li>
Detication To Health
<ul class="child">
<li>More Info</li>
<li>Canteen Announcments</li>
</ul>
</li>
<li>
Canteen Ordering
</li>
</ul>
</nav> <!--NAV SELECTION END-->
<section class="canteenContainer">
<img src="images/png/narrabundah.png" alt="eagle banner" class="bird">
<section class="food box">
<?php
$result = $database->getData();
while ($row = mysqli_fetch_assoc($result)){
component($row['product_name'], $row['product_price'], $row['product_image'], $row['id']);
}
?>
<br>
</section>
</section>
</section> <!--END PAGE 8-->
</body>
</html>
this is the page that stores PHP values in HTML component.php
<?php
function component($productname, $productprice, $productimage){
$element = "
<div id=\"latte\" class=\"subCont\">
<form action=\"index.php\" method=\"post\">
<h3>$productname</h3>
<img src=\"resources/images/png_jpg/$productimage\" alt=\"Latte\">
<p class=\"text\">
Macaroni and cheese—also called mac 'n' cheese in the United States, and
macaroni cheese in the United Kingdom—is a dish of cooked macaroni pasta
and a cheese sauce, most commonly cheddar. It can also incorporate other
ingredients, such as breadcrumbs or meat.
</p>
<div class=\"canBtnContainer\">
<button type=\"submit\" name=\"add\" class=\"canBtn\">Add to Cart</button>
<p>$$productprice</p>
</div>
<form>
</div>
";
}
This is is the page that creates the database called create_db.php
<?php
class CreateDb
{
public $servername;
public $username;
public $password;
public $dbname;
public $tablename;
public $con;
// class constructor
public function __construct(
$dbname = "newdb",
$tablename = "productdb",
$servername = "localhost",
$username = "root",
$password = ""
)
{
$this->dbname = $dbname;
$this->tablename = $tablename;
$this->servername = $servername;
$this->username = $username;
$this->password = $password;
// create connection
$this->con = mysqli_connect($servername, $username, $password);
// Check connection
if (!$this->con){
die("Connection failed : " . mysqli_connect_error());
}
// query
$sql = "CREATE DATABASE IF NOT EXISTS $dbname";
// execute query
if(mysqli_query($this->con, $sql)){
$this->con = mysqli_connect($servername, $username, $password, $dbname);
// sql to create new table
$sql = " CREATE TABLE IF NOT EXISTS $tablename
(id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
product_name VARCHAR (25) NOT NULL,
product_price FLOAT,
product_image VARCHAR (100)
);";
if (!mysqli_query($this->con, $sql)){
echo "Error creating table : " . mysqli_error($this->con);
}
}else{
return false;
}
}
// get product from the database
public function getData(){
$sql = "SELECT * FROM $this->tablename";
$result = mysqli_query($this->con, $sql);
if(mysqli_num_rows($result) > 0){
return $result;
}
}
}
This is the database
items
This is the database
structure
This is the
database name and table name
see first your table is created and function is able to fetch values,
then
you operating with many " quotations in function it makes confusion use single quote ' while using things inside statement/double quotes ,
use . concatenate two things
<?php
function component($productname, $productprice, $productimage){
$element = "
<div id='latte' class='subCont'>
<form action='index.php' method='post'>
<h3>".$productname."</h3>
<img src='resources/images/png_jpg/".$productimage."' alt='Latte'>
<p class='text'>
Macaroni and cheese—also called mac 'n' cheese in the United States, and
macaroni cheese in the United Kingdom—is a dish of cooked macaroni pasta
and a cheese sauce, most commonly cheddar. It can also incorporate other
ingredients, such as breadcrumbs or meat.
</p>
<div class='canBtnContainer'>
<button type='submit' name='add' class='canBtn'>Add to Cart</button>
<p>".$productprice."</p>
</div>
<form>
</div>
";
}
?>
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
Im trying to echo the information that is inserted to my database. As im pretty new to coding php im not really sure how i could manage to do this. My database name is "nyheter" and the table is "post". So i want to selecte the data from db and echo it on the page.
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "nyheter");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
//get results from database
$result = mysqli_query($connection,"SELECT * FROM post");
$all_property = array(); //declare an array for saving property
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8" />
<link rel="stylesheet" href="nyheter.css" type="text/css" />
<link rel="stylesheet" href="read.css">
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="jquery.dynamicmaxheight.js"></script>
</head>
<body>
<div class="allt-2">
<div class="content">
<img src="http://cdn4.iconfinder.com/data/icons/socialmediaicons_v120/48/google.png"/ alt="" >
<h3><?php echo $Name?></h3>
</div>
<section class="section js-dynamic-height" data-maxheight="150" >
<p class="dynamic-height-wrap"> Hej
</p>
<button class="js-dynamic-show-hide button" title="Läs mer" data-replace-text="Läs mindre">Läs mer</button>
</section>
<img class="ny-img" src="http://placehold.it/500x320"/>
</div>
</body>
<script>
$(document).ready(function(){
$('.js-dynamic-height').dynamicMaxHeight();
});
</script>
</body>
</html>
You're selecting everything from the database. So you've to use a loop to display the content you've selected from the database.
Write something similar to this:
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "name: " . $row["name"]."<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn); ?>
Hope this was helpful.
I'm making a website for school about MLB with a database. But now I want information in the database as an option in a dropdownlist. This is my HTML Code:
<!DOCTYPE html>
<html>
<head>
<link rel="icon" type="image/png" href="./images/favicon-32x32.png"
sizes="32x32" />
<link rel="icon" type="image/png" href="./images/favicon-16x16.png"
sizes="16x16" />
<title>MLB: Major League Baseball</title>
<link href="css folder/MLBstylesheet.css" rel="stylesheet"
type="text/css"/>
</head>
<body>
<div id="container">
<div id="titel">
<img class="MLBTitel" src="./images/MLBtitel.jpg" alt="MLBTitel" >
<div id="titeltekst">
MAJOR LEAGUE BASEBALL
<br>
</div>
<nav>
<ul>
<li><a class= "menu" href="index.html">Home</a></li>
<li><a class= "menu" href="spelers.php">Spelers</a></li>
<li><a id = "active" class= "menu" href="teams.php">Teams</a></li>
<li><a class= "menu" href="wedstrijden.html">Wedstrijden</a></li>
<li><a class= "menu" href="contact.html">Contact</a></li>
</ul>
</nav>
<br><br>
</div>
<div id="teamtabel">
<?php
$servername = "localhost";
$username = "id1419279_root";
$password = "*******";
$dbname = "id1419279_mlb";
// Create connection
$conn = new mysqli($localhost, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM teams";
$result = $conn->query($sql);
echo "<select name='naamteam'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['teamnaam'] ."'>" . $row['teamnaam'] ."
</option>";
}
echo "</select>";
?>
</div>
</div>
</body>
</html>
The options would be the teamnames, but it seems like my dropdownlist is still empty: picture of the dropdownlist on the website
This is what my database looks like:
picture of my database
I hope someone could help me and I would really appreciate it.
<!DOCTYPE html>
<html>
<head>
<link rel="icon" type="image/png" href="./images/favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="./images/favicon-16x16.png" sizes="16x16" />
<title>MLB: Major League Baseball</title>
<link href="css folder/MLBstylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="container">
<div id="titel">
<img class="MLBTitel" src="./images/MLBtitel.jpg" alt="MLBTitel" >
<div id="titeltekst">
MAJOR LEAGUE BASEBALL
<br>
</div>
<nav>
<ul>
<li><a class= "menu" href="index.html">Home</a></li>
<li><a class= "menu" href="spelers.php">Spelers</a></li>
<li><a id = "active" class= "menu" href="teams.php">Teams</a></li>
<li><a class= "menu" href="wedstrijden.html">Wedstrijden</a></li>
<li><a class= "menu" href="contact.html">Contact</a></li>
</ul>
</nav>
<br><br>
</div>
<div id="teamtabel">
<?php
$servername = "localhost";
$username = "id1419279_root";
$password = "*******";
$dbname = "id1419279_mlb";
// Create connection
$conn = new mysqli($localhost, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM teams";
$result = $conn->query($sql);
$dropdownlist = '';
while($row = mysqli_fetch_array($result)) {
$teamnaam = $row['teamnaam'];
$dropdownlist .="<option value='" . $teamnaam . "'>" . $teamnaam . "</option>";
}
if(isset($dropdownlist)){
echo "<select name='naamteam'>";
echo $dropdownlist;
echo "</select>";
}
?>
</div>
</div>
</body>
</html>
I have a data-table in mySQL and I need help accessing the information to display on an HTML Page.
Here are some details.
Host: 127.0.0.1:8889
username: root
password: root
database-name: gibsonek
table-name: events
Here is my code:
<html>
<head>
<meta charset="UTF-8">
<title>Gibson Ek Schedule</title>
<!--JQuery Add-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<!--My JS Add-->
<script src="script.js"></script>
<!--Normalize CSS Add-->
<link rel="stylesheet" href="css/normalize.css">
<!--Google Font - Open Sans - Add -->
<link rel='stylesheet prefetch' href='https://fonts.googleapis.com/css?family=Open+Sans:300,400,600'>
<!--Bootstrap Add-->
<link rel='stylesheet prefetch' href='https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css'>
<!--My CSS Add-->
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="container">
<div class="navbar">
<span>Gibson Ek Schedule</span>
</div>
<div class="header">
<div class="color-overlay">
<div class="day-number"></div>
<div class="date-right">
<div class="day-name"></div>
<div class="month"></div>
</div>
</div>
</div>
<div class="timeline">
<ul id = "l">
<?php
$connection = mysql_connect('127.0.0.1:8889', 'root', 'root');
mysql_select_db('gibsonek');
$query = "SELECT * FROM `events` WHERE 1";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
echo "<p>SQL DATA WILL GO IN HERE</p>";
}
mysql_close(); //Make sure to close out the database connection
?>
</ul>
</div>
</div>
</body>
</html>
Please use PDO (it's more flexible and safe)
$servername = "127.0.0.1:8889";
$username = "root";
$password = "root";
$dbname = "gibsonek";
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT * FROM `events` WHERE id='1'");
$stmt->execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
$result = $stmt->fetchAll();
var_dump($result); // or as you like
There is a good article.
There is a best guide.
<div class="timeline">
<ul id = "l">
<?php
$host = "127.0.0.1:8889";
$username = "root";
$password = "root";
$db_name = "gibsonek";
$mysqli = new mysqli($host, $username, $password, $db_name);
if ($mysqli->connect_error) {
die('Error : ('. $mysqli->connect_errno .') '.$mysqli->connect_error);
}
$query = "SELECT * FROM `events` WHERE id='1'"; // Here if 1 means id
$result = $mysqli->query($query);
while($row = $result->fetch_assoc()) {
?>
<!-- Here You can add any HTML or css -->
<li><?php echo $row["column"]; ?> </li> // write your column name what you want to show
<?php
}
?>
</ul>
</div>
I think that would be clear for you, leave a comment if any query.
Thanks
I think you missed something
here is correct code:
<?php
$connection = mysql_connect('127.0.0.1:8889', 'root', 'root');
mysql_select_db('gibsonek');
$query = "SELECT * FROM `events` WHERE id(or any column as u want)='1'";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
print_r($row);
}
mysql_close(); //Make sure to close out the database connection
?>