2013-03-06 78 views
-1

我已經建立了一個網站,但我不知道很多HTML或PHP。我已經設法在郵件列表中註冊函數,但是我希望人們也能夠自己刪除它們。這可能嗎?只有一個郵件列表,所以它只需接受一個電子郵件地址,即可註冊或註銷。允許註冊和註銷的網站郵件列表代碼

回答

1

我假設你正在使用文件系統來保存。 在這種情況下,您可能需要遍歷每一行以查找匹配的字符串並將其刪除。

幸運的是,由於您使用的是PHP,因此使用像MySQL這樣的數據庫可能更容易。 搜索「PHP MySQL CRUD」或「PHP MySQL Tutorial」,你應該找到比你需要更多的幫助。

然後之後,它只是像這樣:

$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'; 
    } 
} 
+0

感謝斯科特。 Id想知道如果MySQL是前鋒,但我不是一個程序員的訓練,因爲你可能猜到:) – cianius 2013-03-07 00:26:18

+0

公頃,我也沒有我 – 2013-03-07 00:27:51

+0

但我不得不說PHP是有史以來最簡單的語言,因爲它的文件。 MySQL也很容易,但是PHP5.5迫使每個人都從程序化的mysql適配器切換到mysqli或PDO,這使得事情「更加困難和更加簡潔」。 – 2013-03-07 00:34:30