Profile photo upload in php and database - php

I have problem that the photo uploaded to the site but the name of it don't be saved on database .
The code upload the photo to the site but the name of the file didn't be uploaded to the database
help me, I need to know what the problem in the code?
please someone answer!
the code:
<?php session_start();
$con = mysqli_connect("my host","my account","my passwod","my table name");
$_SESSION['id'] = "$con_id";
?>
<?php
if(isset($_POST['submit'])){
move_uploaded_file($_FILES['file']['tmp_name'],"../userstorage/p_photos/".$_FILES['file']['name']);
$con = mysqli_connect("my host","my account","my password","my table name");
$q = mysqli_query($con,"UPDATE users SET image = '".$_FILES['file']['name']."' WHERE id = '".$_SESSION['id']."'");
}
?>

Your error is here
$q = mysqli_query($con,"UPDATE users SET image = '".$_FILES['file']['tmp_name']."' WHERE id = '$id'");
$_FILES['file']['tmp_name'] is the image data, while $_FILES['file']['name'] is the name of the file. So in the end of the day you need to change this piece of code
for reference check this article on W3Schools on how to upload and display images from a database.

Related

Upload Image won't upload unless I add a specific line of code that I don't want

My code wont work unless I add this specific line of code, the one that has the comment next to it (Line 3). Does anyone have any open suggestions on what I should do? Because I don't wont $_SESSION['username'] = "nameofuser";
:
Here is the link to the code: sweettune.info/code.txt
Using "$_SESSION['username'] = "nameofuser";" and deleting it, but if I delete it my image won't upload.
<?php
session_start();
$_SESSION['username'] = "nameofuser"; // Won't work unless this line of code is added
if(isset($_POST['submit'])){
move_uploaded_file($_FILES['file']['tmp_name'],"uploads/".$_FILES['file']['name']);
$conn = mysqli_connect("localhost","newkit","frtysk489","configurenow");
$q = mysqli_query($conn,"UPDATE users SET image = '".$_FILES['file']['name']."' WHERE username = '".$_SESSION['username']."'");
}
?>
If you don't want to use $_SESSION['username'] = 'nameofuser'; then you will need to choose another unique column from your table (such as the record id) to identify the row to be updated.
The reason the database is not updated is because your query specifically asks WHERE username = '".$_SESSION['username']."'"
"UPDATE users SET image = '".$_FILES['file']['name']."' WHERE username = '".$_SESSION['username']."'"
Without a unique identifier, the table row cannot be updated, your update will fail.

Displaying image from path returns about:Blank

I have a bunch of jpg images in a folder that I need to call up one at a time. The user looks up a number, the number pulls up a filename from a database, and the filename is appended to a path to make a valid image path. That path is then placed in the src tag of an html element. The problem: nothing shows up except the little 16x16 image error thing. When I open it in a new tab, it says "about:Blank". I've echoed the path on its own to make sure it's showing up properly, and I've tried calling up the image in an independent html element with the full path placed in the src tag. What's going on here?
<?php //Location
function Location($JobID) {
$db = mysqli_connect('localhost','root','******','testdb1')
or die('Error connecting to MySQL server.');
$JobID = $_POST['JobID'];
$getinfo = "SELECT Location FROM Jobs WHERE JobID = '$JobID'";
$query = mysqli_query($db, $getinfo);
$row = mysqli_fetch_array($query);
$Location = $row['Location'];
echo "<img src=C:/wamp64/www/Floormaps/".$Location.">";
}
?>
If you're doing this on a web page that resides in the root of your web directory try changing your image tag as shown below. The web server only needs to know where the image is in relationship to the page where the source tag is.
<?php //Location
function Location($JobID) {
$db = mysqli_connect('localhost','root','******','testdb1')
or die('Error connecting to MySQL server.');
$JobID = $_POST['JobID'];
$getinfo = "SELECT Location FROM Jobs WHERE JobID = '$JobID'";
$query = mysqli_query($db, $getinfo);
$row = mysqli_fetch_array($query);
$Location = $row['Location'];
echo "<img src='Floormap/$Location'>";
}
?>

change profle pic logged in user php

