473,387 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Why is my image not uploading?

97 64KB
I have a from that brings a record from the database, including the image that is currently stored for that record. The user can then update the information for the record, but the problem is that when I pass the new image from the php page with the form to the script that processes the update of the database record, the image is not actually passing.

This is the php that brings the record from the database to the page:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include ('includes/DbCon.php');
  3. $id = intval($_GET['id']);
  4. $target_dir = $_SERVER['DOCUMENT_ROOT'].'/pc.v.2/images/photo/';
  5. $query = "SELECT * FROM news WHERE id = '$id'";
  6. $result = $mysqli->query ($query);
  7. if(mysqli_num_rows($result)>=1){
  8. $row = mysqli_fetch_array($result, MYSQLI_BOTH);
  9. $headline = $row['headline'];
  10. $body = $row['body'];
  11. $image = $row['image'];
  12. }
  13. ?>
  14.  
This is the form to display the record:

Expand|Select|Wrap|Line Numbers
  1. <form action="update.php" method="post" class="newNews">
  2. <input type="hidden" name="ud_id" value="<?=$id;?>">
  3. <!-- <input type="hidden" name="old_id" value="<?=$image;?>"> -->
  4.  
  5. <label for="title">Title</label><br />
  6. <input type="text" name="ud_headline" value="<?=$headline;?>"/><br /><br /><br />
  7.  
  8. <label for="text">Body</label><br />
  9. <textarea name="ud_body" rows="5" cols="21" value="" class="editBody"><?=$body;?></textarea><br />
  10.  
  11. <p>Current Photo</p>
  12. <img src="images/photo/<?=$image?>" alt=" " width="auto" height="auto"><br /> <!-- $target_dir. -->
  13.  
  14. <input type="file" name="ud_image" class="newsImage" ><br />
  15. <input type="submit" name="submit" value="Update news item" class='addNew' />
  16. </form>
  17.  
And this is the update.php file that processes the update:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include ('includes/DbCon.php');
  3. $target_dir = 'images/photo/'; // $_SERVER['DOCUMENT_ROOT'].'/pc.v.2/
  4. $target_file = $target_dir . basename($_FILES["ud_image"]["name"]);
  5. if (move_uploaded_file($_FILES["ud_image"]["tmp_name"], $target_file))
  6. {
  7. echo '<script type="text/javascript">';
  8. echo 'alert("News Items Saved")';
  9. echo '</script>';
  10. }
  11. else {
  12. echo "Sorry, there was an error with your file.";
  13. }
  14. if (isset($_POST['id']))$ud_id = $_POST['id'];
  15. if (isset($_POST['ud_headline']))$ud_headline = $_POST['ud_headline'];
  16. if (isset($_POST['ud_body']))$ud_body = $_POST['ud_body'];
  17. if (isset($_POST['ud_image']))$ud_image = $_POST['ud_image'];
  18. if(isset($_POST['submit'])){
  19.     $query = "UPDATE news SET headline='$ud_headline', body='$ud_body', 
  20.     image='$ud_image' WHERE id='$ud_id'";
  21.     if($mysqli->affected_rows>=1){
  22.     //$result = $mysqli->query($query);  .$_FILES[ud_image][name]
  23.     echo '<script type="text/javascript">';
  24.     echo 'document.location.href = "/pc.v.2/admin-news.php";';
  25.     echo '</script>';
  26.     }
  27.     else
  28.     {
  29.     echo '<script type="text/javascript">';
  30.     echo 'alert("The news item was not able to be created. Please try again."). mysql_error()';
  31.     echo '</script>';
  32.     }}
  33.  
  34. $mysqli->close();
  35. ?>
  36.  
Why is the image not passing from the form to the script?
Jun 18 '15 #1
12 1742
computerfox
276 100+
You're using $_POST['ud_image'], but isn't it suppose to be a $_FILE?
If this is an update, I would actually start the code by initializing the values with the current values, then when the submit button gets hit, gather the new values and check if the file is set, if not, use the current value, if it is set, use the uploaded value.

