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 型別值的陣列偏移量 插入專案 <?php require_once "classes/DBAccessModify.php";
$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();
包括“模板/layoutModifyItem.html.php”;?>
uj5u.com熱心網友回復:
我應該將影像放在“影像”目錄中。謝謝閱讀
uj5u.com熱心網友回復:
droopsnoot 解決了問題 不是我!不知道怎么把他換過來。非常感謝
您的表單開始標記是否包含適當的 enctype 設定?– droopsnoot 15 小時前
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/401867.html
