如果 enctype 设置不正确,照片不会通过 php 文件插入,但其他数据可以通过

if enctype setting is not correct, photo is not inserted through php file however other data can come through

h

您好,我想在包含照片的数据库中插入数据。同样在插入数据库后,图像应该出现在网页上。它被插入到数据库中,但是当我想在网页中看到它时,我收到以下警告:

警告:第 30

行 D:\xamppweekend\htdocs\work\deliverc\web7ForServer3\insertItem.php 中未定义数组键“photoPath”

警告:尝试访问第 30

行 D:\xamppweekend\htdocs\work\deliverc\web7ForServer3\insertItem.php 中 null 类型值的数组偏移量

警告:第 43 行

中 D:\xamppweekend\htdocs\work\deliverc\web7ForServer3\insertItem.php 中未定义的数组键“photoPath”

警告:尝试访问第 43 行 D:\xamppweekend\htdocs\work\deliverc\web7ForServer3\insertItem.php 中 null 类型值的数组偏移量 插入项目

$title = "Insert";
$pageHeading = "Insert item";
//get database settings
include "settings/db.php";
// ob_start();
//create database object
$db = new DBAccess($dsn, $username, $password);
//connect to database
$pdo = $db->connect();
$message = "";
$error = false;
//get categories to poulate drop down list
$sql = "select categoryId, categoryName from category";
$stmt = $pdo->prepare($sql);
//execute SQL query
$categoryRows = $db->executeSQL($stmt);

//insert item when the button is clicked

if(isset($_POST["submit"]))
{
//check a item name was supplied
if(!empty($_POST["itemName"]))
{
    $targetDirectory = "images/";
// //get the filename
$photoPath = basename($_FILES["photoPath"]["name"]); 
// //set the entire path
$targetFile = $targetDirectory . $photoPath;
// //only allow image files
$imageFileType = pathinfo($targetFile,PATHINFO_EXTENSION);
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" )
{
$message = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$error = true;
}
//check the file size php.ini has an upload_max_filesize, default set to 2M
// if the file size exceeds the limit the error code is 1
if ($_FILES["photoPath"]["error"] == 1)
{
$message = "Sorry, your file is too large. Max of 2M is allowed.";
$error = true;
}
if($error == false)
{
if (move_uploaded_file($_FILES["photoPath"]["tmp_name"], $targetFile))
{
$message = "The file $photoPath has been uploaded.";
}
else
{
$message = "Sorry, there was an error uploading your file. Error Code:" . $_FILES["photoPath"]["error"];
$photoPath = "";
}
}
else
{
$photoPath = "";
 }
    //set up query to execute
    //insert items
$sql = "insert into item(itemName, price, salePrice, categoryId, photo, description) values(:itemName, :price, :salePrice, :categoryId, :photo,
 :description)";
$stmt = $pdo->prepare($sql);
$stmt->bindValue(":itemName" , $_POST["itemName"], PDO::PARAM_STR);
$stmt->bindValue(":price" , $_POST["price"], PDO::PARAM_STR);
$stmt->bindValue(":salePrice" , $_POST["salePrice"], PDO::PARAM_STR);
$stmt->bindValue(":categoryId" , $_POST["categoryId"], PDO::PARAM_STR);

$stmt->bindValue(":photo" , $photoPath, PDO::PARAM_STR);
$stmt->bindValue(":description" , $_POST["description"], PDO::PARAM_STR);




//execute SQL query
$id = $db->executeNonQuery($stmt, true);
$message = "The item was added, id: " . $id;
}
}
//start buffer
ob_start();
//display form
// include "templates/modifyItemForm.html.php";
$output = ob_get_clean();

包括“templates/layoutModifyItem.html.php”; ?>

我应该将图像放在“图像”目录中。感谢阅读

droopsnoot 解决了问题 不是我!不知道怎么把他代替。非常感谢 lotttt

您的表单开始标记是否包含适当的 enctype 设置? – 低头 15 小时前