Hope that helps.
Jun 18 '15 #2
tdrsam
97 64KB
Ok, so I've changed my update.php script, the one that processes the update of the db record. This is what I have:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include ('includes/DbCon.php');
  3.  
  4. // Initialize the current values.
  5. $query = "SELECT * FROM news WHERE id = '$id'";
  6. $result = $mysqli->query ($query);//or die(mysqli_error($result->db_link));;
  7. if(mysqli_num_rows($result)>=1)
  8. {
  9. $row = mysqli_fetch_array($result, MYSQLI_BOTH);
  10.     $headline = $row['headline'];
  11.     $body = $row['body'];
  12.     $image = $row['image'];
  13. }
  14. else{
  15.     $mysqli->error;
  16. }
  17.  
  18. // When submit button gets hit
  19. if(isset($_POST['submit'])){
  20.  
  21. // Gather the new values
  22. if (isset($_POST['id']))$ud_id = $_POST['id'];
  23. if (isset($_POST['ud_headline']))$ud_headline = $_POST['ud_headline'];
  24. if (isset($_POST['ud_body']))$ud_body = $_POST['ud_body'];
  25. if (isset($_POST['ud_image']))$ud_image = $_POST['ud_image'];
  26.  
  27. // Check if the file is set
  28. $target_dir = 'images/photo/'; // $_SERVER['DOCUMENT_ROOT'].'/pc.v.2/
  29. $target_file = $target_dir . basename($_FILES["ud_image"]["name"]);
  30. if (!move_uploaded_file($_FILES["ud_image"]["tmp_name"], $target_file))
  31. {
  32.  
  33. // If not use the current value
  34. $query2 = "UPDATE news SET headline='$ud_headline', body='$ud_body', 
  35. image='$image' WHERE id='$ud_id'";
  36. $result2 = $mysqli->query($query2)or$mysqli->error;
  37. }                            
  38. else
  39. {
  40.  
  41. // If isset, use the new value
  42. $query3 = "UPDATE news SET headline='$ud_headline', body='$ud_body', 
  43. image='$ud_image' WHERE id='$ud_id'";
  44. $result3 = $mysqli->query($query3)or$mysqli->error;
  45. }
  46. }
  47. else
  48. {
  49. $mysqli->error;
  50. }
  51. echo '<script type="text/javascript">';
  52. echo 'location.replace.href="/pc.v.2/news.php")';
  53. echo '</script>';
  54. $mysqli->close();
  55. ?>
  56.  
But, unsurprisingly it's not working. I might need some help with this one as it's slightly towards the advanced side for my level of experience.
Jun 22 '15 #3
tdrsam
97 64KB
"You're using $_POST['ud_image'], but isn't it suppose to be a $_FILE?"

