Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Inserting values into database when a button is clicked

When I click add to favourites, it add’s the user_id and the golf_id, however ‘golf_price’ keeps entering as ‘0’ instead of the actual price. Any help would be appreciated?

public function isFavourite($golf_id){
    $query = "SELECT * FROM user_favourites WHERE user_id = :user_id AND golf_id = :golf_id";
    $stmt = $this->Conn->prepare($query);
    $stmt->execute([
        "user_id" => $_SESSION['user_data']['user_id'],
        "golf_id" => $golf_id
    ]);
    return $stmt->fetch(PDO::FETCH_ASSOC);
}
public function toggleFavourite($golf_id){
    // Check if club is already in basket
    $is_favourite = $this->isFavourite($golf_id);

    if($is_favourite) {
        // Is already favourite - so remove. 
        $query = "DELETE FROM user_favourites WHERE user_fav_id = :user_fav_id";
        $stmt = $this->Conn->prepare($query);
        $stmt->execute([
            "user_fav_id" => $is_favourite['user_fav_id']
        ]);
        return false; // Return false for "removed"
    } else {
        // Is not favourite - so add
        $query = "INSERT INTO user_favourites (user_id, golf_id, golf_price) VALUES (:user_id, :golf_id, :golf_price)";
        $stmt = $this->Conn->prepare($query);
        $golf_price = "SELECT golf_price FROM products WHERE golf_id = :golf_id";
        return $stmt->execute(array(
            "user_id"=> $_SESSION['user_data']['user_id'],
            "golf_id" => $golf_id,
            "golf_price" => $golf_price
        ));
        return true; // Return false for "added"
        //1.use golf_id to select from products to get golf_price where golf_id matches
    }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You’re never executing the query in $golf_price, you’re trying to set golf_price to that literal string. Since it’s not numeric, it gets converted to 0.

You should put the SELECT query into the INSERT query.

        // Is not favourite - so add
        $query = "INSERT INTO user_favourites (user_id, golf_id, golf_price)
                  SELECT :user_id, golf_id, golf_price
                  FROM products WHERE golf_id = :golf_id";
        $stmt = $this->Conn->prepare($query);
        return $stmt->execute(array(
            "user_id"=> $_SESSION['user_data']['user_id'],
            "golf_id" => $golf_id
        ));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading