我创建了4种输入类型名称,价格,描述,品牌和一种输入文件类型图像。点击提交按钮我希望所有5个输入进入数据库表"产品"但是在提交被点击时,它只显示例外输出"你的一些字段是空的"但所有领域都在填补。我甚至可以提供HTML代码。
<?php
// Do we have a Form Submission
if (isset($_POST['submit'])) {
try {
// Check that all your fields have values
if (
(!isset($_POST['name']) || empty($_POST['name'])) ||
(!isset($_POST['desc']) || empty($_POST['desc'])) ||
(!isset($_POST['price']) || empty($_POST['price'])) ||
(!isset($_POST['brand']) || empty($_POST['brand'])) ||
(!isset($_POST['image']) || empty($_POST['image']))
) {
throw new Exception('Some of your fields are empty.');;
}
$filename = $_FILES['fileToUpload']['name'];
$filetemp = $_FILES['fileToUpload']['temp_name'];
$filesize = $_FILES['fileToUpload']['size'];
$filebasename = basename($_FILES['fileToUpload']['name']);
$dir="uploads/";
$finaldir=$dir.$filebasename;
move_uploaded_file($filetemp,$finaldir);
// Prepare your Query and bind the parameters to the POST array
$stmt = $db->prepare("INSERT INTO product (name, description, price, brand, image) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param("ssss", $_POST['name'], $_POST['desc'], $_POST['price'], $_POST['brand'], $_POST['image']);
// Execute your query
$stmt->execute();
// Success!
printf("<p class='msg success'>%s</p>", "Your record was created.");
} catch (Exception $e) {
// There was a problem
printf("<p class='msg error'>%s</p>", $e->getMessage());
}
}
?>