Appear A Picture Which User Input In Web PHP Using Database - php

Can anyone help me out? So, now I'm making a website, and one of the feature there is profile feature. In there I want user can upload their own picture, but if the value is null, then I want the picture is from the default image from mine. But I got a problem when I get the value of the pictures in database. I use a loop to determine whether the value is null or not, but all the images just appear from all the users images, not as the login user.
Here's the php code :
<?php
$arrayPictures[] = array();
$row=0;
while ($result= mysqli_fetch_object($query)) {
$arrayPictures[] = $result->pictures;
$row++;
}
for($i=0;$i<=$row;$i++)
{
if($arrayPictures[$i] == null)
{
?>
<img src="mypath\default.jpg" class="pictures" alt="">
<?php
}
else {
?>
<img class="pictures" src="mypath\<?php echo "$arrayPictures[$i]";?>" >
<?php
}
}
?>
So, what I want is if I login as 'User A', I just want to get the pictures from user A. But If User A not yet upload the pictures, I want to use the default pictures from mine.
This is the query code :
$id_account = $_SESSION['id'];
$query = mysqli_query($con, "SELECT * FROM t_akun WHERE username=$id_account");

Related

Click on an image stored in a MySQL database table and get additional row content for that image

I have created a members.php page that connects to a database table. The table has the following fields: id, username, profile image.
The following PHP code displays the profile image for each user in rows of 6 and allows each image to be clickable.
<?php
// The code below will display the current users who have created accounts with: **datemeafterdark.com**
$result=mysql_query("SELECT * FROM profile_aboutyou");
$row1 = mysql_fetch_assoc($result);
$id = $row1["id"];
$_SESSION['id'] = $id;
$profileimagepath = $row1["profileimagepath"];
$_SESSION['profileimagepath'] = $profileimagepath;
$count = 0;
while($dispImg=mysql_fetch_array($result))
{
if($count==6) //6 images per row
{
print "</tr>";
$count = 0;
}
if($count==0)
print "<tr>";
print "<td>";
?>
<center>
<img src="<?php echo $dispImg['profileimagepath'];?>" width="85px;" height="85px;">
</center>
<?php
$count++;
print "</td>";
}
if($count>0)
print "</tr>";
?>
This is all great, however, when I click on the image that loads it re-directs me to: viewmemberprofile.php which is what it is supposed to do. But it always displays the same image with the same id value (i.e.) 150 no matter which image I click. What I would like to have happened is. If I click on an image with id 155 etc... it will display content for that image data field not consistently the same image data regardless of which image I click.
Your help and guidance would be greatly appreciated. Thank you in advance.
One thing that I forgot to mention is that I do use sessions so... when I am re-directed to the viewmemberprofile.php page I use the following code to aide in getting the data that I need from the table.
<?php
$id = $_SESSION['id'];
echo($id);
?>
<?php
echo('<br>');
?>
<?php
$profileimagepath = $_SESSION['profileimagepath'];
?>
<img src="<?php echo($profileimagepath);?>" width="50px;" height="50px;">
I have yet to impliment the suggested solution.
You need to pass the ID of the row to viewmemberprofile.php, e.g.:
<a href="viewmemberprofile.php?id=<?= $dispImg['profileimagepath'] ?>">
And viewmemberprofile.php needs to select that row from the DB:
SELECT * FROM profile_aboutyou WHERE id = $_GET['id']
The above SQL statement is pseudo-code; you need to write actual code to accomplish what it is describing, preferably using parameterized queries.

How do I change my page to display different information based on how the user access it?

