Hey guys/gals,

I literally just started teaching myself MySQL today and have started to write a email list script for mass emails.

Ive got the adding emails and sending mail scripts working but i cannot figure out for the life of me what is wrong with the deleting script. I include the entire code and separated what is giving me the issue.

I changed the error message and believe what i have singled out is where the issue arises.

I know the connection info is correct because its the same i have used for the other two scripts and i get no issues so i assume its in the code somewhere.

<body>
  <img src="blankface.jpg" width="161" height="350" alt="" style="float:right" />
  <p>Please select the email addresses to delete from the email list and click Remove.</p>
  <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 

<?php
      $dbc = mysqli_connect('localhost', 'dmwimbley0606', 'password', 'example')
    or die('Error connecting to MySQL server.'); 
  // Delete the customer rows (only if the form has been submitted)
  if (isset($_POST['submit'])) {
    foreach ($_POST['todelete'] as $delete_id) {
      $query = "SELECT FROM email_list WHERE id = $delete_id";
      mysqli_query($dbc, $query)
        or die('Error querying dHIHIHIHIHIHI.');
    } 
    echo 'Customer(s) removed.<br />';
    }  

  // Display the customer rows with checkboxes for deleting
  $query = "SELECT * FROM email_list";
  $result = mysqli_query($dbc, $query);
  while ($row = mysqli_fetch_array($result)) {
    echo '<input type="checkbox" value="' . $row['id'] . '" name="todelete[]" />';
    echo $row['first_name'];
    echo ' ' . $row['last_name'];
    echo ' ' . $row['email'];
    echo '<br />';
  }

  mysqli_close($dbc);
?>

    <input type="submit" name="submit" value="Remove" />
  </form>
</body>

Thanks,

David

Change the query : delete from....

$query = "SELECT FROM email_list WHERE id = $delete_id";

Change the query : delete from....

$query = "SELECT FROM email_list WHERE id = $delete_id";

Thanks for the response but i actually have tried that and i still get a connection error. Any other thoughts?

Thanks for the response but i actually have tried that and i still get a connection error. Any other thoughts?

If you get a connection error than it's not the query that is wrong.
What EXACTLY is the error being displayed to the page?

If you get a connection error than it's not the query that is wrong.
What EXACTLY is the error being displayed to the page?

The error being desplayed is this one here, i edited the display error so i could narrow it down. The page displays this error "Error querying dHIHIHIHIHIHI."

I dont understand, if it can query the database and bring the rows of information and display it on the page, then why cant it delete it?

The connection string is the same for the other two scripts i have written and they work fine, the adding and sending email script

Thanks for the help