I am creating a website with user register and login system. Users can give several options for in the database by registrering. But now I add a option to upload your profile pic. But I can only change that one thats mentioned in the script and not the user thats logged in. Can someone help me?
<?PHP
require_once("./include/fg_membersite.php");
require_once("./include/membersite_config.php");
if(!$fgmembersite->CheckLogin())
{
$fgmembersite->RedirectToURL("login.php");
exit;
}
?>
<?php session_start();
$_SESSION['username'] = "ademd";
?>
<?php
if(isset($_POST['submit'])){
move_uploaded_file($_FILES['file']['tmp_name'],"images/".$_FILES['file']['name']);
$con = mysqli_connect("voetbaltransfermarkt.nl.mysql","voetbaltransfer","","voetbaltransfer");
$q = mysqli_query($con, "UPDATE fgusers3 SET image = '".$_FILES['file']['name']."' WHERE username = '".$_SESSION['username']."'");
}
?>
You should use a link to profile picture in your database table to display profile picture and then in my profile page put an UPDATE query of mysql for updating link to that uploaded picture.

How to destroy the current image in image upload

I want to do is when a user decided to change his profile picture the link in the database will be update and move the image to upload folder and destroy the current link and image on the upload folder on that specific user.
My problem in my code is when a user change his profile picture it will add another picture on the upload folder. I want to do is to delete the current image first on that specific user before the new image he selected is move on the upload folder.
php code
<?php
include_once('../dbc/database.php');
$db = new Connection();
$db = $db->dbConnect();
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$email = isset($_POST['email']) ? $_POST['email'] : "";
$image = addslashes(file_get_contents($_FILES['imageInput']['tmp_name']));
$image_name = addslashes($_FILES['imageInput']['name']);
$image_size = getimagesize($_FILES['imageInput']['tmp_name']);
move_uploaded_file($_FILES["imageInput"]["tmp_name"], "../upload/" . $_FILES["imageInput"]["name"]);
$location = "elogFiles/upload/" . $_FILES["imageInput"]["name"];
$qOldpic = "SELECT user_image FROM tbl_user WHERE user_email = : email";
$queryOldpic = $db->prepare($qOldpic);
$queryOldpic->bindParam(':email', $email);
$queryOldpic->execute();
$num_rows = $queryOldpic->rowCount();
if ($num_rows == 1) {
unlink($queryOldpic);
$q = "UPDATE tbl_user SET user_image = '$location' WHERE user_email= :email ";
$query = $db->prepare($q);
$query->bindParam(':email', $email);
$results = $query->execute();
echo "1";
}
?>
http://php.net/manual/en/function.unlink.php
above url will help you understand proper approach to delete the previous image file.
If you want to make the error message invisible when that file is not exist on given location, use '#' so you could simply ignore the error display as the following:
<?php
unlink($filename);
?>
Before you do your update, run a select and get the user_image by email, store it into a variable, like $oldPicture.
After that, you should remove the picture like this:
if ((!!$oldPicture) && (file_exists($oldPicture))) {
unlink($oldPicture);
}
You only delete an image if you have a value for its path and the file exists. Make sure the path of $oldPicture is correct. Finally, after the if above you can run your update command. I will not delve into security problems, as it is out of scope in this question, but make sure you prevent SQL injection.

backup mysql column is value with php post

I trying create a simple profile edit page. Don't care syntax, I've refined
<?php
$resultMember = mysql_query("SELECT * FROM member WHERE email='".$_SESSION['memberEmail']."'");
..
$oldProfilePhoto = $resultMember['pp'];
..
{ //post controll
$W = " WHERE email='".$_SESSION['memberEmail']."' AND pasw='".$_SESSION['memberPsw']."'";
if(!isset($_FILES['profilePhoto']['value'])){
mysql_query("UPDATE member SET pp='".$oldProfilePhoto."'".$W) or die(mysql_error());
}
$profilePhoto = "inc/img/".$_SESSION['memberSkype']."/".$_FILES['profilePhoto']['name'];
move_uploaded_file($_FILES['profilePhoto']['tmp_name'],$profilePhoto);
mysql_query("UPDATE uye SET pp='".$profilePhoto."'".$W) or die(mysql_error());
}
..
?>
So, Image upload (OK), oldPhotoName get (OK), user select photo sent by post (OK), in short everthing (OK) but first changed profile photo after try change profile photos is return to empty. Not added oldphoto,
If the user chooses photos, old photo is not changed.
Thank you for your interest.
Good works..

Categories