質問

I've set up a website, but I don't know a lot of HTML or PHP. I have managed to get a mailing list sign up function into it, but I would like people to be able to remove themselves too. Is this possible? There's only one mailing list so all it need do is accept an email address, for sign up or sign off.

役に立ちましたか?

解決

I'm assuming you're using a file system to save. In that case you may need to loop through every line to find matching string and delete it.

Fortunately, since you're using PHP, it's probably easier to use a database like MySQL. Search for "PHP MySQL CRUD" or "PHP MySQL Tutorial" and you should find more help than you'll need.

After then it's just something like this:

$db = (MySQL Connection from the tutorials, usually PDO or mysqli);

function saveEmail($db, $name, $email){
    // Simple email validation, you will probably want to validate or sanitize other fields too
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
        return 'Email is not valid';
    }

    // Straight query, you may want to look into prepared statements too
    // You may also wish to check for duplicate emails or to set the field as UNIQUE
    $sql = "INSERT INTO table (name, email) VALUES ('$name', '$email')";
    if($db->query($sql)){
        return true;
    }else{
        return 'DB Insert Failed';
    }
}

function deleteEmail($db, $email){
    $sql = "DELETE FROM table WHERE email = '$email'";
    if($db->query($sql)){
        return true;
    }else{
        return 'DB Delete Failed';
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top