// Delete the customer rows (only if the form has been submitted)
  if (isset($_POST['submit'])) {
    foreach ($_POST['todelete'] as $delete_id) {
      $query = "SELECT FROM email_list WHERE id = $delete_id";
      mysqli_query($dbc, $query)
        or die('Error querying dHIHIHIHIHIHI.');
    }

The error being desplayed is this one here, i edited the display error so i could narrow it down.

I dont understand, if it can query the database and bring the rows of information and display it on the page, then why cant it delete it?

The connection string is the same for the other two scripts i have written and they work fine, the adding and sending email script

Thanks for the help

// Delete the customer rows (only if the form has been submitted)
  if (isset($_POST['submit'])) {
    foreach ($_POST['todelete'] as $delete_id) {
      $query = "SELECT FROM email_list WHERE id = $delete_id";
      mysqli_query($dbc, $query)
        or die('Error querying dHIHIHIHIHIHI.');
    }

Because as adapost said, you're not deleting with that query, you're just selecting. Changed SELECT to DELETE. And for future reference, when you're doing or die() on a query, don't put in arbitrary text do die(mysql_error()) (In other words, read the damn FAQ)

Because as adapost said, you're not deleting with that query, you're just selecting. Changed SELECT to DELETE. And for future reference, when you're doing or die() on a query, don't put in arbitrary text do die(mysql_error()) (In other words, read the damn FAQ)

Hey ive changed the delete to select just to see what it does if anything was done differently but let me edit the code and ill let you know. Thanks.

Because as adapost said, you're not deleting with that query, you're just selecting. Changed SELECT to DELETE. And for future reference, when you're doing or die() on a query, don't put in arbitrary text do die(mysql_error()) (In other words, read the damn FAQ)

Ive changed the SELECT back to DELETE and have entered the die(mysql_error()) and now no error pops up. But its not deleting the record, it is still fetching the records out of the database.

Ive changed the SELECT back to DELETE and have entered the die(mysql_error()) and now no error pops up. But its not deleting the record, it is still fetching the records out of the database.

When you tell us you changed code and you're getting a new error the smart thing to do would be to post the new code. We're not telepathic nor do we live in this here computer-box and can travel through the wires to see what you're doing.

Is it corrent?

SELECT FROM email .....

If this statement is select then he/she must have to include column list or *.

// Delete the customer rows (only if the form has been submitted)
  if (isset($_POST['submit'])) {
    foreach ($_POST['todelete'] as $delete_id) {
      $query = "SELECT FROM email_list WHERE id = $delete_id";
      mysqli_query($dbc, $query)
        or die('Error querying dHIHIHIHIHIHI.');
    }

Is it corrent?

SELECT FROM email .....

If this statement is select then he/she must have to include column list or *.

// Delete the customer rows (only if the form has been submitted)
  if (isset($_POST['submit'])) {
    foreach ($_POST['todelete'] as $delete_id) {
      $query = "SELECT FROM email_list WHERE id = $delete_id";
      mysqli_query($dbc, $query)
        or die('Error querying dHIHIHIHIHIHI.');
    }

That code will always fail because SELECT FROM is invalid MySQL syntax. The comment says that it should be deleted, all that has to be changed is SELECT to DELETE

When you tell us you changed code and you're getting a new error the smart thing to do would be to post the new code. We're not telepathic nor do we live in this here computer-box and can travel through the wires to see what you're doing.

That is the "new" code under the comment: // Delete the customer rows (only if the form has been submitted)

<?php
	  $dbc = mysqli_connect('65.175.112.230', 'dmwimbley0606', '6sknvn', 'example')
    or die(mysql_error()); 

  // Delete the customer rows (only if the form has been submitted)
  if (isset($_POST['submit'])) {
    foreach ($_POST['todelete'] as $delete_id) {
	$query = "DELETE FROM email_list WHERE id = $delete_id";
	mysqli_query($dbc, $query)
        or die(mysql_error());
    } 

    echo 'Customer(s) removed.<br />';
    }  

  // Display the customer rows with checkboxes for deleting
  $query = "SELECT * FROM email_list";
  $result = mysqli_query($dbc, $query);
  while ($row = mysqli_fetch_array($result)) {
    echo '<input type="checkbox" value="' . $row['id'] . '" name="todelete[]" />';
    echo $row['first_name'];
    echo ' ' . $row['last_name'];
    echo ' ' . $row['email'];
    echo '<br />';
  }

  mysqli_close($dbc);
?>

If you didn't get an error then it means it deleted the row. Before mysqli_query do echo $query; just to make sure it's deleting what you think it is deleting.

>That is the "new" code under the comment:

Thanks,

Please mark this thread as Solved.

>That is the "new" code under the comment:

Thanks,

Please mark this thread as Solved.

haha, I don't think (s)he actually had it solved, (s)he was just responding to my request to repost the new code.

If you didn't get an error then it means it deleted the row. Before mysqli_query do echo $query; just to make sure it's deleting what you think it is deleting.

Hey i hate to do this to you but i did the echo $query; and it displayed my query but it still did not delete anything.

this is what it displayed

DELETE FROM email_list WHERE id =

Hey i hate to do this to you but i did the echo $query; and it displayed my query but it still did not delete anything.

this is what it displayed

DELETE FROM email_list WHERE id =

Which tells you what? That $delete_id is empty. Change the current

// Delete the customer rows (only if the form has been submitted)
  if (isset($_POST['submit'])) {
    foreach ($_POST['todelete'] as $delete_id) {
	$query = "DELETE FROM email_list WHERE id = $delete_id";
	mysqli_query($dbc, $query)
        or die(mysql_error());
    } 

    echo 'Customer(s) removed.<br />';
    }

to

if (isset($_POST['submit'])) {
  var_dump($_POST['todelete']);
/*
  foreach ($_POST['todelete'] as $delete_id) {
    $query = "DELETE FROM email_list WHERE id = $delete_id";
    mysqli_query($dbc, $query)
      or die(mysql_error());
  } 

  echo 'Customer(s) removed.<br />';
*/
}

and tell me the output

Which tells you what? That $delete_id is empty. Change the current

// Delete the customer rows (only if the form has been submitted)
  if (isset($_POST['submit'])) {
    foreach ($_POST['todelete'] as $delete_id) {
	$query = "DELETE FROM email_list WHERE id = $delete_id";
	mysqli_query($dbc, $query)
        or die(mysql_error());
    } 

    echo 'Customer(s) removed.<br />';
    }

to

if (isset($_POST['submit'])) {
  var_dump($_POST['todelete']);
/*
  foreach ($_POST['todelete'] as $delete_id) {
    $query = "DELETE FROM email_list WHERE id = $delete_id";
    mysqli_query($dbc, $query)
      or die(mysql_error());
  } 

  echo 'Customer(s) removed.<br />';
*/
}

and tell me the output

Here is the output from your changes

array(1) { [0]=> string(0) "" }

Ok, now post your form from which this is getting submitted to.

Here is the entire code from the delete email address page

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Remove Email</title>
  <link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
  <p>Please select the email addresses to delete from the email list and click Remove.</p>
  <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 

<?php
	  $dbc = mysqli_connect('ipaddress', 'username', 'password', 'example')
    or die(mysql_error()); 

  // Delete the customer rows (only if the form has been submitted)
  
  
      if (isset($_POST['submit'])) {

      var_dump($_POST['todelete']);

      /*
        foreach ($_POST['todelete'] as $delete_id) {
        $query = "DELETE FROM email_list WHERE id = $delete_id";
        mysqli_query($dbc, $query)
        or die(mysql_error());
        }
       
        echo 'Customer(s) removed.<br />';
      */
      } 



  // Display the customer rows with checkboxes for deleting
  $query = "SELECT * FROM email_list";
  $result = mysqli_query($dbc, $query);
  while ($row = mysqli_fetch_array($result)) {
    echo '<input type="checkbox" value="' . $row['id'] . '" name="todelete[]" />';
    echo $row['first_name'];
    echo ' ' . $row['last_name'];
    echo ' ' . $row['email'];
    echo '<br />';
  }

  mysqli_close($dbc);
?>

    <input type="submit" name="submit" value="Remove" />
  </form>
</body>
</html>

Do the checkboxes show up on the form? And are you checking a checkbox when you test the delete?

Do the checkboxes show up on the form? And are you checking a checkbox when you test the delete?

the check boxes do come up and i am checking the box when i test the delete.

When i did the var dump statement from earlier, with no check boxes selected i got an output of NULL, with one box selected i got array(1) { [0]=> string(0) "" } , two boxes i got array(2) { [0]=> string(0) "" } and so on

If you're getting empty values then $row['id'] is empty when you're writing the checkboxes. View the source of the page and make sure their values aren't empty. If they are the only explanation is that there is no "id" field

Your values are empty which means there is now id field in your table. var_dump($row); before you echo the <input> tag

Here is the page when you first go to it

edited

the output from when you select and click remove is still empty

Of course it's going to be empty, there is no ID field in the table. I'm going to assume you're using the email as the primary key, so change id to email in both the $row['id'] and changed WHERE id = $delete_id to WHERE email = '$delete_id' .

Also, edit your post to remove those emails.

Of course it's going to be empty, there is no ID field in the table. I'm going to assume you're using the email as the primary key, so change id to email in both the $row['id'] and changed WHERE id = $delete_id to WHERE email = '$delete_id' .

Also, edit your post to remove those emails.

ok that seemed to work, but its still not deleting it from the database...thanks a lot for the help i appreciate it. The echo query shows that only one selected checkboxes email is shown at the end of the echo query command

ok that seemed to work, but its still not deleting it from the database...thanks a lot for the help i appreciate it. The echo query shows that only one selected checkboxes email is shown at the end of the echo query command

stupid question, did you uncomment the part that is deleting? ie. remove the /* */. Also, you didn't put quotes around the email like I showed

stupid question, did you uncomment the part that is deleting? ie. remove the /* */. Also, you didn't put quotes around the email like I showed

lol yea i have uncommented it, still no luck. Thanks, i do appreciate the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.