gpt4 book ai didi

php - 如果条件成功后 isset 无法正常工作

转载 作者:行者123 更新时间:2023-11-29 11:49:21 25 4
gpt4 key购买 nike

我有一个表单,它将信息发布到一个表,并将图片发布到第二个表,并使用第一个表的外键,两次插入都成功,但我的重定向语句没有...

这是 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;
}
}

最佳答案

您包括这一行:

if ($user->newImage($image, $item_item_id))

PHP 的计算结果为:

if ($user->newImage($image, $item_item_id) == true)

这意味着 return value newImage 需要评估为 true。但是,您的函数仅在发生错误时返回 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();
return true;
} catch (PDOException $e) {
echo $e->getMessage();
return false;
}
}

关于php - 如果条件成功后 isset 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34402758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com