php - $album =$_post ['formdata' ] -- 值未分配给带有文件上传 html 表单和 php 5 的变量

标签 php html mysql forms

您好,我是 php 的新手,我正在尝试创建一个网站,在该网站上可以上传文件,其中包含从带有 php 文件上传的 html 用户界面表单收集的其他详细信息。在这里,我的目标是将图像文件上传到一个文件夹并生成唯一的文件名,并将文件名以及其他详细信息(如相册名称和描述)添加到 mysql 数据库中,以下是我得到的错误

ERROR: Try AgainError: INSERT INTO playlist (name, description, user_id, thumb) VALUES ('', '', '', 'dewdropsondandelionseeds7054953530.jpg') Duplicate entry '0' for key 'uniqueindex'

HTML 表单 (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(上传.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";
?>

最佳答案

您在播放列表表中有唯一键,并且没有设置 AUTOINCREMENT,因此请手动设置或添加正确的键

关于php - $album =$_post ['formdata' ] -- 值未分配给带有文件上传 html 表单和 php 5 的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32114775/

相关文章:

PHP:如何在另一个 if 语句中使用变量

php - 用于 PHP 处理新行的自定义 Markdown

html - Css 背景颜色不起作用

javascript - Web 应用程序从哪里开始?

php - 表设计及相关sqli查询

MySQL 6.3 - 创建过程语法错误

java - 对 "__key__"属性的查询是否与 GAE 数据存储高度一致?

php - 将所有 id 更改为 uuid

javascript - 无法使用 jquery 获取背景框以打开和关闭显示

java - 无法使用 document.getElementById 获取元素,返回 null