0
我有一個表單將信息發佈到一個表和一張圖片到第二個表中,第一個表中有一個外鍵,兩個插入都是成功的,但是我的重定向語句不是...如果條件成功後isset工作不正常
這是PHP代碼:
if (isset($_POST['btn-save'])) {
$itemname = $_POST['title'];
$price = $_POST['price'];
$description = $_POST['description'];
$city_city_id = $user->getcityID($_POST['city']);
$category_category_id = $user->getcategoryID($_POST['category']);
$user_user_id = $_SESSION['userSession'];
if ($user->newItem($itemname, $price, $description, $city_city_id, $category_category_id, $user_user_id)) {
if (isset($_FILES['image'])) {
$image = file_get_contents($_FILES["image"]["tmp_name"]);
$item_item_id = $user->lastInsertID();
if ($user->newImage($image, $item_item_id)) {
header("Location: sellitem.php?inserted");
}
} else {
header("Location: sellitem.php?failure");
}
}
}
這些都是使用這兩種功能:
public function newItem($itemname, $price, $description, $city_city_id, $category_category_id, $user_user_id) {
try {
$stmt = $this->db->prepare("INSERT INTO item(itemname,description,price,city_city_id,category_category_id,user_user_id) VALUES(:itemname, :description, :price, :city_city_id, :category_category_id, :user_user_id)");
$stmt->bindparam(":itemname", $itemname);
$stmt->bindparam(":description", $description);
$stmt->bindparam(":price", $price);
$stmt->bindparam(":city_city_id", $city_city_id);
$stmt->bindparam(":category_category_id", $category_category_id);
$stmt->bindparam(":user_user_id", $user_user_id);
$stmt->execute();
return true;
} catch (PDOException $e) {
echo $e->getMessage();
return false;
}
}
public function newImage($image, $item_item_id) {
try {
$stmt = $this->db->prepare("INSERT INTO picture(image,item_item_id) VALUES(:image, :item_item_id)");
$stmt->bindparam(":image", $image);
$stmt->bindparam(":item_item_id", $item_item_id);
$stmt->execute();
} catch (PDOException $e) {
echo $e->getMessage();
return false;
}
}
什麼是錯誤? –
嘗試在函數'newImage'中添加'return true'在try條件下..它應該工作.. –
注意:測試'$ _FILES ['image']'是** NOT **是成功上傳的有效測試。所有這些會告訴你,如果上傳*嘗試*。你需要檢查'$ _FILES ['image'] ['error']'。另外,你只是假設查詢永遠不會失敗。這不是編碼事物的好方法。您需要顯式測試返回值或try/catch。 –