Skip to content
Advertisement

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

h

hi I want to insert data in database included photo. also after inserting in database, image should come to a web page. it get inserted in database but I get the following warning when I want to see it in a web page:

Warning: Undefined array key “photoPath” in D:xamppweekendhtdocsworkdelivercweb7ForServer3insertItem.php on line 30

Warning: Trying to access array offset on value of type null in D:xamppweekendhtdocsworkdelivercweb7ForServer3insertItem.php on line 30

Warning: Undefined array key “photoPath” in D:xamppweekendhtdocsworkdelivercweb7ForServer3insertItem.php on line 43

Warning: Trying to access array offset on value of type null in D:xamppweekendhtdocsworkdelivercweb7ForServer3insertItem.php on line 43 Insert item <?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();

include “templates/layoutModifyItem.html.php”; ?>

Advertisement

Answer

droopsnoot solved the problem Not me! Do not know how to put him instead. Thanks a lotttt

Does your form opening tag contain the appropriate enctype setting? – droopsnoot 15 hours ago

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement