PHP
--
Questions
--
Followers
Top Experts
I am trying to upload an image to a field in a table. The field is called photo. I do not want to rename it or anything else. Just a direct upload from a folder on the computer. The folder is as follows. assets/artists/
move_uploaded_file( '../assets/artists/'.'photo' );
$statement = $pdo->prepare("INSERT INTO tbl_partist (p_first_name,
p_last_name,
post_slug,
post_date,
photo,
p_bio,
p_category_id,
p_type_id,
p_commish_id,
p_is_active
) VALUES (?,?,?,?,?,?,?,?,?)");
$statement->execute(array($_POST['p_first_name'],
$_POST['p_last_name'],
$_POST['post_slug'],
$_POST['post_date'],
$_POST['photo'],
$_POST['p_bio'],
$_POST['p_category_id'],
$_POST['p_type_id'],
$_POST['p_commish_id'],
$_POST['p_is_active']
));
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Hey David,
First thing - you have 9 placeholders (question marks), and 10 columns, so that needs sorting out.
Now, onto your problem. To upload a file from an HTML form, you need to have a File Input element:
<input type="file" name="photo">
When your form is posted (submitted), the normal fields (input / select / radio etc) become part of the POST array. However - a file input becomes part of the FILES array, so needs treating differently to the other fields. You would need to access the data like so:
$_FILES['photo']['name']
$_FILES['photo']['size']
$_FILES['photo']['tmp_name']
Basically, you won't have a $_POST['photo'] in the submitted data.
FYI - your move_uploaded_file line is also wrong - your need 2 parts for the - FROM (the tmp_name bit), and the TO bit
Hi Chris,
Ok fixed the “?” part. Added this…

This seems to be the problem…

I have the file on my local computer. Just trying to insert that file into the computer so that in the ‘photo’ field in the table it reads as such.
mark_adams.jpg
Tried this.. did not work.
$photo = $ai_id.'.'.$ext;
move_uploaded_file( '../assets/artists/'.'photo' );
OK,
2 moving parts to this question.
Inserting the data into the DB - I'm guessing we've got that part sorted by fixing the placeholders and by including the correct filename value.
The other part is doing something useful with the Uploaded file. When you upload a file like this, it initially goes to your temp folder, along with a temp name. This name is passed to your script as $_FILES['photo']['tmp_name']. To move that file to a location of your choosing, you need to call the move_uploaded_file function - this function takes 2 arguments : where you want to copy the file FROM and where you want to copy the file TO - look at your code and you're only passing in 1 argument - the FROM bit
move_uploaded_file( '../assets/artists/'.'photo' );
The FROM part will always be the tmp_name value from $_FILES, because that's where the file got uploaded to. The TO part is similar to what you have above, so what you need is more like this:
$from = $_FILES['photo']['tmp_name'];
$to = '../assets/artists/' . $_FILES['photo']['name'];
move_uploaded_file($from, $to);
You may want to use some logic to rename the file differently - the code above will simply use the original name (mark_adams.jpg for example) - but if you do that, make sure you're inserting the correct value into your DB






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Worked perfectly Chris. Thank you. Trying to bring it up from the edit page. I think I have my quotes wrong. Have been playing with them but no luck.
if($photo == '') {
echo 'No image found';
} else {
echo '<img src=../assets/artists/ . $_FILES['photo']['name']
class="existing-photo" style="width:200px;">';
}
?>
<input type="hidden" name="previous_photo"
value="<?php echo $photo['name']; ?>">
</div>
Yeah - your quotes are looking a little off
echo '<img src="../assets/artists/' . $_FILES['photo']['name'] . '" class="existing-photo" style="width:200px;">';
Here's a slighter better (more advanced way to do it). In PHP, you have a function called printf(). It's a bit like echo, but it allows you to use placeholders and makes it easier than concatenating (joining) different strings together. Basically, you just use %s as a placeholder, and then pass in the value as a second argument, so this:
printf('<img src="../assets/artists/%s" class="existing-photo" style="width:200px;">', $_FILES['photo']['name']);
You can see we have our string containing the placeholder as the first argument and then we have the actual value as the second argument.
You may or may not find it easier, but for me - it's gives cleaner code - particularly when messing around with quotes inside quotes etc.
Thank you Chris. Any tool helps me. I added the code and in return I received this error message.



Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
OK - the $_FILES['photo'] array will only exist if you've submitted a File Input called photo with a file selected.
Looking at your code above and you've got $photo['name'] and $_FILES['photo']['name'], so I'm not sure where you're at with this.
So. Are you saying that I have two different names here? Basically the edit page displays the image that we added with the previous code. So I removed the ['name'] part…

But that still left an error message. In over my head here.

I've lost track of what code you have as it seems we're looking at different pages here.
As I understand it, you have a page that contains a form that has a File Upload HTML element called photo. When you submit that form, the receiving page (either self or another one) then has access to that Form's data - the fields are part of the POST array and the File is part of the FILES array. At that point, you move the uploaded file to somewhere, and insert the data (including the filename) into the DB.
I don't really know where an ‘edit’ page comes into play here. Explain your pages. Is the code you've just posted on the same page as the code in your opening question?






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Hi Chris. The first page is adding an image. post-artist-add.php
This file is viewed on the post-artist.php page. On this page there is an edit button. This takes you to the post-artist-edit.php page this is where the problem is. The other two pages work well. The problem is with the edit page where the image Mark_Adams.jpg is suppose to be displayed. I am attaching the post-artist-edit.php page. The one that is getting the error
updated
When I isolate this code I no longer receive an error code.

So I would say the problem lies here. Just need to grab and display the image in the table….
Hi Chris. I figured it out! Yeah! This worked. Thank you for your help!
echo '<img src="../assets/artists/'.$photo.'" class="existing-photo" style="width:200px;">';

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Nice one David,
I'm glad you got it sorted.
I'd still recommend you work through the other issues as there are a lot of bugs in there that will at some point rear their ugly head :)
Chris thank you so much! I'm glad that you didn't use a red pen! LOL!
Really I do appreciate your help. Some of the mistakes were there some are mine. It's great that you are showing me the correct way. About formatting. It looks good on my end but I am sure it's not correct. Dreamweaver does have a “Format Source Code” button. Maybe I should click it? I am attaching a file after I clicked it. Can you tell me if it looks good?
Once again. Thank you!
Haha - sorry - it did feel a little like marking homework :)
I know it can be tough if you're working on someone else's code. Try to fix up all the errors can seem hard work.
I'm not sure Dreamweaver did a very good job of formatting. It still looks a little all over the place.
What is that page and how does it related to what you've been working on? I though we were dealing with the post-artist-edit.php page !






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
That's just a sample page. I really want to know about this formatting thing. Can you attach a sample page please. Should I start at the extreme left of the page?
OK. There's no specific right and wrong of this - it just makes life easier if your code is formatted in a clear manner. HTML, by it's nature is hierarchical - and you can use this to your advantage when indenting code. PHP can also be seen as hierarchical to a lesser extent. Things like functions / control blocks etc act as a parent to code within it, and again, you can use this to your advantage.
Not sure whether DreamWeaver allows you to configure things as I've not used it for a long time. I use VS Code for all development work and I've got that set up so that a TAB automatically takes 4 spaces, so indenting is really easy. I've also got a plug-in installed that verifies the PHP code, highlights problems, helps with formatting etc.
I've attached a copy of your file that I've auto-formatted. Hopefully it'll give you an idea of how to indent your code. I'ts not perfect as it makes some semi-intelligent guesses, but it's a good starting point.
PHP
--
Questions
--
Followers
Top Experts
PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.