I use the post with 'image' in the create section of this system and it works, so I assume it would work for the update as well. Is that not necessarily true?
Jun 25 '15 #4
computerfox
276 100+
As for the update code (this isn't a script by the way), simply take the code from the create part and modify it to use update... where heading='$heading' .

First off where are you setting the value for the id here? The only thing I see is where you're trying to grab the data for something that isn't set. You should pass the identifier value to the URL and retrieve the ID by using $_GET['id'].

Normally, you use $_FILE if you're using a file input type. POST just isn't standard and might not always work.
Jun 25 '15 #5
tdrsam
97 64KB
I'll try modifying the insert statement and see if that works.

Funny you say that the value for the id isn't set. I noticed that this morning too. So, I used a var_dump to see what I was getting and fixed the code until it showed I was bringing the right id across.

I'll try again now.
Jun 25 '15 #6
computerfox
276 100+
Ehhhh.... for this query, it'll be something like update news set.... where id='$id'
Jun 25 '15 #7
tdrsam
97 64KB
Yeah, I can't even get that far now. I've tried making it so that the id is brought over first and that's working . Then I'm basically using the same script as the insert, but it's not finding the file, which is the next part of the script. I've got it set to upload the file the same way as the insert script, but I get an error message saying bool false
Jun 25 '15 #8
computerfox
276 100+
Please provide the CODE that you have now.

PS:
Script: System script like Python, Perl, Batch, PowerShell, Bash (no interactivity)
GUI: User interactivity is possible in a graphical interface.

If you're not sure, you can use "code". Just trying to educate you for when you talk about the system you're building.
Jun 25 '15 #9
tdrsam
97 64KB
Thanks. Here's the start of the code:

Expand|Select|Wrap|Line Numbers
  1. include ('includes/DbCon.php');
  2. if (isset($_POST['ud_id']))$id = $_POST['ud_id'];
  3.  
  4. //Set directory etc for image upload
  5. $target_dir = "images/photo/";
  6. $target_file = $target_dir . basename($_FILES["ud_image"]["name"]);
  7.  
  8. if (move_uploaded_file($_FILES["ud_image"]["tmp_name"], $target_file))
  9. {
  10. echo '<script type="text/javascript">';
  11. echo 'alert("News Items Saved")';
  12. echo '</script>';
  13. }
  14. else{
  15. echo "Sorry, there was an error with your file.";
  16. }
  17.  
Even that won't go.
Jun 25 '15 #10
computerfox
276 100+
I feel like I keep giving you the same code. Have you reviewed the reference guides I posted?
You can't keep depending on us to fix up a system YOU are trying to build. This is considered consulting work which in other cases you'd have to pay for. You also forgot to clean up the code. How are you navigating to the update page? Can I please see the hyperlink?

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  include ('includes/DbCon.php');
  3.  $id=$_GET['id']; //Pass the id as a paramater in the url   ....php?id=
  4.  $getter=mysql_query("select* from news where id='$id'");
  5.  $d=mysql_fetch_assoc($getter);
  6.  $image_path=$d['image'];
  7.  $target_dir = "http://bytes.com/images/photo/";
  8.  $target_file = $target_dir . basename($_FILES["ud_image"]["name"]);
  9.  if(move_uploaded_file($_FILES["ud_image"]["tmp_name"], $target_file)){
  10.   $image_path=$target_file;
  11.   $succ=mysql_query("update news set image='$image_path' where id='$id'");
  12.   if($succ){
  13.    ?><script>alert("Image updated...");</script><?php
  14.   }
  15.  }
  16.  else{
  17.   print "Sorry, there was an error with your file.";
  18.  }
  19. ?>
  20.  
References:
https://www.cs.cmu.edu/~fp/courses/l...res/lp-all.pdf
https://en.wikipedia.org/wiki/Logic_programming
http://www.cs.rice.edu/~vardi/comp409/
Jun 25 '15 #11
tdrsam
97 64KB
I can understand your frustration. Trust me when I say that no one has been more frustrated by this than me.

I can now happily report that the whole system is up and running. The problem I had was that I hadn't set the enctype on the form that posted to the update page. So sorry to have caused you this frustration over such a little mistake.
Jun 26 '15 #12
computerfox
276 100+
Yup that would also be the cause. That part is on me since I didn't catch it either, but I'm glad now that we took care of the rest of the code and the system is now functional. Sorry for being so rough with you I just wanted to make sure you had an awesome product that you can be proud of to talk about.
Jun 26 '15 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Tom | last post by:
Hi, I created a webform with asp.net and c#. I need to upload image to server and record the path to that image in DB. Any sample code for image uploading? Thanks for help Tom
2
by: raj chahal | last post by:
Hi there I want to let users upload a image to their directory and then update my database so whenever someone searches the database an image is displayed.. I've seen some scripts that use...
5
by: Deejam | last post by:
hi, i've this situations where user will upload images..my code works fine for uploading images.. but i need to create auto thumbnail picture (of the same image) whenever user upload images......
2
by: pdesai007 | last post by:
hi all, i am trying to upload a image on to server with asp and AJAX, but it is not worked. so please give me information that upload an image to the server with java script without...
2
kamill
by: kamill | last post by:
Dear All, I am uploading a image file and after resize the image i am saving it. My script is working fine with small size images but when i am trying to upload big size image, i am getting below...
4
pradeepjain
by: pradeepjain | last post by:
Hii guys, I have a small requirement which before starting i want to be clear of how to go above it. Since javascript will not work for image preview i need to write a php code so that...
4
by: brettokumar | last post by:
hi i my form im using ifram tag if i click a button from my form my ifram from will be open and im uploading a image the image is uploaded and it is diosplay in ifram form but the image can't...
3
Raju B
by: Raju B | last post by:
hi friends i had written a code for uploading image.but it showing error like failed to open stream on line 61.please go through my code and suggest me. <?php //include 'include/dbconnect.php';...
2
by: sam thush | last post by:
In my web site there are 5 image uploading sections. I want to validate them by size,width,height and type.can any one help me to cover this task by PHP.
0
by: dharmeshjohn | last post by:
I am trying to upload a image into a folder using classic asp. I am able to upload image into a folder but not getting the values of other input fields. <%@ Language="VBScript" %> <form...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.