dongshou1991 2015-12-21 19:15
浏览 32
已采纳

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

I have a form which posts information to one table and a picture to a second table with a foreign key from the first table, both inserts are successful, but my redirect statement isn't...

This is the PHP code:

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");
            }
        }
    }

These are both functions used:

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;
    }
}
  • 写回答

1条回答 默认 最新

  • dongyan6235 2015-12-21 20:23
    关注

    You include this line:

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

    Which PHP evaluates as:

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

    This means the return value of newImage needs to evaluate as true. But, your function is only returning a value in case of error when it returns false. Edit the function to include a return value in the success case as well:

    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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效