Hi I am new to php and am trying to make a website where files can be uploaded with additional details collected from a html user interface form with php file upload. Here my aim is to upload Image files to a folder and generate unique file name and add file name along with other details like album name and description into mysql database following is the error i get
ERROR: Try AgainError: INSERT INTO playlist (name, description, user_id, thumb) VALUES ('', '', '', 'dewdropsondandelionseeds7054953530.jpg') Duplicate entry '0' for key 'uniqueindex'
HTML Form (index.html)
<!DOCTYPE html>
<html>
<body>
<form method="post" action="upload.php" enctype="multipart/form-data">
Album: <input type="text" name="name" value='' size="20">
<p>Description</p>
<textarea rows="10" cols="35" name="details"></textarea>
<p>Thumb:</p>
<input type="file" name="thumb" size="20">
<br/>
<input TYPE="submit" name="upload" value="Add"/>
</form>
</body>
</html>
PHP(Upload.php)
<?php
// Access the $_FILES global variable for this specific file being uploaded
// and create local PHP variables from the $_FILES array of information
$sessionid = 1;
$album = isset($_POST['album']) ? $_POST['album'] : ''; // The Album's Name
$detail= isset($_POST['detail']) ? $_POST['detail'] : '';
//The Album's Detail
echo "bla bla $album,$detail";
$fileName = $_FILES["thumb"]["name"]; // The file name
$fileTmpLoc = $_FILES["thumb"]["tmp_name"]; // File in the PHP tmp folder
$fileType = $_FILES["thumb"]["type"]; // The type of file it is
$fileSize = $_FILES["thumb"]["size"]; // File size in bytes
$fileErrorMsg = $_FILES["thumb"]["error"]; // 0 for false... and 1 for true
$fileName = preg_replace('#[^a-z.0-9]#i', '', $fileName);
$kaboom = explode(".", $fileName); // Split file name into an array using the dot
$newfilename = substr(microtime(), 2, 7) . '.' .end($kaboom);
$fileExt = end($kaboom); // Now target the last array element to get the file extension
$ff_name =$kaboom[0].$newfilename;
// $fileName = time().rand().".".$fileExt;
// START PHP Image Upload Error Handling --------------------------------------------------
if (!$fileTmpLoc) { // if file not chosen
echo "ERROR: Please browse for a file before clicking the upload button.";
exit();
} else if($fileSize > 52428800) { // if file size is larger than 50 Megabytes
echo "ERROR: Your file was larger than 50 Megabytes in size.";
unlink($fileTmpLoc);
// Remove the uploaded file from the PHP temp folder
exit();
} else if (!preg_match("/.(gif|jpg|png)$/i", $fileName) ) {
// This condition is only if you wish to allow uploading of specific file types
echo "ERROR: Your image was not .gif, .jpg, or .png.";
unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
exit();
} else if ($fileErrorMsg == 1) { // if file upload error key is equal to 1
echo "ERROR: An error occured while processing the file. Try again.";
exit();
}
// END PHP Image Upload Error Handling ----------------------------------------------------
// check directory of album and or create new
//-----------------------------------------------
// Desired folder structure
$folder = './uploads/$album/';
if (file_exists($folder)){
echo "ERROR:The Album Already exists Please rename the Album, ./uploads/$album";
} else {
// To create the nested structure, the $recursive parameter
// to mkdir() must be specified.
if (!mkdir($folder, 0777, true)){
die('ERROR:Failed to create folders...');
}
}
// Now Upload Image File
//---------------------------------
// Place it into your "uploads" folder now using the move_thumb() function
$moveresult = move_uploaded_file($fileTmpLoc, "${folder}${ff_name}");
echo "$fileTmpLoc, $folder$fileName. $newfilename";
// Check to make sure the move result is true before continuing
if ($moveResult = true) {
echo "ERROR: File uploaded successfully. ${folder}${ff_name}";
} else{
echo "ERROR: File not uploaded. Try again.";
exit();
}
//start inserting data into database
//--------------------------------------
$servername = "localhost";
$username = "audgaldbusr";
$password = "audgaldbpwd";
$dbname = "audiogallery";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("ERROR:Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO playlist (name, description, user_id, thumb) VALUES ('$album', '$detail', '$session', '$ff_name')";
if ($conn->query($sql) === TRUE) {
echo "ERROR:New record created successfully";
} else {
echo "ERROR: Try Again";
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
//end inserting data into database
//-----------------------------------------------
// Display things to the page so you can see what is happening for testing purposes
echo "Thumb <strong>$newfilename</strong> uploaded successfuly.<br /><br />";
echo "It is <strong>$fileSize</strong> bytes in size.<br /><br />";
echo "It is an <strong>$fileType</strong> type of file.<br /><br />";
echo "The file extension is <strong>$moveresult</strong><br /><br />";
echo "The Error Message output for this upload is: $fileErrorMsg";
?>
You have unique key in playlist table, and there is no AUTOINCREMENT set, so set it or add correct key manualy