Page 1 of 1

File Upload don't work

Posted: 18 Apr 2017, 09:09
by mlucicom
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();
}


?>

File Upload don't work

Posted: 18 Apr 2017, 10:40
by 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);

Re: File Upload don't work

Posted: 23 Apr 2017, 12:05
by mlucicom
Hello! The script don't upload the file and don't insert the image Patch in database

File Upload don't work

Posted: 23 Apr 2017, 17:08
by 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.