<?php
try{
include("dbconnectie.php");
$query = $db->prepare("SELECT * FROM shop WHERE id_img = '3'");
$query->execute();
$result = $query->fetchALL(PDO::FETCH_ASSOC);
$image = $result['img_url'];
echo "<table>";
foreach($result as &$data) {
echo "<tr>";
echo "<td>" . $data["brand"] . "</td>";
echo "<td>" . $data["model"] . "</td>";
echo "<td>" . $data["cond"] . "</td>";
echo "<td>" . $data["price"] . "</td>";
echo '<img src="data:image/png;base64,'.base64_encode( $data["image"] ).'"/>';
echo "</tr>";
}
echo "</table>";
} catch(PDOException $e) {
die("Error!: " . $e->getMessage());
}
?>
<html>
<body>
<a src='<?php echo $image; ?>' border='0'></a>
</body>
</html>
in line 7 u can see that i'm trying to define $image as the url that's saved in the database so i could use it all the way at the bottom to project it as an image.
Related
I need to retrieve the color hex code stored in a database and display it as seen on screen within a table cell. i'm trying to find a way to convert the hex code the db query returns for that specific table cell into the actual color that the hex denotes. Here's my code...
<?php
// Include config file
require_once "config.php";
$pdo = new PDO($dsn, $username, $password, $options);
// Attempt select query execution
$sql = "SELECT * FROM sales";
if($result = $pdo->query($sql)){
if($result->rowCount() > 0){
echo "<table class='table table-bordered table-striped'>";
echo "<thead>";
echo "<tr>";
echo "<th>S/N</th>";
echo "<th>Transaction Date</th>";
echo "<th>Customer Name</th>";
echo "<th>Address</th>";
echo "<th>Phone Number</th>";
echo "<th>Vehicle Model</th>";
echo "<th>Vehicle Chassis Number</th>";
echo "<th>Vehicle Registration Number</th>";
echo "<th>Vehicle Color</th>";
echo "<th>Amount Paid</th>";
echo "<th>Advance</th>";
echo "<th>Balance</th>";
echo "<th>Balance Due Date</th>";
echo "<th>Paid To</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($row = $result->fetch()){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['transactiondate'] . "</td>";
echo "<td>" . $row['customername'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['phonenumber'] . "</td>";
echo "<td>" . $row['vehiclemodel'] . "</td>";
echo "<td>" . $row['vehiclechassisnumber'] . "</td>";
echo "<td>" . $row['vehicleregistrationnumber'] . "</td>";
echo "<td>" . $row['vehiclecolor'] . "</td>";
echo "<td>" . $row['amountpaid'] . "</td>";
echo "<td>" . $row['advance'] . "</td>";
echo "<td>" . $row['balance'] . "</td>";
echo "<td>" . $row['balancedate'] . "</td>";
echo "<td>" . $row['paidto'] . "</td>";
echo "<td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
// Free result set
unset($result);
} else{
echo "<p class='lead'><em>No records were found.</em></p>";
}
} else{
echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
}
// Close connection
unset($pdo);
?>
It is a bit unclear about where you wish to use the colour but I'm assuming it is here. And also my assumption is the retrieved value for vehiclecolor column is a HEX value
echo "<td>" . $row['vehiclecolor'] . "</td>";
You can use tag property 'background-color' for this.
$temp = $row['vehiclecolor'];
echo "<td style='background-color:" . $temp ."'></td>";
Pretty self-explanatory I guess. String concatenation.
I am new to using database with php, using 000webhost I don't know what to write to get data from specific table
connection.php
<?php
$servername = "localhost";
$username = "*****************";
$password = "***********";
$database = "*****************";
try {
$conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
getalldata.php
<?php
include 'connection.php';
//get data from Database
?>
Let suppose you are fetching records from studentrecord
require_once 'connection.php';
$roll = $_POST['roll'];
$query = "SELECT * from studentrecord where roll = '$roll'";
try{
$statement = $conn->query($query);
echo "<table border='1'>
<tr>
<td>Roll</td>
<td>Stream</td>
<td>Session</td>
<td>Name</td>
<td>Mother Name</td>
<td>Father Name</td>
<td>Total Mark's</td>
<td>Grade</td>
<td>Result</td>
</tr>";
//if you want to fetch records as an array indexed
while ($result = $statement->fetch()){
echo "<tr>";
echo "<td>" . $result['id'] . "</td>";
echo "<td>" . $result['stream'] . "</td>";
echo "<td>" . $result['session'] . "</td>";
echo "<td>" . $result['name'] . "</td>";
echo "<td>" . $result['mother_name'] . "</td>";
echo "<td>" . $result['father_name'] . "</td>";
echo "<td>" . $result['total_marks'] . "</td>";
echo "<td>" . $result['grade'] . "</td>";
echo "<td>" . $result['result'] . "</td>";
echo "</tr>";
}
//if you want to fetch records as an anonymous object
while ($result = $statement->fetchObject()){
echo "<tr>";
echo "<td>" . $result->id . "</td>";
echo "<td>" . $result->stream . "</td>";
echo "<td>" . $result->session . "</td>";
echo "<td>" . $result->name . "</td>";
echo "<td>" . $result->mother_name . "</td>";
echo "<td>" . $result->father_name . "</td>";
echo "<td>" . $result->total_marks . "</td>";
echo "<td>" . $result->grade . "</td>";
echo "<td>" . $result->result . "</td>";
echo "</tr>";
}
echo "</table>";
}
catch (PDOException $ex) {
echo "Failed to retrieve record".$ex->getMessage();
`enter code here`}
<?php
try{
include("dbconnectie.php");
$query = $db->prepare("SELECT * FROM shop WHERE id_u = :id");
$query->bindParam("id", $_SESSION['id_u']);
$query->execute();
$result = $query->fetchALL(PDO::FETCH_ASSOC);
echo "<table>";
foreach($result as &$data) {
echo "<tr>";
echo "<td>" . $data["brand"] . "</td>";
echo "<td>" . $data["model"] . "</td>";
echo "<td>" . $data["cond"] . "</td>";
echo "<td>" . $data["price"] . "</td>";
echo "</tr>";
}
echo "</table>";
} catch(PDOException $e) {
die("Error!: " . $e->getMessage());
}
?>
<html>
<body>
<div class="poster">
<img src="<?php echo $data['img_url']; ?>" width='400' height='300' ></img>
</div>
</body>
</html>
So in a different file, $_SESSION['id_u'] was defined as id_u which is in the 'account' table in my database. Now in the 'shop' table I have every sell placement written down with the corresponding user id: "id_u"
Now what I'm trying to do it Select all the sell placements that are put under that user id, but it's not working. Now for some reason it just shows a big border with nothing but a broken image icon. Not even the corresponding text.
Do a session_start(); at the top.
Complete newbie to PHP/MySQL and HTML so please bear with me if I dont explain myself properly. At present I have an order form which stores the order in my database and shows it on a table in my admin area. I would like to be able to delete the row from the table and my database when I have completed the order.
At present my code looks like this:
<?php
//87229feely.php
include('connection.php');
$result = mysql_query("SELECT * FROM orderform");
echo "<table border='1' >
<tr>
<th><u>ID</th>
<th><u>Date</th>
<th><u>Product</th>
<th><u>Product Comments</th>
<th><u>Name</th>
<th><u>Address</th>
<th><u>Age</th>
<th><u>Delivery</th>
<th><u>Delete</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id']. "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['product'] . "</td>";
echo "<td>" . $row['productcomments'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['delivery'] . "</td>";
echo "<td>delete</td>";
echo "</tr>";
}
echo "</table>";
?>
<?php
//deleterow.php
include('connection.php');
$id = $_GET['id']; //this needs to be sanitized
if(!empty($id)){
$result = mysql_query("DELETE FROM orderform WHERE id=".$id.";");
}
header("Location: 87229feely.php");
?>
Any help? All greatly appreciated. Thanks
This answer does not meet security standards but should do the job:
<?php
//index.php
include('connection.php');
$result = mysql_query("SELECT * FROM orderform");
echo "<table border='1' >
<tr>
<th><u>ID</th>
<th><u>Date</th>
<th><u>Product</th>
<th><u>Product Comments</th>
<th><u>Name</th>
<th><u>Address</th>
<th><u>Age</th>
<th><u>Delivery</th>
<th><u>Delete</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id']. "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['product'] . "</td>";
echo "<td>" . $row['productcomments'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['delivery'] . "</td>";
echo "<td>delete</td>";
echo "</tr>";
}
echo "</table>";
?>
<?php
//delete.php
include('connection.php');
$id = $_GET['id']; //this needs to be sanitized
if(!empty($id)){
$result = mysql_query("DELETE FROM orderform WHERE id=".$id.";");
}
header("Location: index.php");
?>
Many ways to do this. Since you're a beginner, this would probably be the most straight-forward (albeit not how I would do it)
Create a form around the table
Add a button on the delete column for each row and assign an id to it (the ID should be the ID from your database) <input type="submit" name="submit" value"/*YOUR ID*/">
Add some processing script before the table is being parsed
if (isset($_POST['submit'])) {
$sql = "DELETE FROM table WHERE id='/*YOUR ID*/'";
mysql_query($sql);
}
First of all ,you need to send ID of completed order to next form. You cam do this by adding:
echo("<input type='hidden' name='orderID' value='".$row['id']."'/>");
That will create a hidden field with value of ID.
If your form uses POST:
then:
if(isset($_POST['submit'])){
$orderID = $_POST['orderID'];
mysql_query("DELETE FROM table WHERE id=$oderID");
}
If you are using GET method:
<form method="GET">
You could either use hidden field as mentioned above or you could parse ID of order in GET url:
<form action='action.php?id=".$row['id']."'>
and after submitting:
if(isset($_GET['submit']) && isset($_GET['id')){
$orderID = $_GET['id'];
mysql_query("DELETE FROM table WHERE id=$orderID");
}
Maybe something like this with PDO
<?php
include('connection.php');
?>
<form name="" action="delete.php" method="post">
<table border='1' >
<tr>
<th> </th>
<th><u>ID</th>
<th><u>Date</th>
<th><u>Product</th>
<th><u>Product Comments</th>
<th><u>Name</th>
<th><u>Address</th>
<th><u>Age</th>
<th><u>Delivery</th>
<th><u>Delete</th>
</tr>
<?php
try {
$conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$row = $conn->query('SELECT * FROM orderform');
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>";
echo "<td><input type=\"checkbox\" name=\"id[]\" id=\"checkAll\" value=\"".$row['id']."\" /></td>"
echo "<td>" . $row['id']. "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['product'] . "</td>";
echo "<td>" . $row['productcomments'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['delivery'] . "</td>";
echo "</tr>";
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
?>
</table>
<input type="submit" name="submit" value="submit" />
</form>
delete.php
<?php
$id
if(isset($_POST['submit']) {
include('connection.php');
try {
$conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql="DELETE FROM orderform WHERE id IN (".implode(',',$conn->quote($id)).")";
$stmt->exec($sql);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
}
i have two table:
category
id | name
products
id | cat_id | name..
Like the title, I want to create a menu of category, when I click on one category, it will display all products in that category.I have try several way but it won't works! This is my menu.php
try {
$dbh = new PDO("mysql:host=$hostname;dbname=...", $username, $password);
$sql = "select * from category";
echo "<table>";
echo "<tr><th>ID</th><th>Name</th></tr>";
foreach ($dbh->query($sql) as $row)
{
$c_id=$row['c_id'];
echo "<tr>";
echo "<td>" . $row['c_id'] ."</td>";
echo "<td><a href='deal_list.php?cat_id=$c_id'>" . $row['c_name'] . "</td>";
echo "</tr>";
}
echo "</table>";
$dbh = null;
}
catch(PDOException $e) { echo $e->getMessage(); }
?>
and this is my product list
try {
$cat_id = $_GET['cat_id'];
$dbh = new PDO("mysql:host=$hostname;dbname=..", $username, $password);
$sql = "select id,name,description,price,groupbuy_price, CEIL((groupbuy_price/price)*100) AS saving,current_buyer,maximum_buyer,expired_time,status,sum_img,cat_id,c_name from groupbuy,category where c_id=cat_id";
echo "<table border=2px>";
echo "<tr><th>Name</th><th>Description</th><th>Price</th><th>Group buy price</th><th>Saving</th><th>Number of current buyer</th><th>Maximum Buyer</th><th>Expired Time</th><th>Status</th><th>Category</th><th>Sumary image</th></tr>";
foreach ($dbh->query($sql) as $row)
{
$id=$row['id'];
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['description'] ."</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td>" . $row['groupbuy_price'] ."</td>";
echo "<td>" . $row['saving'] .'%' ."</td>";
echo "<td>" . $row['current_buyer'] ."</td>";
echo "<td>" . $row['maximum_buyer'] ."</td>";
echo "<td>" . $row['expired_time'] ."</td>";
echo "<td>" . $row['status'] ."</td>";
echo "<td>" . $row['c_name'] . "</td>";
?>
<td><img src="<?php echo $row['sum_img']?>" width="50px" /></td>
<?php
echo "<td><a href='delete_deal.php?id=$id'>Delete</a></td>";
echo "<td><a href='deal_detail.php?id=$id'>Detail</a></td>";
echo "<td><a href='edit_deal.php?id=$id'>Edit</a></td>";
echo "</tr>";
}
echo "</table>";
$dbh = null;
}
catch(PDOException $e) { echo $e->getMessage(); }
?>
looks like you just need to replace
$sql = "select id,name,description,price,groupbuy_price, CEIL((groupbuy_price/price)*100) AS saving,current_buyer,maximum_buyer,expired_time,status,sum_img,cat_id,c_name from groupbuy,category where c_id=cat_id";
with
$sql = "select id,name,description,price,groupbuy_price, CEIL((groupbuy_price/price)*100) AS saving,current_buyer,maximum_buyer,expired_time,status,sum_img,cat_id,c_name from groupbuy,category where c_id={$cat_id}";
but for secure reasons change also in begining
$cat_id = (int) $_GET['cat_id'];