This image shows the products table that I am using to upload my image from the back-end side and I wanted to display it on the front-end page.
I realize that the picture that I have uploaded through the back end side only saved as '48 B' but when I was uploading it through MySQL manually it saved as '64 KiB', although both are the same file.
When it comes to displaying the 48 B Image file would not show.
[code1] This is the code that I am using to insert the image from the back-end.
if(isset($_POST['submit'])){
$pid = mysql_real_escape_string($_POST['productID']);
$productName = mysql_real_escape_string($_POST['productName']);
$productImg = mysql_real_escape_string($_POST['productImg']);
$insert = mysql_query("INSERT INTO products (productID, productName, productImg) VALUES ('$pid', '$productName', '$productImg')");
if($insert)
{
echo 'Successfully added new record.';
} else {
echo 'Failed to add new record because '.mysql_error();
}
}
?>
[code2] and this is the code that I am using to display the image from the front-end.
while($row = mysql_fetch_array($query)){
echo '<tr>
<td>'.$row['productID'].'</td>
<td>'.$row['productName'].'</td>
<td>'.
'<img src="data:image/jpg;base64,'.base64_encode($row['productImg']).'" width="auto" height="150" class="img-thumnail"/>'
.'</td>
I think the problem is in the insertion code [code1] even though all the data successfully inserted.
if you are using <input type="file" /> and if you are not aware this that "you cannot send file type like normal string" check that what you are doing before submit.
Refer this link it may help you:
https://www.codexworld.com/store-retrieve-image-from-database-mysql-php/
Related
I am trying to query the image file names from the database using the Id inputted by the user and want to display the images with the resulting file names form the select statement. However the echo function is showing only the broken image icons instead of the image itself. How can I fix this? Thanks!
//$searchValue is the Id inputted by the user in the html page
$result = mysqli_query($con,"SELECT FileName FROM images WHERE Id='$searchValue'");
//$folder is the path where the images are currently stored
$folder = "C:/xampp/htdocs/images/";
while($row = mysqli_fetch_array($result)){
$file = $row["FileName"];
echo '<img src="'.$folder.$file.'"><br />';
}
I have a system where you'll click a link to insert two pieces of data into a table. There are multiple images with links next to them.
So basically:
1) User logs in, images are retrieved from the database as blobs.
2) Clicks a "use coupon" button
3) Code uses a JOIN query to add the coupon ID and current user ID to a database.
4) Code will display all coupon images that haven't been used.
If I manually add the coupon ID and User ID to the database, it works perfectly fine and only displays the unused coupons. However, I have two issues. Only the last images button is working. If I have 3 images listed, only the button on the 3rd image works. On top of that, the button is removing all images, not just the single image corresponding to the $row['id'].
<?php
$coupons = mysqli_query($link,
"SELECT coupons.*
FROM coupons
LEFT JOIN deleted_coupons ON
deleted_coupons.coupon_id = coupons.id AND
deleted_coupons.user_id = {$_SESSION['act']}
WHERE deleted_coupons.coupon_id IS NULL"
);
if (isset($_GET['del_task'])) {
$sql = "INSERT INTO deleted_coupons (coupon_id, user_id) VALUES ('{$row['id']}', '{$_SESSION['act']}')";
mysqli_query($link, $sql);
}
while ($row = mysqli_fetch_array($coupons)) {
?>
<tr>
<td><?php echo $row['id'] ?>
<td class="task">
<?php
echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['name'] ).'"/>';
?>
</td>
<td class="delete">
Use Coupon
</td>
</tr>
<?php
}
?>
I have a feeling it has something to do with the way my insert query is inside the loop but I can't figure out what exactly is wrong. Any help would be appreciated!
I have a form that allows a user to input a contraction (e.g. Larry's dogs) into a mysql table> The code is as follows:
<?php
<?php
if(isset($_POST['submit'])){
$title = mysqli_real_escape_string($conn,$_POST['title']);
$insertImageQ = "INSERT INTO images (title, name, keyword1, keyword2, keyword3, enter_date) VALUES('$title', '$name', '$keyword1','$keyword2','$keyword3',now())";
if(!mysqli_query($conn, $insertImageQ)){
die('error inserting new image');
}
$newImage = "one new image added";
}
}
?>
When I echo the field "title" on a webpage, it works fine unless it is echoed in an image title attribute. For example,
<?php
$slideShowQ = "SELECT * FROM slideshow";
$result= mysqli_query($connection, $slideShowQ) or die("error getting connected");
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
echo "<img class='lgImg'";
echo "id='";
echo $row['Id'];
echo "'";
echo "title='";
echo htmlentities($row['title']);
echo "'";
echo "alt='";
echo htmlentities($row['title']};
echo "'";
echo "src='_images/";
echo $row['name'];
echo "'/>";
}
?>
<?php
//release returned data
mysqli_free_result($result);
?>
In a javascript file, I call a function that on mouseover the image, a div id="topLeft" has it's html set to the title. For example,
$('.lgImg').mouseover(function(){
var title =$(this).attr('title');
$('#topLeft').html(title);
When there is a contraction in the title field, everything from the apostrophe is cut off.
Also, on another page, the title field is included in a fancybox application where the images are drawn from the mysql table. The title field appears as it should in a fancybox application unless there is a contraction in the title, in which case, the image will not even pop up as it should in a fancybox application.
Also, if I update the same image with a contraction with this code,
<?php
if(isset($_POST['submit'])){
$title= mysqli_real_escape_string($conn,$_POST['title']);
$updateQ = "UPDATE images SET title='$title'";
if(!mysqli_query($conn, $updateQ)){
die('error inserting');
}
}
?>
the field in the table goes blank and the fancybox does not work.
In sum, I can insert a contraction and echo that contraction as long as I don't echo it in an image title attribute, but I cannot update the field with a contraction.
Can anyone please help me to understand this contradiction?
htmlentities() will leave single quotes by default and as you are using them in your html attributes this will cause invalid html.
You can enforce encoding of single quotes like this:
htmlentities("'", ENT_QUOTES);
More info at the manual page
So I'm trying to display images from a database for a website that I'm building for a friend. He wants to be able to upload an image, and have it display on the front page.
I have the image uploading all working. Saving it in the database as a BLOB.
I began working on displaying the images on a separate file. I got it working on my test website, images displayed how he wanted them. But then I moved the code over to the actual website, got it to how it needed to be and it didn't work.
I ended up trying it again on my test website, and it all worked fine.
For some, it isn't working on the actual website, but it's working fine on my test website.
The actual website grabs more information the my test website does.
Here is the code the I've used on my test website.
image.php
$query = mysql_query("SELECT * FROM images LIMIT 1");
while($row = mysql_fetch_assoc($query)){
$image_id = $row['id'];
echo "<img src=showimage.php?id=".$image_id.">";
}
Here is how I'm grabbing the image from the database and displaying it.
showimage.php
include 'inc/db.php';
$id = addslashes($_REQUEST['id']);
$image = mysql_query("SELECT image FROM sites WHERE id = '$id'");
$image = mysql_fetch_assoc($image);
$image = $image['image'];
header("Content-type: image/png");
echo $image;
Using that code on the test website works fine, there's no problem there.
Here is my code for the actual website.
sites.php
$select = mysql_query("SELECT * FROM sites");
while($row = mysql_fetch_assoc($select)){
$image_id = $row['id'];
echo '
<tr class="bottom"><td>'.$row['host'].'</td>
<td>'.$row['currency'].''.$row['price'].' every '.$row['payment'].'</td>
<td>'.$row['domain'].'</td>
<td>'.$row['paid_currency'].''.$row['paid_domain'].'</td>
<td>'.$row['features'].'</td>
<td>
<span title="Edit '.$row['id'].'"><img src="images/edit.png" alt="Edit"></span>
<span title="Delete '.$row['id'].'"><img src="images/delete.png" alt="Delete"></span>
</td>
<td><img src=showimage.php?id='.$image_id.'></td></tr>
';
}
That using the showimage.php file to grab the image from the database, but isn't working at all.
If you're set on using the database, look into using the imagecreatefrompng() function, depending on how the img was put into the DB.
Also, instead of addslashes(), try doing $id = (int)$_GET['id']; then checking if $id > 0. Finally, +1 on storing images on the filesystem and not as a BLOB.
[And insert mysql_query-is-deprecated-use-the-PDO-extension lecture here]
I'm trying to create an e-commerce site which sells PC components using PHP and MySQL.
I'm using a database which has a table called 'components' which stores PC components with fields like 'name', 'type' etc. I have a field called 'image' as I was told it is better to store images in the file system rather than as a BLOB. The field contains the name of the image for that component (e.g. for Intel i7 2700k processor the image filed has inteli72700k/jpg). The image will be stored in '../images/products/'.
Now i'm trying to make my product list (i.e. a user clicked on 'Processors' and I am now trying to show the list of products we have, similar to overclockers.co.uk). I'm creating a table where the table heading is the name of the product and there is one row per product.
<table id="products">
<?php
//Connects to the DB, localhost is yourpc, root is the username and there is no password
$con = mysql_connect('localhost','root','');
//This is the database we want to access in phpMyAdmin
mysql_select_db('pctweeks');
$query = "SELECT * FROM component WHERE type = 'CPU'";
$query_run = mysql_query($query) or die(mysql_error($con));
if (mysql_num_rows($query_run) >= 1){
while($row = mysql_fetch_assoc($query_run)) {
echo "<th>{$row['name']}</th>" .
"<tr><td>{$row['image']}</td></tr>";
}
} else
{
echo 'No Products';
}
?>
</table>
The problem is the {$row['image']} part. I know I have to use an image tag but I don't know how to insert the directory of the pictures before the $row['image'].
Cant you use the code like here:
echo "<th>{$row['name']}</th>" .
"<tr><td> <img src='../images/products/{$row['image']}'/> </td></tr>";
You could try this:
define( 'IMG_PATH', 'PATH_OF_YOUR_IMAGE_DIRECTORY_HERE' );
while($row = mysql_fetch_assoc($query_run)) {
echo "<th>{$row['name']}</th>" .
"<tr><td><img src='".IMG_PATH."'{$row['image']}</td></tr>";
}
Replace "PATH_OF_YOUR_IMAGE_DIRECTORY_HERE" with your actual image folder path.
Hope this helps..