retrieve base64 images from database - php

I have created simple table and stored an images with base64 as a TEXT ,
when I tried to fetch all data's from database, it works well, Except the images is displaying on website like below code.
LzlqLzRBQVFTa1pKUmdBQkFRQUFBUUFCQUFELzJ3QkRBQkFMREE0TUNoQU9EUTRT ..etc
php code.
<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<title> Welcome </title>
<script> </script>
<style></style>
</head>
<body>
<table border = "solid">
<tr>
<th> id </th>
<th> Millitary number </th>
<th> Fname </th>
<th> Lname </th>
<th> Image </th>
</tr>
<?php
$conn = mysqli_connect("localhost" , "id2549459_salamnti" , "0000000000" , "id2549459_tutorial");
if($conn -> connect_error){
die("my connection faild" . $conn -> connect_error);
}
$sql = "SELECT id, militry_num , firstname, lastname,image from students";
$result = $conn-> query($sql);
if($result -> num_rows > 0){
while($row = $result-> fetch_assoc()){
echo "<tr> <td>" . $row["id"] . "</td> <td>"
. $row["militry_num"] .
"</td> <td>". $row["firstname"] .
"</td><td>" . $row["lastname"] . "</td><td>" .
base64_encode($row["image"]) .
"</td> </tr>";
}
echo "</table>";
}
else{
echo "0 result" . $conn->error;
}
$conn -> close();
?>
</table>
</body>
</html>
any solution how do display the images from db for my current problem?

Decode your image string in data:image/gif;base64
<img src="data:image/gif;base64,' . $row["image"] . '" />
<head>
<meta charset = "utf-8">
<title> Welcome </title>
<script> </script>
<style></style>
</head>
<body>
<table border = "solid">
<tr>
<th> id </th>
<th> Millitary number </th>
<th> Fname </th>
<th> Lname </th>
<th> Image </th>
</tr>
<?php
$conn = mysqli_connect("localhost" , "id2549459_salamnti" , "0000000000" , "id2549459_tutorial");
if($conn->connect_error){
die("my connection faild" . $conn->connect_error);
}
$sql = "SELECT id, militry_num , firstname, lastname,image from students";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo "<tr> <td>" . $row["id"] . "</td> <td>"
. $row["militry_num"] .
"</td> <td>". $row["firstname"] .
"</td><td>" . $row["lastname"] . "</td><td>" .
'<img src="data:image/gif;base64,' . $row["image"] . '" />' .
"</td> </tr>";
}
echo "</table>";
}
else{
echo "0 result" . $conn->error;
}
$conn->close();
?>
</table>
</body>

Related

PHP - Display Serial Numbers automatically using for loop

<thead>
<tr>
<th> S.No </th>
<th> Movie Name </th>
<th> Language </th>
</tr>
</thead>
<?php
$result = mysqli_query($connect,"SELECT * FROM movies ;")or die(mysqli_error());
$rows = mysqli_fetch_array($result);
?>
<tbody>
<?php
$counter=0;
foreach($result as $rows)
{
$counter=$counter+1;
?>
<?php echo "<tr><td>" . $counter . "</td><td>" . $rows['language'] . "</td><td>" . $rows['movie_name'] . "</td></tr>"; ?>
<?php } ?>
</tbody>
I want to display serial numbers in the table automatically for the fetched results . Here the serial number displays correctly for the first five results like 1,2,3,4,5 rows
but on the 2nd page the number shows like 8,9,10,6,7
Where i am making mistake ? I even tried while loop and forloop increment counter. Im using Bootstrap data table to display the results from database.
<?php
$result = mysqli_query($connect,"SELECT * FROM movies");
$counter=0;
while($rows = mysqli_fetch_array($result)){
echo "<tr><td>" . $counter . "</td>
<td>" . $result[$counter]['language'] . "</td>
<td>" . $result[$counter]['movie_name'] . "</td>
</tr>";
$counter++;
}
?>
This should work for you. You need to fetch the results inside a loop in order to parse them.
Try this
You have to declare the $i=1 outside of the while loop and $i++ inside the while loop. So it will display the number of rows available in the database.
You have to use while loop instated of foreach.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM movies";
$result = mysqli_query($conn, $sql);
//mysqli_close($conn);
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<table>
<thead>
<tr>
<th> S.No </th>
<th> Movie Name </th>
<th> Language </th>
</tr>
</thead>
<tbody>
<?php
$i=1;
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($rows = mysqli_fetch_assoc($result)) {
echo "<tr>
<td>" . $i . "</td>
<td>" . $rows['language'] . "</td>
<td>" . $rows['movie_name'] . "</td>
</tr>";
$i++;
}
} else {
echo "0 results";
}
?>
</tbody>
</table>
</body>
</html>

