0

For some reason this is not deleting anything now I echo out the $mailers variable, it puts out a email

$mailers = $_GET['leaveme'];
    $sql = "DELETE FROM `List` WHERE `email` = '" .$mailers . "'";
    $results = mysql_query($sql, $link) or die(mysql_error());  
Peter J
  • 23
  • 3
  • What would `exit( $sql );` before the actual execution of the statement show? Does it look correct? – Professor Abronsius Oct 16 '15 at 07:56
  • 1
    die(mysql_error()) giving anything? – nik Oct 16 '15 at 07:56
  • Does your user have the rights to delete data from the database? Is there a email named 'leaveme'? – sadfiesch Oct 16 '15 at 07:56
  • Do you get an error? – foxbeefly Oct 16 '15 at 07:56
  • 1
    Note: Use placeholders, don't assamble sql-strings with interpolation! – Tom Regner Oct 16 '15 at 07:56
  • @TomRegner to do that he'd have to stop using the deprecated `mysql_*` library first... http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – CD001 Oct 16 '15 at 08:00
  • Echo `$sql` and try make that query in phpmyadmin (or whatever else you use). Then show us error you are getting. – Martin Heralecký Oct 16 '15 at 08:05
  • 1
    1) This code is obsolete already 2) it's vulnerable to SQL injection attacks http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php and 3) are you actually connecting to the database in the first place? – CD001 Oct 16 '15 at 08:05
  • Figured it out, thanks so much everyone. – Peter J Oct 16 '15 at 08:06
  • I don't actually understand the question..? Could you edit the original post to make clearer in what case it did delete records, what change you made and what is happening now in the problem case? Or, if you've figured it out already and it's not something that could be made appropriate to any other users, remove the post? – Craig Graham Oct 16 '15 at 08:07
  • my problem was it was putting an extra line in the mysql database I fixed it with the following $string = trim(preg_replace('/\s\s+/', '', $string)); – Peter J Oct 16 '15 at 08:07
  • you need to refactor to NOT use mysql, its a deprecated driver. Use mysqli instead – DevDonkey Oct 16 '15 at 08:09

3 Answers3

1

Make sure you establish your connection correctly inside your $link variable.

$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());
}

Make sure also that your table (Link) and column (email) names were correct. Try running it first in your SQL page in PhpMyAdmin.

Take a look at SQL injections and use *_real_escape_string.

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

But if I were you, you should use prepared statement instead as mysql_* is already 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 */
Community
  • 1
  • 1
Logan Wayne
  • 6,001
  • 16
  • 31
  • 49
0

you have to connect to the mysql server with mysql_connect and select the DB before calling 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()); 
Martin Heralecký
  • 5,649
  • 3
  • 27
  • 65
0

my problem was it was putting an extra line in the mysql database I fixed it with the following

$string = trim(preg_replace('/\s\s+/', '', $string));
Peter J
  • 23
  • 3