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 data to the database if it doesn’t exists and it will delete the data from the database if it exist?
Thanks!