php mysql没有从数据库中删除

时间:2015-10-16 07:53:44

标签: php mysql

由于某种原因,这不是删除任何东西 现在我回显了$ mailers变量,它发出了一封电子邮件

$mailers = $_GET['leaveme'];
    $sql = "DELETE FROM `List` WHERE `email` = '" .$mailers . "'";
    $results = mysql_query($sql, $link) or die(mysql_error());  

3 个答案:

答案 0 :(得分:1)

确保在$link变量中正确建立连接。

$link = mysql_connect('Host', 'username', 'password'); /* REPLACE NECESSARY DATA */
if (!$link) {
    die('Not connected : ' . mysql_error());
}

$db_selected = mysql_select_db('database', $link); /* REPLACE NECESSARY DATABASE NAME */
if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
}

还要确保您的Link)和email)名称正确无误。首先尝试在PhpMyAdmin的SQL页面中运行它。

查看SQL injections并使用*_real_escape_string

$mailers = mysql_real_escape_string($_GET['leaveme']);

但如果我是你,你应该使用prepared statementmysql_*已经deprecated

$link = new mysqli("host", "User", "password", "database"); /* REPLACE NECESSARY DATA */

/* CHECK CONNECTION */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

if($stmt = $con->prepare("DELETE FROM List WHERE `email` = ?")){ /* PREPARE THE QUERY */
  $stmt->bind_param("s", $_GET["leaveme"]); /* BIND THIS VARIABLE TO YOUR QUERY */
  $stmt->execute(); /* EXECUTE THE QUERY */
  $stmt->close(); /* CLOSE THE STATEMENT */
} /* END OF PREPARED STATEMENT */

答案 1 :(得分:0)

你必须用mysql_connect连接到mysql服务器并在调用mysql_query之前选择数据库

$link = mysql_connect('localhost', 'user', 'pass');
if (!$link) die('Not connected : ' . mysql_error());

mysql_select_db('yourdbname', $link);

$mailers = $_GET['leaveme'];
$sql = "DELETE FROM `List` WHERE `email` = '" .$mailers . "'";
$results = mysql_query($sql, $link) or die(mysql_error()); 

答案 2 :(得分:0)

我的问题是它在mysql数据库中添加了一行,我用以下

修复了它
$string = trim(preg_replace('/\s\s+/', '', $string));