Part of my code to retrieve the stores images and the content from the SQL database (only the path is saved at the database) is as follows. I get the content displayed except the images.
My database record says the path as; C:/xampp/htdocs/bro/productLoader/uploaded_files/1377935517-IMG_0150.JPG
The image source I entered does not seems to be helping me. How do I adjust my approach to make sure the pictures are extracted from the database?
code as follows;
<?php
$j=0;
while ($rows = mysql_fetch_assoc($query))
{
?>
<tr style="height:100px; font-family: Georgia, 'Times New Roman', Times, serif;">
<td style=" width:100px;">
<img src='<?php $rows['pictures'];?>'><br><br>
</td>
<td style="text-align:justify;"><?php echo $rows['description'];?><br><br></td>
<td style="text-align:right;"><?php echo $rows['brand'];?><br><br></td>
<td style="text-align:right;"><?php echo $rows['model'];?><br><br></td>
<td style="text-align:right;"><?php echo $rows['unitprice'];?><br><br></td>
<td style="text-align:right;"><?php echo $rows['availability'];?> units available<br><br></td>
<td><input type='submit' id='buynow[]' class='buynow' name='but' value='Buy'><br><br></td>
</tr>
<?php
$j++;
}
echo "</table>";
}?>
You have to insert only relative path excluding your document root. That is if your document root is set till htdocs folder only (which is default in apache for localhost) then you have to insert image path from this document root in your case
/bro/productLoader/uploaded_files/1377935517-IMG_0150.JPG
And yes you need to echo that variable too.
Try html link first
<img src='bro/productLoader/uploaded_files/1377935517-IMG_0150.JPG' />
Ok
Try this fixed:<img src='<?php echo substr_replace($rows['pictures'], '',0, 16);?>'/>
Related
I'm in the process of building an entertainment website. It uses THREE MySQL tables; one called title, one called clips and one called dialogue.
I've been playing around all day with having my PHP fetch data from TWO of the tables (clips and dialogue) and output the content in a HTML table.
I've not had much luck and to cap it all, I was using a free host which has now reached its EP limit and although I've upgraded, I've got to wait 24 hours to try the code I've now come up with.
My question is, have I done it right? Will this collect basic information about the clip and then produce each line of the script in a new TR before going back to the start and collecting information for the next clip?
I really hope this makes sense.
I've tried researching this and have re-built my PHP from the ground up, ensuring that I annotate each section. Last time I checked, it still didn't work!
<table class='container'>";
##############################
# CLIPS QUERY & ECHOING HERE #
##############################
$clipsquery = "SELECT * FROM clips WHERE titleid=$mainurn ORDER BY clipid";
$result2 = mysqli_query($cxn, $clipsquery);
while ($row2 = mysqli_fetch_assoc($result2))
{extract ($row2);
echo "
<tr>
<td colspan='3' class='divider'></td>
</tr>
<tr>
<td class='description'>";
if ($epident == "")
{echo "";}
else
{echo "<span class='episode'>$epident</span>";}
echo "</td>
<td rowspan='2' style='text-align: right'><audio controls>
<source src='media/$clipid.mp3' type='audio/mpeg'>Your browser does not support the audio element.</audio></td>
<td rowspan='2' style='text-align: right'><a href='media/$clipid.mp3' download='$clipid.mp3'><img src='graphics/dl-icon.png' alt='Download Icon' title='Right-click here to download this clip to your computer.'></a></td>
</tr>
<tr>
<td class='description'>$clipsynopsis</td>
</tr>
<tr>
<td colspan='3'></td>
</tr>";
#################################
# DIALOGUE QUERY & ECHOING HERE #
#################################
$dialoguequery = "SELECT * FROM dialogue WHERE clipid=$clipid ORDER BY linenum";
$result3 = mysqli_query($cxn, $dialoguequery);
while ($row3 = mysqli_fetch_assoc($result3))
{extract ($row3);
echo "
<tr>
<td class='speaker'>$speaker:</td>
<td colspan='2' class='script'>$dialogue:</td>
</tr>";}}
echo "
</table>
I've got the site to work (sort of) but the formatting went wild and sometimes included clips from other sources not meant to be on the page!
There are some things I would change in your code. If you trust the variables going into your sql query then change your while loops:
while($row = $result->fetch_assoc()){
$fetchValue = $row['COLUMN_NAME'];
}
I would say you shouldn't be using extract here.
If the user is able to modify the variables going into your sql statement you should implement prepared statements. I would build a class/function and just call it when you need it.
From the HTML/CSS side its probably going to serve you better to use css div tables, especially if you are planning to make the site responsive down the line. This can convert yours
With regards to the queries, I'm not sure I understand the structure of your tables. If you want to keep playing around on your machine install a L/W/M/AMP stack depending on your operating system. See here for more information.
You only need one table where you store the data of clips.
First, you fetch the data into an array, then
you can loop through that array with foreach,
creating the table and filling it with data.
You can use an alternative syntax for foreach to make mixing PHP with HTML easier.
//Creating the array of clips
$clips = [];
$sql = "SELECT * FROM clips";
if($result = mysqli_query($db, $sql)) {
while($row = mysqli_fetch_assoc($result)) {
array_push($clips, $row);
}
}
?>
<!-- Looping through the array and creating the table -->
<table class="container">
<?php foreach ($clips as $clip): ?>
<tr>
<td colspan='3' class='divider'></td>
</tr>
<tr>
<td class='description'>
<span class='episode'><?php echo $clip["id"]; ?></span>
</td>
<td rowspan='2' style='text-align: right'>
<audio controls>
<source src='<?php echo "media/". $clip["id"]. ".mp3"; ?>' type='audio/mpeg'>
Your browser does not support the audio element.
</audio>
</td>
<td rowspan='2' style='text-align: right'>
<a href='<?php echo "media/". $clip["id"]. ".mp3"; ?>' download='<?php echo $clip["id"]. ".mp3"; ?>'>
<img src='graphics/dl-icon.png' alt='Download Icon' title='Right-click here to download this clip to your computer.'>
</a>
</td>
</tr>
<tr>
<td class='description'>
<?php echo $clip["sypnosis"]; ?>
</td>
</tr>
<tr>
<td class='speaker'>
<?php echo $clip["speaker"]; ?>
</td>
<td colspan='2' class='script'><?php echo $clip["dialogue"]; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
You need 1 MySQL table (clips) with the 4 attributes.
You should consider using PDO over mysqli, because it's a better way of dealing with the database.
Also, template engines like Smarty can help you make the code more readable and modifiable, separating the application logic from the presentation logic.
you must use JOIN in your Sql query
SELECT
clips.clipid, clips.columnname,
dilogue.id, dialogue.columnname
FROM
clips JOIN dialogue ON
clips.clipid = dialogue.id
WHERE
clipid=$clipid
ORDER BY linenum
then you can use
while($row = mysqli_fetch_assoc($result)){
echo $row['column name in clips table'];
echo $row['column name in dialogue table'];
}
but you must use Prepared statements or
mysqli_real_escape_string()
to avoid Sql Injections
also open and close PHP tags before and after html)
I have a website, where user can take screenshot of that website and that screenshot should save automaticly to folder "posters" inside my "www" folder.
But when I try to make an screenshot, it is saved directly to "www" folder and not to the "posters" folder. I tried to change atributes of the "posters" folder to 777 but It did not helped. And also, that "posters" folder gives this error: Forbidden You don't have permission to access /posters/ on this server. when I try to visit it via browser. I dont know what is wrong.
Here is my save.php code:
<?php
//Get the base-64 string from data
$filteredData = substr($_POST['img_val'], strpos($_POST['img_val'], ",") + 1);
//Decode the string
$unencodedData = base64_decode($filteredData);
//Save the image
file_put_contents('img_' . date("U") . '.png', $unencodedData);
move_uploaded_file('img.png', './posters/$newname');
?>
<h2>Save the image and show to user</h2>
<table>
<tr>
<td>
<a href="img.png" target="blank">
Click Here to See The Image Saved to Server</a>
</td>
<td align="right">
<a href="index.php">
Click Here to Go Back</a>
</td>
</tr>
<tr>
<td colspan="2">
<br />
<br />
<span>
Here is Client-sided image:
</span>
<br />
<?php
//Show the image
echo '<img src="' . $_POST['img_val'] . '" />';
?>
</td>
</tr>
</table>
<style type="text/css">
body, a, span {
font-family: Tahoma; font-size: 10pt; font-weight: bold;
}
</style>
Is it possible to make something like this? file_put_contents(.PathToFolder/'img_' . date("U") . '.png', $unencodedData);
I have this code:
<?php
$images = 'image';
$sql = "SELECT image FROM items WHERE itemsID = '4';";
$result = mysqli_query($connection, $sql) or die(mysqli_error($connection));
?>
<div id="table1" style="position:absolute; overflow:hidden; left:199px; top:139px; width:"241px"; height:"163px"; z-index:43">
<div class="wpmd">
<?php
while($rows = mysqli_fetch_assoc($result)) {
?>
<div><TABLE bgcolor="#FFFFFF" border=1 bordercolorlight="#C0C0C0" bordercolordark="#808080">
<TR valign=top>
<TD width=231 height=155><img src="images/<?php $rows[$images];?><BR>
</TD>
</TR>
</TABLE>
</DIV>
<?php
}
?>
</DIV>
</DIV>
this should be work but not
I'm putting an image get from the database then put it into table cell but I keep getting this error
If you need to display the image from the mysql which is storing the image name that you have uploaded you have to follow the steps in order to display it.
Ensure under which folder you have saved the uploaded image.
Make sure that you store only the name of the image into the database so that we can retrieve it very easy by looping it over the query.
Note: As per the question you have to retrieve the images from the database so that you need to follow the code which i have given below.
Method 1: To retrieve the image from the DB
Your code which displays the <?php $rows[$images];?> is wrong pertaining to that you need to display the name of the DB field over to there and echo the value and then alone you will be getting the image over there.
Replace:
<TD width=231 height=155><img src="images/<?php $rows[$images];?><BR>
With:
<TD width=231 height=155><img src="images/<?php echo $rows['images'];?><BR>
provided the $row['images'] where images is the field containing the name of the image stored in the DB. or else you can change the name as per the Table field what you have. Rather than that your code look good.
Hence you entire code looks like,
<?php
$images = 'image';
$sql = "SELECT image FROM items WHERE itemsID = '4';";
$result = mysqli_query($connection, $sql) or die(mysqli_error($connection));
?>
<div id="table1" style="position:absolute; overflow:hidden; left:199px; top:139px; width:"241px"; height:"163px"; z-index:43">
<div class="wpmd">
<?php
while($rows = $result->fetch_assoc()) {
?>
<div>
<TABLE bgcolor="#FFFFFF" border=1 bordercolorlight="#C0C0C0" bordercolordark="#808080">
<TR valign=top>
<TD width=231 height=155><img src="images/<?php echo $rows['images'];?><BR>
</TD>
</TR>
</TABLE>
</DIV>
<?php
}
?>
</DIV>
</DIV>
Method 2: To retrieve the image as per your Structure
You have stored the $images='image'.
Ensure that the table has the field image where it stores the image name.
If it does so you can simply change the code like this .
You are missing echo in the code so that it displays the name of the image stored in the DB and it prints that image stored under int images folder with the hep of the image name you given as input to the img tag.
Replace:
<TD width=231 height=155><img src="images/<?php $rows[$images];?><BR>
with:
<TD width=231 height=155><img src="images/<?php echo $rows[$images];?><BR>
Note: But method 2 is not advisable since if you loop through the DB for getting the value you have to follow that Method-1 alone.
I am a student and I have been given a project to do, we made a database in MySql called Video_Game_Shop. Then we had to connect it to a website using PHP and HTML. And we have to show on one page all the games in our database listed. So we did that by calling a procedure which ordered the games by price. This is where I have the problem. We now need to show images for every one of these games on the page. What I have done so far I have downloaded the pictures, for every game we have listed, and I have no idea on how to do that task. I heard of some ways of inputting the path for the images or to use BLOB file type, but I don't know how to do any of that. I will add the PHP file so you can more easily understand my situation. I would be very grateful if you could help me. :)
<!DOCTYPE html>
<html>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css"/>
<style>
h1 {font-family:Serif; font-size:22px; text-align:center}
p {font-family:Serif; font-size:16px}
.container {
width: 100%;
clear: both;
opacity: 1
}
input[type=number] {
border:2px #ffo;
opacity:0.2}
input[type=text] {
border:2px #ff0;
opacity:0.2}
input[type=date] {
border:2px #ff0;
opacity:0.2}
</style>
<h1>videogames</h1>
<head>
<title>videogames</title>
<link rel="stylesheet" href="css/table.css" type="text/css" />
</head>
<body background="http://i.imgur.com/yGkEuwZ.jpg">
<?php
require_once 'user.php';
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname",
$username, $password);
// execute the stored procedure
$sql = 'CALL List_game_by_price()';
$q = $conn->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
} catch (PDOException $pe) {
die("Error occurred:" . $pe->getMessage());
}
?>
<div class="container">
<table border="1" align="center" width="90%">
<tr>
<th>GameID</th>
<th>Name/th>
<th>Developer</th>
<th>Publisher</th>
<th>ESRB</th>
<th>Relase Date</th>
<th>Platform</th>
<th>Genre</th>
<th>Language</th>
<th>Price</th>
</tr>
<?php while ($r = $q->fetch()): ?>
<tr>
<td><?php echo $r['GameID'] ?></td>
<td><?php echo $r['Name'] ?> </td>
<td><?php echo $r['Developer'] ?></td>
<td><?php echo $r['Publisher']?></td>
<td><?php echo $r['ESRB'] ?></td>
<td><?php echo date_format ($r['Relase Date']) ?></td>
<td><?php echo $r['Platform'] ?></td>
<td><?php echo $r['Genre'] ?></td>
<td><?php echo $r ['Language'] ?></td>
<td><?php echo $r['Price'] ?></td>
</td>
</tr>
<?php endwhile; ?>
</table>
</div>
</body>
</html>
A quick solution without changing your database at all would be to upload images to the FTP and rename them for their name to match the GameID.
Example: Change your Mass-Effect2_600x400.jpg into 1.jpg.
In your fetch (where you show the data from the base) just add another something like this
<td><?php echo '<img src="path/to/'.$r['GameID'].'.jpg" />' ?></td>
You could create an assets table with cols [ID Primary Key], [Url]. Create a foreign key on your products table AssetID. This way you only have one record per image URL, and multiple products can use that image.
Upload the files to FTP and insert records into the assets table.
If you really wanted to go above and beyond your task, you could create an image upload for the products from an Admin panel. Which when uploaded inserts the path into the assets table and assigns the AssetID to the product.
Upload the images to an ftp, input your image names in a column and then call that in your query. Might be a bit of a workaround but it works well!
<td><img src="path/to/<?= $r['GameID'] ?>.jpg" /></td>
I am trying to get images out of the database using Blob, I know its not secure but it is for demo purposes. Below is the code I have at the moment.
<?php
error_reporting(0);
require './db/connect.php';
include './includes/header.php';
?>
<?php
if($result = $connection->query("SELECT * FROM Production")){
if($count = $result->num_rows){
while($row = $result->fetch_object()){
?>
<table class="productioninfo" style="width: auto; height: auto; border: 5px black solid;">
<tr>
<th>Image:</th>
<td><img src="/phpmyadmin/production/<?php echo $row->ProductionId;?>.jpeg" </td>
<th>Production Name:</th>
<td><?php echo $row->ProductionName; ?></td>
<th>Production Type:</th>
<td><?php echo $row->ProductionType; ?></td></br>
</tr>
<?php
}
$result->free();
}
}
echo $result;
include './includes/footer.php';
Also I want to know how to do it the other way, by having the file path in the database and then displaying it on the web page.
Many Thanks for your help!
An arror in this line:
<td><img src="/phpmyadmin/production/<?php echo $row->ProductionId;?>.jpeg"</td>.
Try to close correctly the img tag :
<td><img src="/phpmyadmin/production/<?php echo $row->ProductionId;?>.jpeg" /></td>
Then be sure that the image was correctly encoded and decoded respectively when storing and retrieving.
While saving an image to database, You have to encode it and then save to db. & while showing it in HTML you have to decode it.
eccoding :
$img_encoded = base64_encode('your image');
decode and show in HTML
<img src="data:image/jpg;base64,'.$img_encoded.'"/>