PHP update MySQL and export to csv

I have a PHP page that shows pending claims which can have their status changed when a checkbox is selected and the array updated.
<?php include("partials/header.php"); ?>
<p>Manage expenses here!</p>
<body>
<style type="text/css">
#dis{
display:none;
}
</style>
<div id="dis">
<!-- here message will be displayed -->
</div>
<p>Current Claims</p>
<div class="container">
<form method="post" action="export.php" enctype="multipart/form-data" >
<table cellspacing="0" width="100%" id="example" class="table table-striped table-hover table-responsive">
<thead>
<tr>
<th>ID</th>
<th>Employee ID</th>
<th>Ref</th>
<th>Date</th>
<th>Filed Date</th>
<th>Type</th>
<th>Client</th>
<th>Project</th>
<th>Narrative</th>
<th>Net</th>
<th>VAT</th>
<th>Status</th>
<th></th>
</tr>
</thead>
<tbody>
<?php
//connection details
require_once 'dbconfig.php';
//end connection details
$query = "SELECT * FROM claim WHERE status='pending'";
$result = mysql_query($query);
if($result === false) {
die(mysql_error());
}
while($row = mysql_fetch_array($result)){
$row_id = $row['c_id'];
$employee_id = $row['employee_id'];
$claim_ref = $row['claim_ref'];
$transaction_date = $row['transaction_date'];
$filed_date = $row['filed_date'];
$expense_type_id = $row['expense_type_id'];
$client_code_id = $row['client_code_id'];
$project_code_id = $row['project_code_id'];
$narrative = $row['narrative'];
$net = $row['net'];
$vat = $row['vat'];
$status = $row['status'];
echo
"<tr>
<td>" . $row_id . "</td>
<td>" . $employee_id . "</td>
<td>" . $claim_ref . "</td>
<td>" . $transaction_date . "</td>
<td>" . $filed_date . "</td>
<td>" . $expense_type_id . "</td>
<td>" . $client_code_id . "</td>
<td>" . $project_code_id . "</td>
<td>" . $narrative . "</td>
<td>" . $net . "</td>
<td>" . $vat . "</td>
<td>" . $status . "</td>
<td><input type='checkbox' name='c_id[]' value='" . $row_id . "'/></td>
</tr>";
}
echo "</tbody></table><input type='submit' value='accept'></form>"; //end form
/***** Update Status *****/
/*print_r($_POST);*/
if(gettype($_POST['c_id'])=="array"){
foreach($_POST['c_id'] as $val){
$id_c = $val;
$query2 = "UPDATE claim SET status = 'accepted' where c_id='".$id_c."'";
$result2 = mysql_query($query2);
if($result2 === false) {
die(mysql_error());
}
echo "Status " .$id_c. " is updated. <br>";
}
}
mysql_close();
?>
</tbody>
</table>
</div>
</div>
<br />
</script>
</body>
</html>
Q1. I was wondering whether the rows which are having the status column changed to accepted, can somehow also be exported to a csv file.
Q2. Is there a way to have two buttons, one for accepting and the other one for rejecting as each claim has three possible statuses: pending, accepted, rejected
Update: the claim table is as follows: http://sqlfiddle.com/#!9/5ce91
create table claim (
c_id INT(5) zerofill primary key auto_increment,
claim_ref VARCHAR(7),
employee_id integer NOT NULL REFERENCES employee(emp_id),
transaction_date DATE,
filed_date DATE,
expense_type_id VARCHAR(20) REFERENCES expense_type(expense_type),
client_code_id VARCHAR(7) REFERENCES client(client_code),
project_code_id VARCHAR(10) REFERENCES project(project_code),
narrative VARCHAR(255),
net decimal(6,2),
vat decimal(6,2),
status ENUM('pending', 'rejected', 'accepted'))
ENGINE = InnoDB;
Another bit is that the project ends in about a week so I'd rather not get into updating to PDO or MySQLi although if anyone has a complete solution in one of these it would be just as good, thanks.

