apparently i have issue with displaying the image as it only show me the name of the file, is there anyway for it to display on the webpage right away?
Below is my source code along with 2screenshots of my database holding the image and the place where i want it to display at:
<?php
error_reporting(E_ERROR);
include("global.php");
session_start();
$receipt = $_GET['receipt'];
$userid = $_SESSION ['userid'];
if (isset($_SESSION['userid']) == false)
{
header ("Location: login.php");
}
$mysqli = new mysqli(spf, dbuser, dbpw, db);
$stmt = $mysqli->prepare("SELECT receipt, date, time, pick, dropoff, userid, carno, cost, branch, area, image FROM items where receipt=?");
$stmt->bind_param("s", $receipt);
$stmt->execute();
$stmt->bind_result($receipt, $date, $time, $pick, $dropoff, $userid, $carno, $cost, $branch, $area, $image);
while ($stmt->fetch()) {
echo "<table border='1' style='width:40%'>";
echo "<td>";
echo "<b>Receipt ID: $receipt</b>";
echo "<br><br>";
echo "$image";
echo "<br><br>";
echo "<b>Date of Travel: $date</b>";
echo "<br><br>";
echo "<b>Time of Travel: $time</b>";
echo "<br><br>";
echo "<b>Pick Up Location: $pick</b>";
echo "<br><br>";
echo "<b>Drop Off Location: $dropoff</b>";
echo "<br><br>";
echo "<b>Area of DropOff: $area</b>";
echo "<br><br>";
echo "<b>Cost of Trip: $cost</b>";
echo "<br><br>";
echo "<b>User ID (NRIC): $userid</b>";
echo "<br><br>";
echo "<b>Branch of Officer: $branch</b>";
echo "</td>";
}
echo "</table>";
$stmt->close();
$mysqli->close();
?>
Preview:
Use <img> tag as below
echo "<img src=$image>"
Try this:
echo "<img src='$image'>";
Learn about img
Related
New to PDO and I have a PDO connection script which I am requiring at the top but this is what I have to try and just display the database that is made of up 4 employees and their birthdays. Whenever I run it, I get nothing. Am I at least in the right ballpark here?
<?php
require "pdoconnect.php";
try{
$stmt = $conn->prepare("SELECT bid, bname, bday FROM birthday");
$stmt->execute();
# setting the fetch mode
$result = $stmt->fetchAll();
if( ! $result){
print('No Records Found');
}
else{
echo "<table border='1' align='center' cellpadding='5px' cellspacing='2px'>";
//while($row = $stmt->fetch())
echo "<tr><th>Bid</th><th>Name</th><th>Birthdaay</th><th colspan='2'></th></tr>";
foreach($result as $row){
echo "<tr>";
echo "<td>";
echo $row['bid'];
echo "</td>";
echo "<td>";
echo $row['bname'];
echo "</td>";
echo "<td>";
echo $row['bday'];
echo "</td>";
}
echo "</table>";
}
}
catch(PDOException $e){
echo "Error: " . $e->getMessage();
}
$conn = null;
echo "</table>";
?>
I am trying to use the results of a database and display them in an HTML table. I've searched around but can't find a specific answer for my situation as I am new to PHP and I had to copy this code from another source because I was unable to get it to work on my own. Please help.
<?php $server = "127.0.0.1";
$user = "admin";
$pass = "password";
$dbname = "hysteryalelogs";
// Create connection in mysqli
$connection = new mysqli($server, $user, $pass, $dbname);
//Check connection in mysqli
if($connection->connect_error){
die("Error on connection:" .$connection->connect_error);
}
//Display the informaion
$sql = "SELECT * FROM logs";
$res = $connection->query($sql);
if($res->num_rows > 0){
while($row = $res->fetch_assoc()){
echo "Sales Number: ". $row["Sales_Number"]. "<br/>";
echo "Quantity: ". $row["Quantity"]. "<br/>";
echo "Due Date: ". $row["Due_Date"]. "<br/>";
echo "Burnished Housing: ". $row["Burnished"]. "<br/>";
echo "Greased Bearings: ". $row["Grease"]. "<br/>";
echo "Air Cleaned: ". $row["Air"]. "<br/>";
echo "SS Screw: ". $row["Screw"]. "<br/>";
echo "Test Date: ". $row["Test_Date"]. "<br/>";
echo "Pass: ". $row["Pass"]. "<br/>";
echo "Fail: ". $row["Fail"]. "<br/>";
echo "Tester: ". $row["Tester"]. "<br/>";
echo "Final Check: ". $row["Final_Check"]. "<br/>";
echo "Green Dot: ". $row["Green_Dot"]. "<br/>";
echo "Green Dot Check: ". $row["Green_Dot_Check"]. "<br/>";
echo "Ship Date: ". $row["Ship_Date"]. "<br/>";
echo "Serial Number: ". $row["Serial_Number"]. "<br><br><hr><br/>";
}
} else {
echo "No Record Found!";
}
$connection->close();
?>
(clarification) The code works. I Just don't know how to format the table for it to display inside.
Hi :) Hope this helps :)
<?php
$server = "127.0.0.1";
$user = "admin";
$pass = "password";
$dbname = "hysteryalelogs";
// Create connection in mysqli
$connection = new mysqli($server, $user, $pass, $dbname);
//Check connection in mysqli
if($connection->connect_error){
die("Error on connection:" .$connection->connect_error);
}
//Display the informaion
$sql = "SELECT * FROM logs";
$res = $connection->query($sql);
if($res->num_rows > 0){
echo "<table border='3px' cellpadding='5px' cellspacing='5px' align='center' bgcolor='skyblue' ";
echo "<tr>";
echo "<td><center><strong>Sales Number</strong></center></td>";
echo "<td><center><strong>Quantity</strong></center></td>";
echo "<td><center><strong>Due Date</strong></center></td>";
echo "<td><center><strong>Burnished Housing</strong></center></td>";
echo "<td><center><strong>Greased Bearings</strong></center></td>";
echo "<td><center><strong>Air Cleaned</strong></center></td>";
echo "<td><center><strong>SS Screw</strong></center></td>";
echo "<td><center><strong>Test Date</strong></center></td>";
echo "<td><center><strong>Pass</strong></center></td>";
echo "<td><center><strong>Fail</strong></center></td>";
echo "<td><center><strong>Tester</strong></center></td>";
echo "<td><center><strong>Final Check</strong></center></td>";
echo "<td><center><strong>Green Dot</strong></center></td>";
echo "<td><center><strong>Green Dot Check</strong></center></td>";
echo "<td><center><strong>Ship Date</strong></center></td>";
echo "<td><center><strong>Serial Number</strong></center></td>";
echo "</tr>";
while($row = $res->fetch_assoc()){
echo "<tr>";
echo "<td>"."<center>"."<i>".$row["Sales_Number"]."</i>"."</center>"."</td>";
echo "<td>"."<center>"."<i>".$row["Quantity"]."</i>"."</center>"."</td>";
echo "<td>"."<center>"."<i>".$row["Due_Date"]."</i>"."</center>"."</td>";
echo "<td>"."<center>"."<i>".$row["Burnished"]."</i>"."</center>"."</td>";
echo "<td>"."<center>"."<i>".$row["Grease"]."</i>"."</center>"."</td>";
echo "<td>"."<center>"."<i>".$row["Air"]."</i>"."</center>"."</td>";
echo "<td>"."<center>"."<i>".$row["Screw"]."</i>"."</center>"."</td>";
echo "<td>"."<center>"."<i>".$row["Test_Date"]."</i>"."</center>"."</td>";
echo "<td>"."<center>"."<i>".$row["Pass"]."</i>"."</center>"."</td>";
echo "<td>"."<center>"."<i>".$row["Fail"]."</i>"."</center>"."</td>";
echo "<td>"."<center>"."<i>".$row["Tester"]."</i>"."</center>"."</td>";
echo "<td>"."<center>"."<i>".$row["Final_Check"]."</i>"."</center>"."</td>";
echo "<td>"."<center>"."<i>".$row["Green_Dot"]."</i>"."</center>"."</td>";
echo "<td>"."<center>"."<i>".$row["Green_Dot_Check"]."</i>"."</center>"."</td>";
echo "<td>"."<center>"."<i>".$row["Ship_Date"]."</i>"."</center>"."</td>";
echo "<td>"."<center>"."<i>".$row["Serial_Number"]."</i>"."</center>"."</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "No Record Found!";
}
$connection->close();
?>
I'm trying to fetch all rows using this below coding. Here you can see echo $row['UserName']; and echo $row['Address']; i can get all the username and address from database. But when i use this coding ($uname = $row['UserName'];echo $uname; and $uaddress = $row['Address'];echo $uaddress;) outside of while loop it always fetch the last row values. My question is how to fetch all row values outside of while loop?
<?php
include('config.php');
try
{
$stmt = $conn->prepare('SELECT * FROM ebusers');
$conn->errorInfo();
$stmt->execute();
while($row = $stmt->fetch())
{
echo "<a target=_blank href='edit.php?id=". $row['UserID'] ."'>Edit</a>";
echo "<br>";
echo "<a target=_blank href='delete.php?id=". $row['UserID'] ."'>Delete</a>";
echo $row['UserName'];
$uname = $row['UserName'];
echo "<br>";
echo $row['Address'];
$uaddress = $row['Address'];
echo "<br>";
}
}
catch(PDOException $e)
{
'Error : '.$e->getMesssage();
}
echo $uname;
echo $uaddress;
?>
Just pass them into an array
while($row = $stmt->fetch())
{
echo "<a target=_blank href='edit.php?id=". $row['UserID'] ."'>Edit</a>";
echo "<br>";
echo "<a target=_blank href='delete.php?id=". $row['UserID'] ."'>Delete</a>";
echo $row['UserName'];
$uname[] = $row['UserName']; // An array of usernames...
echo "<br>";
echo $row['Address'];
$uaddress[] = $row['Address']; // An another array of addresses
echo "<br>";
}
//Below two statements prints all usernames and addresses
print_r($uname); // or you can access like echo $uname[2] to view the second username
print_r($uaddress); // similarly you can do like that as you did for $uname..
//or if you want to loop through individually , you can make use of a foreach construct
foreach($uname as $usernames)
{
echo $usernames."<br>";
}
During the while loop, the $row variable get overwritten each time it executed.
You may have a try on FetchAll which prepares the array for you.
This is the code I am trying to use to display information from multiple MySQL tables in one table. I'm also trying to create an else clause if the table information doesn't exist. This is my failed attempt at it.
I keep editing this as I peck away at it but at this point I still can't get it to display the final else when the row doesn't exist.
<?
include"db.inc.php";//database connection
$item = "SELECT * FROM Item";
$result = mysql_query($item);
while ($row=mysql_fetch_array($result)) // Display information from Item Table.
{
echo ("<tr><td>Edit</td>");
echo ("<td>$row[ItemID]</td>");
echo ("<td>$row[Category]</td>");
echo ("<td>$row[Cost]</td>");
echo ("<td>$row[Condition]</td>");
echo ("<td>$row[PurchaseLot_PurchaseLotID]</td>");
echo ("<td>$row[Location]</td>");
echo ("<td>$row[Date]</td>");
echo ("<td>$row[Desc]</td>");
echo ("<td>$row[Notes]</td>");
echo ("<td>Pics</td>");
echo ("<td>Info</td></tr>");
$info = "SELECT * FROM Info WHERE Item_ItemID = $row[ItemID] ";
$info_results = mysql_query($info);
while ($info_row = mysql_fetch_array($info_results))
{
if ($info_row['Item_ItemID'] = $row['ItemID']) // Display Information from Info table If column exists with same ItemID.
{
echo ("<tr><td colspan=\"12\">");
echo ("Manufacturer:$info_row[Manufacturer]<br>");
echo ("Model:$info_row[Model]<br>");
echo ("Model Number:$info_row[ModelNumber]<br>");
echo ("Serial Number:$info_row[SerialNumber]<br>");
echo ("Service Number:$info_row[ServiceNumber]<br>");
echo ("</td></tr>");
} else //else dispaly Add information to info link.
{
echo ("<tr><td colspan=\"12\">Add Extra Information</td></tr>");
}
}
}
?>
you could re-write the SQL statement in your $info "SELECT * FROM Info" to "SELECT * FROM Info wHERE Item_ItemID=$row['ItemID'] ... and then delete the
if ($info_row['Item_ItemID'] = $row['ItemID'])
Plus you new a while ind your code after $info_results = mysql_query($info);
* EDITED *
then you do like this, instead of
while ($info_row = mysql_fetch_array($info_results))
{
if ($info_row['Item_ItemID'] = $row['ItemID']) // Display Information from Info table If column exists with same ItemID.
{
echo ("<tr><td colspan=\"12\">");
echo ("Manufacturer:$info_row[Manufacturer]<br>");
echo ("Model:$info_row[Model]<br>");
echo ("Model Number:$info_row[ModelNumber]<br>");
echo ("Serial Number:$info_row[SerialNumber]<br>");
echo ("Service Number:$info_row[ServiceNumber]<br>");
echo ("</td></tr>");
} else //else dispaly Add information to info link.
{
echo ("<tr><td colspan=\"12\">Add Extra Information</td></tr>");
}
}
try this
if(mysql_num_rows($info)>0){
while($info_row = mysql_fetch_array($info){
echo ("<tr><td colspan=\"12\">");
echo ("Manufacturer:$info_row[Manufacturer]<br>");
echo ("Model:$info_row[Model]<br>");
echo ("Model Number:$info_row[ModelNumber]<br>");
echo ("Serial Number:$info_row[SerialNumber]<br>");
echo ("Service Number:$info_row[ServiceNumber]<br>");
echo ("</td></tr>");
}
}else {
echo ("<tr><td colspan=\"12\">Add Extra Information</td></tr>");
}
i try to edit your code but im not able to try it.
<?
include"db.inc.php";//database connection
$item = "SELECT * FROM Item";
$result = mysql_query($item);
while ($row=mysql_fetch_array($result)) // Display information from Item Table.
{
echo ("<tr><td>Edit</td>");
echo ("<td>$row[ItemID]</td>");
echo ("<td>$row[Category]</td>");
echo ("<td>$row[Cost]</td>");
echo ("<td>$row[Condition]</td>");
echo ("<td>$row[PurchaseLot_PurchaseLotID]</td>");
echo ("<td>$row[Location]</td>");
echo ("<td>$row[Date]</td>");
echo ("<td>$row[Desc]</td>");
echo ("<td>$row[Notes]</td>");
echo ("<td>Pics</td>");
echo ("<td>Info</td></tr>");
$info = "SELECT * FROM Info where Item_ItemID = ".$row['ItemID'];
$info_results = mysql_query($info);
if ($info_row = mysql_fetch_array($info_results))
{
echo ("<tr><td colspan=\"12\">");
echo ("Manufacturer:$info_row[Manufacturer]<br>");
echo ("Model:$info_row[Model]<br>");
echo ("Model Number:$info_row[ModelNumber]<br>");
echo ("Serial Number:$info_row[SerialNumber]<br>");
echo ("Service Number:$info_row[ServiceNumber]<br>");
echo ("</td></tr>");
}
else { //else dispaly Add information to info link.
echo ("<tr><td colspan=\"12\">Add Extra Information</td></tr>");
}
}
?>
i hope this is the answer.
I am using this code which allows me to see my DB records in a table and in this table there is an option to delete or edit the records.
Only I get this error message and I can't figure out what I am doing wrong.
The error message:
Fatal error: Call to a member function prepare() on a non-object in C:\wamp\www\Inventaris++\NinjaCodeDelete.php on line 32
The code:
<?php
include'Connect2db3.php';
$action = isset($_GET['action']) ? $_GET['action']: "";
if($action=='delete'){ //if the user clicked ok, run our delete query
try {
$query = "DELETE FROM BCD WHERE id = ?";
$stmt = $conn->prepare($query);
$stmt->bindParam(1, $_GET['id']);
$result = $stmt->execute();
echo "<div>Record was deleted.</div>";
}catch(PDOException $exception){ //to handle error
echo "Error: " . $exception->getMessage();
}
}
$query = "SELECT ID, Categorie, SerieNummer, MacAdress, ProductCode, Prijs, RekNummer, PaletNummer, Hoeveelheid, Aantekeningen FROM BCD";
$stmt = $conn->prepare( $query );
$stmt->execute();
$num = $stmt->rowCount();
echo "<a href='reports.php'>View Reports</a>";
if($num>0){ //check if more than 0 record found
echo "<table border='1'>";//start table
echo "<tr>";
echo "<th>Categorie</th>";
echo "<th>SerieNummer</th>";
echo "<th>MacAdress</th>";
echo "<th>ProductCode</th>";
echo "<th>Prijs</th>";
echo "<th>RekNummer</th>";
echo "<th>PaletNummer</th>";
echo "<th>Hoeveelheid</th>";
echo "<th>Aantekeningen</th>";
echo "</tr>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
echo "<tr>";
echo "<td>{$Categorie}</td>";
echo "<td>{$SerieNummer}</td>";
echo "<td>{$MacAdress}</td>";
echo "<td>{$ProductCode}</td>";
echo "<td>{$Prijs}</td>";
echo "<td>{$RekNummer}</td>";
echo "<td>{$PaletNummer}</td>";
echo "<td>{$Hoeveelheid}</td>";
echo "<td>{$Aantekeningen}</td>";
echo "<td>";
echo "<a href='edit.php?id={$id}'>Edit</a>";
echo " / ";
echo "<a href='#' onclick='delete_user( {$id} );'>Delete</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";//end table
}else{
echo "No records found.";
}
?>
<script type='text/javascript'>
function delete_user( id ){
var answer = confirm('Are you sure?');
if ( answer ){
window.location = 'NinjaCodeDelete.php?action=delete&id=' + id;
}
}
</script>
I also want to say that I am not an advanced programmer I found this code online, where it seemed to be working for the other people who have used it.
I have some experience with Mysql and php but not with PDO.
I hope u can help me!
thank you in advanced.
What is inside your " include'Connect2db3.php'; "?
And with this I'm refering to connect2db3.php and is this file inside the right folder?
Your connection could look like this:
<?php
$config['conn'] = array(
'host' => 'yourHostName',
'username' => 'yourUserName',
'password' => 'yourPassword',
'dbname' => 'yourDBName'
);
$conn = new PDO('mysql:host=' . $config['conn']['host'] . ';dbname=' . $config['conn']['dbname'], $config['conn']['username'], $config['conn']['password']);
?>