File Upload don't work

mlucicom
Posts: 23

File Upload don't work

Hello! i have a script that insert some details in MySql database but the file upload don't work.Have you any ideea why don't work ?

Code: Select all

if(isset($_POST['submit']))
{
   
    // get values form input text and number

$id = trim($_GET['id']);
$subiect = $_POST['motiv'];
    $mesaj = $_POST['coment'];
    $contact = $_POST['perioada'];
    $data = date('m/d/Y');
       $target = "images/";   
       $fileName = $_FILES['Filename']['name'];   
      $fileTarget = $target.$fileName;   
      $tempFileName = $_FILES["Filename"]["tmp_name"];
      $fileDescription = $_POST['Description'];   
      $result = move_uploaded_file($tempFileName,$fileTarget);

   /*
   *   If file was successfully uploaded in the destination folder
   */
   if($result) {  echo "FIle uploaded";
}
    // mysql query to insert data


$servername = "localhost";
$username = "";
$password = "";
$dbname = "";

// Create connection

$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO contact2 (location,subiect,mesaj,imagine,contact,data)
VALUES ('$id', '$subiect','$mesaj','$fileName','$contact','$data')";

if ($conn->query($sql) === TRUE) {
    $last_id = $conn->insert_id;
  echo "Your message was se";

} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
}


?>

Admin
Hello
I not know what problem you have with that script. Does it not put the file on server, or does it not insert data in mysql?
It might be problem from the html form, or the column types in mysql, or variable with data for inserting in mysql is not defined.
Check with " echo $sql; " to see if the resulted sql query string is ok, with all necessary data. Then, you can copy and test the sql query in phpmyadmin.

Also, you can use this code at the beginning of the script, it sets php to display its errors:

Code: Select all

ini_set('display_errors',1);
error_reporting(E_ALL);

mlucicom
Hello! The script don't upload the file and don't insert the image Patch in database

Admin
I not know whay it not works, it might be problem from the html form, or the column types in mysql, or variable with data for inserting in mysql is not defined.
If there is no error, try add: echo $sql; to see if all data are properly added in that sql query.
Also, the directory where you want to upload the files must have CHMOD 0755 (or 0777) to allow php write files in it.

Adv. Posts: 01

Similar Topics