HTML table error in php

i am new to php and i try to solve this many times but i couldn't.
this is my php code
Can someone help me with this it should be easy for a php expert.
<?php
require_once 'connector.php';
$result = mysql_query('SELECT * FROM highscores ORDER BY score DESC');
$username = mysql_query('SELECT username FROM users WHERE id in(SELECT user_id FROM highscores)');
echo"<html>
<head>
<title>Highscores</title>
</head>
<body>
<table border='1'>
<tr>
<th>user</th>
<th>score</th>
<th>Date</th>
</tr>
";
while ($name = mysql_fetch_array($username) )
{
echo "<tr>
<td>" . $name ['username'] . "</td>";
}
while( $row = mysql_fetch_array($result))
{
echo"
<td>" . $row ['score'] . "</td>
<td>" . $row ['date'] . "</td>
</tr>";
}
echo"
</table>
</body>
</html>
";
the table i want to take
mysql_* is deprecated! use mysqli_*
<?php
require_once 'connector.php';
$SQL = "SELECT u.username, h.score, h.date
FROM users AS u
JOIN highscores AS h ON (h.user_id = u.users_id)";
$result = mysql_query($SQL) or die( mysql_error() );
echo "<html>
<head>
<title>Highscores</title>
</head>
<body>";
if( mysql_num_rows($result) > 0 )
{
echo "<table border='1'>
<tr>
<th>user</th>
<th>score</th>
<th>Date</th>
</tr>";
while ( $row = mysql_fetch_array($result) )
{
echo "<tr>";
printf("<td>%s</td>", $row['username']);
printf("<td>%s</td>", $row['score']);
printf("<td>%s</td>", $row['date']);
echo "</tr>";
}
echo "</table>
</body>
</html>";
}
mysql_free_result($result);
Can you try this,
$result = mysql_query('SELECT hs.score, hs.date, u.username FROM highscores as hs, users as u where hs.user_id=u.id ORDER BY score DESC');
echo "<html>
<head>
<title>Highscores</title>
</head>
<body>
<table border='1'>
<tr>
<th>user</th>
<th>score</th>
<th>Date</th>
</tr>
";
while( $row = mysql_fetch_array($result))
{
echo"<tr>
<td>" . $row ['username'] . "</td>
<td>" . $row ['score'] . "</td>
<td>" . $row ['date'] . "</td>
</tr>";
}
Instead of putting everything in a double quote, you can use string concatenation with (.) operator. For example, Instead of writing like this
echo"<html>
<head>
<title>Highscores</title>
</head>
<body>
<table border='1'>
<tr>
<th>user</th>
<th>score</th>
<th>Date</th>
</tr>
";
You can write like this:
echo "<html>" .
"<head>" .
"<title>Highscores</title>"
"</head>" .
"<body>" .
"<table border='1'>" .
"<tr>" .
"<th>user</th>" .
"<th>score</th>" .
"<th>Date</th>" .
"</tr>" ;
The following code block
echo "<tr>
<td>" . $name ['username'] . "</td>";
Should be written as
echo "<tr>" .
"<td>" . $name ['username'] . "</td>";
In this way the code looks more readable as well.
The second loop needs to be inside the first loop, and the closing </tr> shouldn't be inside the second loop.
<?
while ($name = mysql_fetch_array($username)) {
echo "<tr>";
echo "<td>" . $name["username"] . "</td>";
while ($row = mysql_fetch_array($result)) {
echo "<td>" . $row["score"] . "</td>";
echo "<td>" . $row["date"] . "</td>";
}
echo "</tr>";
}
?>