I have a page called profiles.php that displays the users own information when logged in. Recently I made it so the user can click on the name of someone else and it will take them to profiles.php. I want it to display the user's they clicked on profile/information, but it only shows your own information.
The way I have the page now is, it uses a session varaible based on if your logged in and from there it puts your data out in its designated places if that makes sense.
//if theuser is logged in then it turns their database id into a variable for later use
if(isset($_SESSION['userUid'])){
$current = $_SESSION['Id'];
}
//This is the link a person clicks on thats supposed to take them to that user's page. $post is a varaible from a foreach. I tried to make it so once they click it takes them to profiles.php users id (href = "profiles.php/id"'.$post["idUser"].'")
<a href = "profiles.php/id"'.$post["idUser"].'" ><h1>'.$post["UserID"].'</h1></a>
I expect the output to be taking the the current user to the desired user's profile page, but the actual output is taking the current user to their own profile page.
From what I understand you would like to show specific users profile information.
// define your connection
require_once('connection.php');
$users = mysql_query($conn, "Select id, name from users);
$row = mysql_fetch_array($users)
?>
<ul>
<?php
while($row){
echo <? <a href='profile_display.php?id=<?php $row['id'] ?> > <li> <?php $row['name'] ?> </li></a> ;
}
</ul>
in profile display.php
<?php
require_once'connection.php' ;
$id = $_GET['id'];
if(isset($id)) {
$result = mysql_query($conn, 'select * from users where id=$id);
if(!empty($result)) {
// display all value here
} else {
echo "No user profile information was found!"
}
?>
This code is not tested by me. But I would say this is the strategy needed to be used here.
Pseudocode:
List all users
User click specific other user, grab their id
make a query on db for the user information
display if available else display error information
Hope this helps!

Is it safe to use GET method for deleting information from a database?

I'm creating a website where users will need to be able to upload images, and I'd like them to be able to delete those images. Right now, I have a page that will display all of the images that that user has uploaded, and I have a php set up to delete an image from the database. It just needs to be given the id of the image. I have it functioning with the GET method, but I'm concerned a user could find the URL for my delete php and put in random ids, deleting everyone's images. Is there a way I can adjust my code to make it safer?
<?php
$sql = "SELECT id, userid, name, image FROM images";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
if ($imageUser == $row["userid"]){
echo "<tr>";
echo "<th>".$row["userid"]."</th>";
echo "<th>".$row["name"]."</th>";
echo "<th><img src='showimage.php?id=".$row["id"]."'></th>";
echo "<th><a href='imgdelete.php?id=".$row["id"]."'>delete</a></th>";
echo "</tr>";
}
}
} else {
echo "0 results";
}
?>
The delete.php simply deletes the entry WHERE id=$_GET['id'];
In a RESTful API, a GET request should never modify the data. If you want to delete items, you should use a POST or a DELETE request.

Displaying specific image and MySql information when clicked on

I am building an image uploading website. Images are uploaded to a directory on the server and data such as the filename is stored in a MySql table. I have created a 'gallery' page which displays all the uploaded images as thumbnails. When a user clicks on one of the images, it takes them to 'image.php' page, which will display the image full size and echo information such as the username of the person who uploaded the image etc.
I am unsure as to what would be the correct way of displaying the image. The images in my MySql table have unique ID's which I'm guessing will have to be manipulated in some way, but how do I would I get the ID of the photo that has been clicked on into the 'image.php' MySql query?
Hope this has been explained well enough. Thanks in advance.
gallery.php page... (exclusing database connections etc.)
//Retrieves data from MySQL
$data = mysql_query("SELECT * FROM photos");
//Puts it into an array
while($info = mysql_fetch_array( $data ))
{
?>
<section class="thumbnails group">
<?php Echo "<img src=http://.../thumbs/tn_".$info['filename'] .">"; }?>
</section>
image.php page...
//Retrieves data from MySQL
$data = mysql_query("SELECT * FROM photos WHERE 'id' = ??");
//Puts it into an array
while($info = mysql_fetch_array( $data ))
{
?>
<section class="main-image group">
<?php echo "<img src=http://.../images/".$info['filename'] .">"; }?>
</section>
You can simply pass the ID of the image through in the querystring
<img src="/path/to/thumbnail">
In your image.php page, you can retrieve the ID like this (assuming it's an integer):
$imageID = intval( $_GET["image"]);
You should then be able to retrieve the path to the image and display it.
# liquorvicar answer is correct one, there's no ambiguity in his answer! well,
try this;
<?php Echo "<img src=http://...thumbs/tn_".$info['filename'] .">"; }?>
on image.php page
$id=intval($_REQUEST['id'])
now you have id for that specific image show its related info.

PHP catalog, click on item image to display product details

I am trying to build a simple catalog displaying items from a mysql database.
So far I can get the items to display in a list format including the images stored as a path in the items table in a field called "path".
I would like the ability to be able to click on the item image and it takes you to a dedicated page showing you the details of that product. The link would correspond to which ever item you click on based on the data in the item table.
So far I have the following code;
<?php
session_start();
require "connect.php";
$date = date("d-M-Y");
$query = "select * from item order by date && time asc limit 0,3";
$query2 = "select * from users where userid = ".$_SESSION['userid'];
$result = #mysql_query($query, $connection)
or die ("Unable to perform query<br>$query");
?>
<?php
while($row= mysql_fetch_array($result))
{
?>
<?php echo $row['item'] ?>
<?php echo $row['description'] ?>
//Code for Image Link
<a href='<a href='<?php echo $row['path']?>'><img src="<?php echo $row['path']?>
<?php
}
?>
The above code allows you to click on the image and it takes me to http://127.0.0.1/steal/%3Ca%20href=
However I don't know what I would need to enter in order for it to take me to a page that shows the product?
Please Help
Many Thanks
I don't know your database schema, so I'm taking some liberties here about your columns for the product. But, try this:
<img src="<?php echo $row['path']?"/>
I also am not sure what your product page URL is either, so change product_page.php to whatever template you are using to display your products.
You need to create another page called for example showProductsOrder.php that accepts the orderID and then does output the content.
showProductsOrder.php?orderID=1
<?php
SELECT * FROM order WHERE orderID = $_GET['orderID']
while(fetch()) {
//> Show product here
}
?>
Also please don't use mysql_query. Use php.net/pdo
Addendum
Show all products
As yes123 said you would need to create another page, but i if you have a problem creating the link here is my suggested solution:
Replace your link with:
<?php echo ("<a href='".$row['pagePath']."'><img src='".$row['imagePath']."' /></a>";?>
$row['pagePath'] is the page that it will load when the image is clicked on, and
$row['imagePath'] is where the image is stored on your pc / server.

Categories