Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

file input update mysql when selected

Posted on 2011-10-10
6
Medium Priority
?
295 Views
Last Modified: 2012-05-12
I am selecting an image filename from a mysql table and displaying the image on the page, as well as displaying a file input form field where the displayed image can be updated with a new image, like so:

Here is the current image:
<img src="<?php  echo $_SESSION['image']; ?>"></img>

Open in new window


Select a new image:
 <input type="file" name="image" value="Select Image">

Open in new window


Upload the new image:
 <input type="submit" value="Submit"> 

Open in new window


The image is updated in the mysql table like so:

 $target = "images/"; 
 $target = $target . basename( $_FILES['image']['name']); 

 $picture = htmlentities(($_FILES['image']['name'])); 

mysql_query("UPDATE users SET image = '" . mysql_real_escape_string($image) . "'
WHERE id = '" . mysql_real_escape_string($_SESSION['userid']) . "' ");

Open in new window


What I want to achieve is: if a file input is selected, then the image field is updated in the mysql table. If no file input is selected, then the mysql field is not updated, leaving the current image intact.

What is the best way to do this?
0
Comment
Question by:razn8
  • 3
  • 2
6 Comments
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 36946563
You may use AJAX way to upload on the clientside process.

Ref. http://www.freshdesignweb.com/10example-ajax-upload-file-with-php.html
0
 

Author Comment

by:razn8
ID: 36948583
Thanks, I'm not using AJAX or Flash for this project. HTML, PHP and MySQL only.
0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 36953523
What I want to achieve is: if a file input is selected, then the image field is updated in the mysql table

What do you mean, you need to handle as event OR just click the button after input field selected ??

You can do this with AJAX
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36954206
Is there a little fifty-point question here that we can answer, or is this a need for application development?

You can test the $_FILES array to see if an image was uploaded.  See the PHP.net web site in the part about POST-method file uploads.
0
 

Accepted Solution

by:
razn8 earned 0 total points
ID: 36976893
I appreciate your responses. Neither answers helped me, so I figured it out on my own, as described below:

I am loading a user image from the database via a session. Then, below the displayed image is a file upload form from which the user can update the image currently stored in the database. There are also other form fields that will be updated along with the image.

<form action"confirm.php">
<p>  
Edit your name: <input name="name" type"text" value="<?php  echo $_SESSION['name']; ?>"><br/>
Current Picture: <img src="<?php  echo $_SESSION['picture']; ?>"><br/>
Update Picture: <input type="file"  name="newpicture" /><br/>
<input name="Submit" type="submit" value="Submit">
</p>
</form>

Open in new window


On confirm.php I display the data and image so the user can confirm prior to updating.
If a new image was uploaded, then '$newpicture' displays. If a new image was not uploaded then '$_SESSION['picture']' displays.
The database image entry has the full url, however the updloaded image has a relative url. So I define the full url with $pictureurl variable. Whichever image is displayed needs to be updated in the database upon the confirmation submit. I define this with the $updatepicture variable.

Form confirmation page:
$uploaddir = "pictures/";
$path = $uploaddir.$HTTP_POST_FILES['newpicture']['name'];
$picturedir = 'http://finalimage.url/pictures/';
echo $name;

if($newpicture != none){ 

    if(copy($HTTP_POST_FILES['newpicture']['tmp_name'], $path)){ 

        $theFileName = $HTTP_POST_FILES['newpicture']['name'];
		
$pictureurl = $picturedir.$theFileName;

echo "<img width=200 src='".$pictureurl ."'>";
$updatepicture = $pictureurl;

    } else {

echo "<img width=200 src='".$_SESSION['picture'] ."'>";
$updatepicture = $_SESSION['picture'];

    }
}

<form action"update.php">
<p>  
<input name="name" type="hidden" value="<?php echo $name ?>">
<input name="updatepicture" type="hidden" value="<?php echo $updatepicture ?>">
<input name="Submit" type="submit" value="Submit">
</form>
</p>

Open in new window


The update.php page is a basic mysql update:
mysql_query("UPDATE users SET fieldname = '$_POST['formfield']'
WHERE id = $loginid ");

Open in new window


I posted this here so that someone else with the same question can see how it was solved. Feel free to add your comments to improve my solution. I removed file cleansing to reduce the amount of code displayed here, so add that back in before using this code live.
0
 

Author Closing Comment

by:razn8
ID: 37005122
I appreciate both logudotcom and Ray_Paseur taking the time to post comments. However, their answers did not provide the specific information I needed to solve this question. Therefore, I spent the time to figure it out on my own.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question