Hiding a table if a search result does not exist

Exactly what the title says. I want the table containing all of the search queries to be hidden, but I've tried a lot of things and none of them work. For example, if($myData!=null) {proceed with showing the table}, but that didn't work. isset() also didn't work. Any ideas?
<style>
ul
{
list-style-type: none;
}
</style>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Search Chemicals</title>
</head>
<p>
<body>
<h3>Chemical Information</h3>
<p>You may search by Catalog number, CASRN, or the chemical name.</p>
<form method="post" action="search.php?go" id="searchform">
<input type="text" name="name">
<input type="submit" name="submit" value="Search">
</form>
<?php
error_reporting(0);
if (isset($_POST['submit'])) {
if (isset($_GET['go'])) {
if (preg_match("/^[a-zA-Z0-9]+/", $_POST['name'])) {
$name = $_POST['name'];
$conn = mysql_connect("localhost", "Blimeo", "password");
$db = mysql_connect("localhost", "-", "-") or die('I cannot connect to the database because: ' . mysql_error());
//-select the database to use
$mydb = mysql_select_db("chemicals");
//-query the database table
$sql = "SELECT * FROM products WHERE Catalog LIKE '%" . $name . "%' OR CASRN LIKE '%" . $name . "%' OR Chemical_Name LIKE '%" . $name . "%'";
$myData = mysql_query($sql, $conn);
echo "<table border=1>
<tr>
<th>Catalog</th>
<th>Image</th>
<th>CASRN</th>
<th>Chemical Name</th>
<th>Quantity 1</th>
<th>Price 1</th>
<th>Quantity 2</th>
<th>Price 2</th>
<th>Quantity 3</th>
<th>Price 3</th>
<th>Quantity 4</th>
<th>Price 4</th>
</tr>";
while ($record = mysql_fetch_array($myData)) {
echo "<tr>";
echo "<td>" . $record['Catalog'] . "</td>";
echo "<td><img src=\"./img/" . $record['Image'] . "\" alt=\"Chemical\"/></td>";
echo "<td>" . $record['CASRN'] . "</td>";
echo "<td>" . $record['Chemical_Name'] . "</td>";
echo "<td>" . $record['Quantity1'] . "</td>";
echo "<td>" . $record['Price1'] . "</td>";
echo "<td>" . $record['Quantity2'] . "</td>";
echo "<td>" . $record['Price2'] . "</td>";
echo "<td>" . $record['Quantity3'] . "</td>";
echo "<td>" . $record['Price3'] . "</td>";
echo "<td>" . $record['Quantity4'] . "</td>";
echo "<td>" . $record['Price4'] . "</td>";
echo "</tr>";
echo "</form>";
echo "<ul>\n";
echo "<li>" . "" . $Catalog . " " . $CASRN . " " . $Chemical_Name . "</li>\n";
echo "</ul>";
}
}
} else {
echo "<p>Product not found! Please rephrase your search criteria.</p>";
}
}
?>
</body>
</html>
</p>
You should add mysql_num_rows();
$myData = mysql_query($sql, $conn);
$exists = mysql_num_rows($myData);
if($exists) {
echo "<table border=1>";
//..................
echo "</table>";
} else {
echo "<p>Product not found! Please rephrase your search criteria.</p>";
}
Well, it seems you are echoing out your table regardless of the results of the search query. You should probably check the number of rows returned in teh result set and only echo out the table if the count is > 0.
Use <div visibility="hidden"> to hide the table and use Javascript to change the visibility based on the search queries or some other condition.

