I had this project wherein, I am supposed to insert and select pictures, as in upload and view them. Initially, when I tried a code, it worked but the image didn't display. After many trials, I just copied a code from a mate which worked for him but when I tried, it didn't. Why? I have posted everything below-
Image upload code
<head>
<title>Retrieve Image</title>
</head>
<body>
<h1>Retrieving Image from database using PHP</h1>
<table border="1px" >
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Image</th>
</tr>
</thead>
<?php
$connection = mysqli_connect("localhost","root","");
$db = mysqli_select_db($connection,"olap");
$query = "SELECT * FROM `blobclob`";
$query_run = mysqli_query($connection,$query);
while($row = mysqli_fetch_array($query_run))
{
?>
<tr>
<td> <?php echo $row['id']; ?></td>
<td> <?php echo $row['name']; ?></td>
<td> <?php echo '<img
src="data:image;base64,'.base64_encode($row['image']).' " alt="FlowerImage"
style="width:100px; height:100px;">'; ?></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
Image View Code
<html>
<head>
<title>Retrieve Image</title>
</head>
<body>
<h1>Retrieving Image from database using PHP</h1>
<table border="1px" >
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Image</th>
</tr>
</thead>
<?php
$connection = mysqli_connect("localhost","root","");
$db = mysqli_select_db($connection,"olap_exp");
$query = "SELECT * FROM `images`";
$query_run = mysqli_query($connection,$query);
while($row = mysqli_fetch_array($query_run))
{
?>
<tr>
<td> <?php echo $row['id']; ?></td>
<td> <?php echo $row['name']; ?></td>
<td> <?php echo '<img
src="data:image;base64,'.base64_encode($row['image']).' " alt="FlowerImage"
style="width:100px; height:100px;">'; ?></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
Output
Database
it should be something like this
echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image']).'"/>';
it should be **src="data:image/jpeg;base64 ... ** instead of src="data:image..
Related
I am trying to fetch data from an sql database into a php table. Yet the problem is that the first data from my database doesn't appear into the table. Did i commit a mistake ? The following is my code....
<?php
include "koneksi.php";
$sql = mysqli_query ($link,
"SELECT * FROM absen");
$data = mysqli_fetch_array($sql);
?>
<html>
<head>
<title>Data Mahasiswa</title>
</head>
<body>
<p><h2><b><center>DATA MAHASISWA</center></b></h2></p>
<table border="2" style="1000px;" align="center">
<tr bgcolor="blue">
<th>No</th>
<th>Nama</th>
<th>NIM</th>
<th>Jenis Kelamin</th>
</tr>
<?php
while($data)
while($data = mysqli_fetch_array($sql)){
?>
<tr>
<td><?php echo $data['no']; ?></td>
<td><?php echo $data['nama']; ?></</td>
<td><?php echo $data['nim']; ?></</td>
<td><?php echo $data['jenis_kelamin']; ?></</td>
</tr>
<?php } ?>
</table>
<center><b><h3><a href="Website.html"><img src="Capture.jpg" width="100px">
</a></h3></b></center>
</p>
</body>
</html>
You are doing mysqli_fetch_array() twice. Fixed code:
<?php
include "koneksi.php";
$sql = mysqli_query ($link,
"SELECT * FROM absen");
?>
<html>
<head>
<title>Data Mahasiswa</title>
</head>
<body>
<p><h2><b><center>DATA MAHASISWA</center></b></h2></p>
<table border="2" style="1000px;" align="center">
<tr bgcolor="blue">
<th>No</th>
<th>Nama</th>
<th>NIM</th>
<th>Jenis Kelamin</th>
</tr>
<?php
while($data = mysqli_fetch_array($sql)){
?>
<tr>
<td><?php echo $data['no']; ?></td>
<td><?php echo $data['nama']; ?></</td>
<td><?php echo $data['nim']; ?></</td>
<td><?php echo $data['jenis_kelamin']; ?></</td>
</tr>
<?php } ?>
</table>
<center><b><h3><a href="Website.html"><img src="Capture.jpg" width="100px">
</a></h3></b></center>
</p>
</body>
</html>
This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 5 years ago.
I have using php code with mysql database showing data into html table, and i'm using filter on listview, now I'm stack with get price Total of the list after filtered. I'm not good enough with php code, am I made mistake with php or sql?
MySQL table:
id INT -
name VARCHAR -
code VARCHAR -
price INT
here is my code so far:
<?php
if(isset($_POST['search']))
{
$valueToSearch = $_POST['valueToSearch'];
// search in all table columns
// using concat mysql function
$query = "SELECT * FROM `toy` WHERE CONCAT(`id`, `name`, `code`, `price`)
LIKE '%".$valueToSearch."%'";
$search_result = filterTable($query);
}
else {
$query = "SELECT * FROM `toy` LIMIT 5";
$search_result = filterTable($query);
}
// function to connect and execute the query
function filterTable($query)
{
$connect = mysqli_connect("localhost", "user", "pass",
"database");
$filter_Result = mysqli_query($connect, $query);
return $filter_Result;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Wu</title>
<link href="style.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div id="toys-grid">
<div class="search-box">
<br>
<form action="index.php" method="post">
<input type="text" name="valueToSearch" placeholder="Name To Search">
<br><br>
<input type="submit" name="search" value="Filter"><br><br>
</div>
<table cellpadding="10" cellspacing="1">
<thead>
<tr>
<th>Id</th>
<th>First Name</th>
<th>Code</th>
<th>Price</th>
</tr>
</thead>
<!-- populate table from mysql database -->
<?php while($row = mysqli_fetch_array($search_result)):?>
<tbody>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['code'];?></td>
<td><?php echo $row['price'];?></td>
</tr>
</tbody>
<?php endwhile;?>
<thead>
<tr>
<th> Total </th>
<th>
<?php
$results = mysql_query('SELECT SUM(price) FROM toy');
$results->execute();
for($i=0; $rows = $results->fetch(); $i++){
echo $rows['SUM(price)'];
}
?>
</th>
</tr>
</thead>
</table>
</form>
</div>
</body>
I appreciate your help...
No need to execute query for getting sum. You can do it with php
<table cellpadding="10" cellspacing="1">
<thead>
<tr>
<th>Id</th>
<th>First Name</th>
<th>Code</th>
<th>Price</th>
</tr>
</thead>
<!-- populate table from mysql database -->
<?php $sum = 0; ?>
<?php while($row = mysqli_fetch_array($search_result)):?>
<tbody>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['code'];?></td>
<td><?php echo $row['price'];?></td>
<?php
$sum += $row['price'];
?>
</tr>
</tbody>
<?php endwhile;?>
<thead>
<tr>
<th> Total </th>
<th>
<?php
echo $sum;
?>
</th>
</tr>
</thead>
</table>
This is my whole html and php code
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2>Student Details</h2>
<div id="EditStudent">
<img src="sawiro/EditImage.jpg" alt="Edit Student Image">
</div>
<table style="width: 100%" border ="0" bordercolor="green" bgcolor="white" cellspacing="20" cellpadding="0">
<thead>
<tr>
<?php
$Carrier = 0;
function clearInput($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if(isset($_GET['SelectedId'])){
$Carrier = clearInput($_GET['SelectedId']);
}
$cnx = new PDO('mysql:host=localhost;dbname=transportsystem;charset=utf8', 'root', '');
$result = $cnx->query("SELECT * FROM person
INNER JOIN student ON person.PerID = student.StPersonID
WHERE PerID = $Carrier");
while($row = $result->fetch()){
$Name = $row["Name"];
$Gender = $row["Gender"];
$Telephone = $row["Telephone"];
$Class = $row["Class"];
echo '<th colspan="8" style="font-size:40px;">'.$row["Name"].'</th>';
}
?>
</tr>
</thead>
<tr style="color: #000;">
<th> Student ID:</th>
<td><?php echo $Carrier;?></td>
</tr>
<tr>
<th> Full Name:</th>
<td><?php echo $Name;?></td>
</tr>
<tr>
<th> Gender:</th>
<td><?php echo $Gender;?></td>
</tr>
<tr>
<th> Telephone:</th>
<td><?php echo $Telephone;?></td>
</tr>
<tr>
<th> Class:</th>
<td><?php echo $Class;?></td>
</tr>
</table>
</body>
</html>
I want to add Selectionid, as you can see on it
?SelectionId=$Carrier;
where my id variable is $Carrier becuase of this code
if(isset($_GET['SelectedId']))
{$Carrier = clearInput($_GET['SelectedId']);}
I want once I click on <img src="sawiro/EditImage.jpg" alt="Edit Student Image"> to give me url UpdateStudent.php?SelectionId=8 for example, to use this id for setting data purpose.
Please help me to collect the id from the target php to use in to another php to update that particular row.
I think you meant it like this:
<a href="UpdateStudent.php?SelectionId=<?php echo $Carrier ;?>">
<img src="sawiro/EditImage.jpg" alt="Edit Student Image"/>
</a>
<?php include("header.php"); ?>
<?php
if (#$_POST['delete']=="Delete"){
$count=count($_POST['delbx']);
for($i=0;$i<$count;$i++){
$delete = "DELETE FROM admin WHERE a_id='".$_POST['delbx'][$i]."'";
$resulty = mysqli_query($conn, $delete) or die(mysql_error());
$select_delete = "SELECT `a_image` FROM admin WHERE a_id='".$_POST['delbx'][$i]."'";
$resultrowdy = $conn->query($select_delete);
$rowdy = $resultrowdy->fetch_assoc();
$path="admin/".$rowdy['a_image'];
echo $path;
unlink($path);
echo '<script>window.location="view_user.php"</script>';
}
} ?>
<div class="table-responsive">
<table class="table">
<caption>All Users</caption>
<?php
$sql = "SELECT a_id, a_name, a_phone, a_password, a_role, a_mail, a_image FROM admin";
$result = $conn->query($sql);
if ($result->num_rows > 0) {?>
<thead>
<tr>
<th><form action="view_user.php" method="post"><input name="delete" type="submit" id="delete" value="Delete"></th><th>S. No.</th> <th>Name</th> <th>Phone No.</th> <th>Mail Id</th> <th>Role</th> <th>Password</th> <th>Image</th>
</tr>
</thead>
<?php
while($row = $result->fetch_assoc()) { ?>
<tbody>
<tr>
<th scope="row">
<?php echo $row["a_id"]; ?>
</th>
<td align="center" bgcolor="#FFFFFF">
<input name="delbx[]" type="checkbox" id="delbx[]" value="<?php echo $row["a_id"]; ?>" />
</td>
<td>
<?php echo $row["a_name"]; ?>
</td>
<td>
<?php echo $row["a_phone"]; ?>
</td>
<td>
<?php echo $row["a_mail"]; ?>
</td>
<td>
<?php echo $row["a_role"]; ?>
</td>
<td>
<?php echo $row["a_password"]; ?>
</td>
<td>
<img src="admin/<?php echo $row["a_image"]; ?>" width="60" height="40">
</td>
<th>
Edit
</th>
</tr>
</tbody>
<?php
}
} else {
echo "0 results";
}?>
</table>
</form>
</div>
<?php include("footer.php"); ?>
The code I mention is not deleting the multiple images from the source folder but deleting the multiple data from database whereas I am trying to delete images from the source folder along with data please help thanks in advance
One of the problem is you are deleting the row and trying to select image column from the deleted row.. dont use user supplied variables directly in your query
your code should be
for($i=0;$i<$count;$i++){
$select_delete = "SELECT `a_image` FROM admin WHERE a_id='".$_POST['delbx'][$i]."'";
$resultrowdy = $conn->query($select_delete);
$rowdy = $resultrowdy->fetch_assoc();
$delete = "DELETE FROM admin WHERE a_id='".$_POST['delbx'][$i]."'";
if(mysqli_query($conn, $delete)){
$path="admin/".$rowdy['a_image'];
unlink($path);
echo '<script>window.location="view_user.php"</script>';
}
}
I am making a site linked with a database. It sorts transactions for budgeting reasons. For the store search section, I have a select drop down menu. I have linked up the data so that the drop down only shows stores that are in the database and automatically adds them as they change dynamically.
My issue is that I need a way to actually use the select options as search terms. Here is the initial page.
<!DOCTYPE html>
<html>
<head>
<title>Output 1</title>
</head>
<body>
<h1>Required Output 1</h1>
<h2>Transaction Search</h2>
<form action="output1out.php" method="get">
Search Store:<br/>
<input type="text" name="StoreName">
<br/>
<input name="Add Merchant" type="submit" value="Search">
</form>
<?php
require_once 'login.php';
$connection = mysqli_connect($db_hostname, $db_username,$db_password, $db_database);
if(mysqli_connect_error()){
die("Database Connection Failed: ".mysqli_connect_error()." (".mysqli_connect_errno().")");
};
$query = "SELECT * from PURCHASE";
//echo $query;
$result = mysqli_query($connection,$query);
if(!$result) {
die("Database Query Failed!");
};
$distinct = "SELECT DISTINCT StoreName FROM PURCHASE";
$distinctResult = mysqli_query($connection,$distinct);
if(!$result) {
die("Database Query Failed!");
};
echo '<select name="search_string" />'."\n";
while ($row = mysqli_fetch_assoc($distinctResult)){
echo '<option value="'.$row["StoreID"].'">';
echo $row["StoreName"];
echo '</option>'."\n";
};
echo '</select>'."\n";
mysqli_free_result($result);
mysqli_close($connection);
?>
Here is the output page.
<?php
$transaction = $_REQUEST["StoreName"];
require_once 'login.php';
$connection = mysqli_connect($db_hostname, $db_username,$db_password, $db_database);
$sql = "SELECT * FROM PURCHASE WHERE StoreName LIKE '%".$transaction."%'";
$result = $connection->query($sql);
?>
Purchases Made From <?php echo $transaction ?>
<table border="2" style="width:100%">
<tr>
<th width="15%">Item Name</th>
<th width="15%">Item Price</th>
<th width="15%">Purchase Time</th>
<th width="15%">Purchase Date</th>
<th width="15%">Category</th>
<th width="15%">Rating</th>
</tr>
</table>
<?php
if($result->num_rows > 0){
// output data of each row
while($rows = $result->fetch_assoc()){ ?>
<table border="2" style="width:100%">
<tr>
<td width="15%"><?php echo $rows['ItemName']; ?></td>
<td width="15%"><?php echo $rows['ItemPrice']; ?></td>
<td width="15%"><?php echo $rows['PurchaseTime']; ?></td>
<td width="15%"><?php echo $rows['PurchaseDate']; ?></td>
<td width="15%"><?php echo $rows['Category']; ?></td>
<td width="15%"><?php echo $rows['Rating']; ?></td>
</tr>
<?php
}
}
?>
I can get regular typing search to work but I can't think of a way to search using the same method. Is it possible?