Jump to content

how i can make a php page that adds values and if the value already exists to deletes it instead?


giannis196
 Share

Recommended Posts

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...