What am i doing wrong here?

I've been staring at this page for over an hour. My update function just doesnt not seem to update. When i tried it through sql it seems ok. I have a form at the bottom of this page which updates a field in a table. Can anyone spot the mistakes?
<?php
// First of all initialise the user and check for permissions
require_once "/var/www/users/user.php";
$user = new CHUser(2);
// Initialise the template
require_once "/var/www/template/template.php";
$template = new CHTemplate();
// And create a cid object
require_once "/var/www/Testing/DisplayWIPOnLocation.php";
$BundleProgress= new CHWIPProgress();
if(isset($_GET['Reference'])){
$todays_date = date("Y-m-d H:i:s");
$content .= " <h3> Details for Bundle : $reference </h3> ";
$bundle = $BundleProgress->GetBundle($_GET['Reference']);
$reference = $_GET['Reference'];
// Now show the details
foreach($bundle as $x){
$content .= "
<table>
<tr>
<th> Location </th>
<td>" . $x['Description'] . "</td>
</tr>
<tr>
<th> Works Order Number </th>
<td>" . $x['WorksOrder'] . "</td>
</tr>
<tr>
<th> Bundle Number </th>
<td>" . $x['Number'] . "</td>
</tr>
<tr>
<th>Qty Issued</th>
<td>" . $x['Qty'] . "</td>
</tr>
<tr>
<th>Bundle Reference </th>
<td>" . $x['Reference'] . "</td>
</tr>
<tr>
<th>Style description</th>
<td>" . $x['Stock'] . "</td>
</tr>
<tr>
<th>Due Date</th>
<td>" . $x['DueDate'] . "</td>
</tr>
<tr>
<th>Date In </th>
<td>" . $x['DateIN'] . "</td>
</tr>
<tr>
<th>Date Out</th>
<td>" . $x['DateOUT'] . "</td>
</tr>
<tr>
<th>Last Code</th>
<td>" . $x['Last'] . "</td>
</tr>
</table>
<br> ";
}
$content .= " </table>
<form action='viewBundle.php?step=2' method='post'>
<p>Reason: <input type='text' name='reason' /><br
/><p>
<p><input type='hidden' name='bundlereference'
id='Username' value='" . $x['Reference'] . "' />
<input type='submit' name ='add'/></form>
</table> ";
if($_GET['step'] == 2) {
$BundleProgress->UpdateReason($_POST['reason'],$_POST['bundlereference']);
$content .= " <a href='index.php?location=" .
$x['Description'] . "'> updated</a> ";
}
}
else {
$content .= "<h3>Something has gone wrong</h3>
<br>
<a href='index.php?location=" . $x['Description'] . "'> Return to Previous
Page </a>
";
}
$template->SetTag("content", $content);
echo $template->Display();
?>
Function
public function UpdateReason($reason, $bundlereference) {
$sql = "UPDATE `ArchiveBundle`
SET `Issue` = " . $reason . "
WHERE `BundleReference` = " . $bundlereference .
";";
mysql_select_db(DB_DATABASE_NAME, $this->conn);
return mysql_query($sql, $this->conn);
}
change:
if($_GET['step'] == 2)
to:
if((int)$_GET['step'] === 2)
and:
public function UpdateReason($reason, $bundlereference) {
$sql = "UPDATE `ArchiveBundle`
SET `Issue` = " . $reason . "
WHERE `BundleReference` = " . $bundlereference .
";";
mysql_select_db(DB_DATABASE_NAME, $this->conn);
return mysql_query($sql, $this->conn);
}
to:
public function UpdateReason($reason, $bundlereference) {
mysql_select_db(DB_DATABASE_NAME, $this->conn);
$_reason = mysql_real_escape_string($reason,$this->conn);
$_bundlereference = mysql_real_escape_string($bundlereference,$this->conn);
$sql = "UPDATE `ArchiveBundle`
SET `Issue` = '" . $_reason . "'
WHERE `BundleReference` = '" . $_bundlereference . "'";
return mysql_query($sql, $this->conn);
}
Try that. Code hasn't been tested but it's a good place to start.
To try and debug what's going on here do the following:
public function UpdateReason($reason, $bundlereference) {
error_reporting(E_ALL ^ E_NOTICE);
$db_selected = mysql_select_db(DB_DATABASE_NAME, $this->conn);
if (!$db_selected) {
die("Can't use db : " . mysql_error());
}
$_reason = mysql_real_escape_string($reason,$this->conn);
$_bundlereference = mysql_real_escape_string($bundlereference,$this->conn);
$sql = "UPDATE `ArchiveBundle`
SET `Issue` = '" . $_reason . "'
WHERE `BundleReference` = '" . $_bundlereference . "'";
mysql_query($sql, $this->conn);
die(mysql_error());
}
Also, it looks like on your form submission you're not passing in the Reference parameter so the if(isset($_GET['Reference'])) will fail when you post the form. I've change the table and form code below to make it more readable, pass in the Reference param on form submission, and also to update the db record BEFORE fetching the dataset so you'll see the updated records in the table returned.
// First of all initialise the user and check for permissions
require_once "/var/www/users/user.php";
$user = new CHUser(2);
// Initialise the template
require_once "/var/www/template/template.php";
$template = new CHTemplate();
// And create a cid object
require_once "/var/www/Testing/DisplayWIPOnLocation.php";
$BundleProgress= new CHWIPProgress();
if(isset($_GET['Reference'])){
if($_GET['step'] == 2) {
$BundleProgress->UpdateReason($_POST['reason'],$_POST['bundlereference']);
}
$todays_date = date("Y-m-d H:i:s");
$content .= " <h3> Details for Bundle : $reference </h3> ";
$bundle = $BundleProgress->GetBundle($_GET['Reference']);
$reference = $_GET['Reference'];
// Now show the details
foreach($bundle as $x){
$content .= "
<table>
<tr><th> Location </th><td>" . $x['Description'] . "</td></tr>
<tr><th> Works Order Number </th><td>" . $x['WorksOrder'] . "</td></tr>
<tr><th> Bundle Number </th><td>" . $x['Number'] . "</td></tr>
<tr><th>Qty Issued</th><td>" . $x['Qty'] . "</td></tr>
<tr><th>Bundle Reference </th><td>" . $x['Reference'] . "</td></tr>
<tr><th>Style description</th><td>" . $x['Stock'] . "</td></tr>
<tr><th>Due Date</th><td>" . $x['DueDate'] . "</td></tr>
<tr><th>Date In </th><td>" . $x['DateIN'] . "</td></tr>
<tr><th>Date Out</th><td>" . $x['DateOUT'] . "</td></tr>
<tr><th>Last Code</th><td>" . $x['Last'] . "</td></tr>
</table>
<br>";
}
$content .= "<table>
<form action='viewBundle.php?Reference=" . $_GET['Reference'] . "&step=2' method='post'>
<p>Reason: <input type='text' name='reason' /></p><br/>
<p><input type='hidden' name='bundlereference' id='Username' value='" . $x['Reference'] . "' /></p>
<input type='submit' name ='add'/>
</form>
</table>";
} else {
$content .= "<h3>Something has gone wrong</h3>
<br>
<a href='index.php?location=" . $x['Description'] . "'> Return to Previous Page </a>
";
}
$template->SetTag("content", $content);
echo $template->Display();
Can I advise you break this down by first checking what mysql_query returns. It maybe that this particular variable is defined incorrectly. Also remember add quotes to the values in your query.

Categories