I have made a photo gallery, which shows photos from MySQLi database, but when I click on the link on each photo, it must go to a single page that only shows that photo.
Index.php
$query = mysqli_query($conn, "SELECT * FROM images");
while($row = mysqli_fetch_array($query)) {
$id = $row['id'];
$image = $row['image'];
echo "<td><a href='single.php?id=$id'><img src='$image'></a></td>";
}
Single.php
$id = $row['id'];
$query = mysqli_query($conn, "SELECT * FROM images WHERE id='$id'");
<?php
while($row = mysqli_fetch_array($query)) {
$image = $row['image'];
echo "<img src='$image'>";
}
Single.php change first line to
$id = intval($_GET['id']);
Related
I have a if statement that goes through all of the title options and all of them are a link to the same page. The idea is that depending on which title option is clicked on the linked page will show the title in h3 tags.
$sql = "SELECT * FROM posts";
$result = mysqli_query($conn, $sql);
$queryResults = mysqli_num_rows($result);
if ($queryResults > 0) {
while ($row = mysqli_fetch_assoc($result)){
$item =$row['title'];
echo "<div>
<a href='p&fproject.php' name='project'>
<p>".$item."</p>
</a>
</div>";
}
}
}
if(isset($_POST['project'])){
$id = (isset($_POST['id'])) ? intval($_POST['id']): null;
echo "<br>".$id;
}
?>
The last if statement was an attempt to identify the link that was clicked on by id instead of title but it doesn't work.
Page linked to:
<?php
include 'header1.php';
$sql = "SELECT * FROM posts";
$result = mysqli_query($conn, $sql);
$queryResults = mysqli_num_rows($result);
if ($queryResults > 0) {
while ($row = mysqli_fetch_assoc($result)){
for ($i = 0; $i < $queryResults; $i++) {
$item = $row['title'];
echo "<div>
<a href='' name='project'>
<p>".$item."</p>
</a>
</div>";
}
}
}
if(isset($_POST['project'])){
$id = (isset($_POST['id'])) ? intval($_POST['id']): null;
echo "<br>".$id;
}
?>
You can easily do that passing the post id in GET. What you want to do is put in the href your link project.php followed by a question mark with the id of the post, like this: 'project.php?id='" . $row["id"] . ", and then, in the page linked to, you can access that variable by doing postId = $_GET["id"] and making an SQL query on that id
I have created a code to delete image which is in a folder and to update image_path to null in the database. Although the image deletes the image, the path does not get updated to null. I spent hours to catch my mistake. But I could not. Any help would be grateful!
This is my code
<?php
//this is were images displayed
$sql = "SELECT * FROM services WHERE user_name='wendi'";
$result = $con->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
?>
<img src="images/template/delete.png" id="AEDbutton">delete
<?php
echo "<img border=\"0\" src=\"" . $row['image_path4'] . "\" width=\"200\" height=\"100\">";
echo "<br>";
}
}
?>
this is delete.php
<?php
include('config.php');
$sql = "SELECT * FROM services WHERE user_name='wendi'";
$result = $con->query($sql);
while ($row = $result->fetch_assoc()) {
$image = $row['image_path4'];
unlink($image);
}
$sql = "UPDATE image_path4=null, file_name4=null FROM services WHERE user_name='wendi'";
$result = $con->query($sql);
?>
It seems your UPDATE query is malformed, the syntax should be like this:
UPDATE {table} SET {column}={value} WHERE {column2}={value2}
So in your case this would be:
UPDATE services SET image_path4=null, file_name4=null WHERE user_name='wendi'
Your query should be like this:
$sql = "UPDATE services SET image_path4 = null, file_name4 = null WHERE user_name = 'wendi' ";
After uplink function assign $image to null:
$image = null;
And use update query below:
$sql = "UPDATE `services ` SET `image_path4` = ' ". $image." ', `file_name4` = ' ". $image." ' WHERE `user_name` = 'wendi' ";
Having a problem uploading a picture from a database, wondering if i'm doing something wrong..
I've checked that the image is there by using <img scr='/upload/imgpath.jpg'>
Any suggestions?
$sql1 = "SELECT pic, stocknr, status FROM stock WHERE status = '1'";
$result = mysqli_query($con,$sql1);
while($row1 = mysqli_fetch_assoc($result));
{
$stocknr2 = $row1['stocknr'];
$picname = $row1['pic'];
$upfile = $picname;
echo "<center>";
echo "<a href='details.php?stocknr=$stocknr2'>";
echo "<img src='/upload/$upfile' border=1 width=350 height=280></a>";
echo "</center>";
}
mysqli_free_result($result);
You just need to change while row;
while($row1 = mysqli_fetch_assoc($result)
when i run this code i get only one image and returns "mysqli_fetch_array() expects parameter 1 error", I want multiple images in my page.
Here is my code,
<?php
$con=mysqli_connect("localhost","root","","education");
$result = mysqli_query($con,"Select * from ep_posts where id > '4' ");
$sql = "Select * from ep_posts where image<>'' order by ID ASC ";
while ($row = mysqli_fetch_array($result)) {
$name = $row['post_title'];
$id = $row['ID'];
$des = $row['des'];
$des = substr($des, 0,35);
$link = $siteurl."?p=".$id;
$sth = $con->query($sql);
$result=mysqli_fetch_array($sth);
$image = '<img src="data:image/jpg;base64,'.base64_encode( $result['image'] ).'" height="150" width="150" >';
}
?>
help needed
Does this help.
Remove the unnecessary second query and anything that was using it. Then just change the image tag to use data from the first query result.
<?php
$con=mysqli_connect("localhost","root","","education");
$result = mysqli_query($con,"Select * from ep_posts where id > '4' ");
// surely this is unnecessary
//$sql = "Select * from ep_posts where image<>'' order by ID ASC ";
while ($row = mysqli_fetch_array($result)) {
$name = $row['post_title'];
$id = $row['ID'];
$des = $row['des'];
$des = substr($des, 0,35);
$link = $siteurl."?p=".$id;
// so these are unnecessary
//$sth = $con->query($sql);
//$result=mysqli_fetch_array($sth);
// so this need to use a column from first query result
// so change $result['image'] to $row['image']
$image = '<img src="data:image/jpg;base64,'.base64_encode( $row['image'] ).'" height="150" width="150" >';
}
?>
Hello pal's i need some help. I wanted to apply lightbox jquery effect on image gallery php file. I'm retrieving images from database which were stored in longblob format. i can get that effect by giving id value manually but by giving the variable like $id it's not providing that lightbox effect.... please check the below code and give some suggestions.
<div id="gallery">
<?php
$query = mysql_query("SELECT * FROM image_uploads") or die(mysql_error());
while($row = mysql_fetch_array($query)){
$id = $row['entry_id'];
?>
<ul>
<li>
<a href="img_retrieve.php?oid=$id" $('#gallery').lightBox();">
<img src="img_retrieve.php?sid=$id" width="72" height="72" alt="" />
</a>
</li>
</ul>
<?php
}
?>
</div>
in retrieve page follows
if(isset($_GET['sid'])){
$id = $_GET['sid'];
$run = mysql_query("SELECT * FROM image_uploads WHERE entry_id=$id") or die(mysql_error());
while( $images = mysql_fetch_array($run) ){
$image = $images['s_image'];
header("Content-type: image/jpeg");
echo $image;
}
}
if(isset($_GET['oid'])){
$id = $_GET['oid'];
$run = mysql_query("SELECT * FROM upload_images WHERE entry_id=$id") or die(mysql_error());
while( $images = mysql_fetch_array($run) )
$s_image = $images['s_image'];
header("Content-type: image/jpeg");
echo $s_image;
}
}
if(isset($_GET['sid'])){
$id = $_GET['sid'];
$run = mysql_query("SELECT * FROM image_uploads WHERE entry_id=$id") or die(mysql_error());
while( $images = mysql_fetch_array($run) ){
$image = $images['s_image'];
header("Content-type: image/jpeg");
echo $image;
}
}
if(isset($_GET['oid'])){
$id = $_GET['oid'];
$run = mysql_query("SELECT * FROM upload_images WHERE entry_id=$id") or die(mysql_error());
while( $images = mysql_fetch_array($run) )
$s_image = $images['s_image'];
header("Content-type: image/jpeg");
echo $s_image;
}
}