3

I am writing a code where I can upload an image in a folder and save data form into a Mysql db. At the moment just Saving data form in a db table is working but I can't upload the image in a folder. This is the code:

    <?php

if(isset($_POST['upload_img'])){

$file_name = $_FILES['image']['name'];
$file_type = $_FILES['image']['type'];      
$file_size = $_FILES['image']['size'];
$file_tmp_name  = $_FILES['image']['tmp_name'];

if($file_name){
            move_uploaded_file($file_tmp_name,"images/$file_name");

            }
}
?>
<?php
  include 'guestconfig/config.php';

if (isset($_POST['upload_img']) && $_POST['upload_img']=="invia")
{
        $image = $file_name;
        $title = addslashes($_POST['title']);
        $price = addslashes($_POST['price']);



  $sql = "INSERT INTO testone
(image,title,price) VALUES ('$image', '$title','$price')";
  if($result = mysql_query($sql) or die (mysql_error()))
  {
    echo "Inserimento avvenuto con successo.<br>
    Vai alla <a href=\"index.php\">Home Amministrazione</a>";
  }
}else{
  ?>
  <table border="1">
  <tr>
  <td>
  <img src="xxx.jpg" alt="xxx" />
  <img src="xxx.jpg" alt="xxx" />
  </td>

  </tr>
  <tr>
  <td>

  <form action="" method="post" enctype="multipart/form-data">

<label>upload image </label><br>
<input type="file" name="image"><br>
<br><br>

title:
<input name="title" type="text"><br><br>

price:
<input name="price" type="text"><br><br>

<input name="upload_img" type="submit" value="invia">
</form>
</td>

</tr>
</table>
  <?php
}
?>
<br>

I hope you can help me. Thanks! :)

EDIT: Ok, now I can upload my image but I can't save the image name (before I could). How can I do it, please? Thanks! :)

EDIT2: Ok, now it's perfect... Thanks to everybody! :)

4
  • What is ctype="multipart/form-data"?? Commented Jul 5, 2016 at 9:34
  • use this image upload code stackoverflow.com/questions/37320872/… Commented Jul 5, 2016 at 9:34
  • Use enctype="multipart/form-data" instead of ctype Commented Jul 5, 2016 at 9:35
  • use enctype="multipart/form-data" instead of ctype="multipart/form-data" Commented Jul 5, 2016 at 9:36

3 Answers 3

1

First replace ctype="multipart/form-data" with enctype="multipart/form-data"

   if(!empty($file_name)){
                  if(move_uploaded_file($file_tmp_name,"images/".$file_name))
echo "image uploaded";
else  echo "image not uploaded";


                }
Sign up to request clarification or add additional context in comments.

2 Comments

Ok, I correct it :) but now why the image name is not saved in my db? Thanks again
@Marci why you have used addslashes function??
1

form tag must be like this

 <form action="" method="post" enctype="multipart/form-data">

and in php you can do something like this

if(!empty($file_name)){
              move_uploaded_file($file_tmp_name,"images/".$file_name);
            }

Comments

1

You have error in form tag

<form action="" method="post" enctype="multipart/form-data">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.