giannis196 Posted November 7, 2019 Share Posted November 7, 2019 how i can make a php page that adds values to the database, and if the value already exist to delete it instead of add it? I am trying to do it but i am making a mistake and it only "deletes values". ``` <?php require("database.php"); if(empty($username) || empty($password)){ echo "error"; }else{ $cdtitle=$_POST["cds"]; $cdartist=$_POST["kal"]; $query= 'SELECT FROM cds WHERE cdtitle=:cdtitle && cdartist=:cdartist'; $result = $sql->prepare($query); $result->execute([":cdtitle" => $cdtitle, ":cdartist" => $cdartist]); $a = $result->fetchAll(); if($a>0){ $cdtitle=$_POST["cds"]; $cdartist=$_POST["kal"]; $query= 'DELETE FROM cds WHERE cdtitle=:cdtitle && cdartist=:cdartist'; $result = $sql->prepare($query); $result->execute([":cdtitle" => $cdtitle, ":cdartist" => $cdartist]); $a = $result->fetchAll(); echo "Deleted!"; }else{ $add = "INSERT INTO cds(username, cdtitle, cdartist) VALUES (:username, :cdtitle, :cdartist)"; $result = $sql->prepare($add); $result->execute([":username" => $username ,":cdtitle" => $cdtitle, ":cdartist" => $cdartist]); $result->fetchAll(); echo "Added!"; } } ?> ``` ok where is my mistake? how i can make it this way that it will add the value to the database if it doesn't exists and it will delete the value from the database if it exist? Link to comment Share on other sites More sharing options...
niche Posted November 8, 2019 Share Posted November 8, 2019 Query your table to initialize values then do your nested if statement. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now