PHP code not working the way I hoped, need help!



I’ve created a single table database for my personal movie collection using self referencing PHP in a single page. I am trying to create a form that will search the database for (a) title(s) with a text input/submit input, return them as checkbox options and then remove (the checked) options with a submit input.

Here is my code for this section:

Remove Titles from Database

Remove by movie title

Search Results:

<?php require_once('connectVars.php'); $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die('can not connect');

if (isset($_POST[‘remove’])) {

foreach ($_POST[‘todelete’] as $delete_id) {

$remove = “DELETE FROM email_list WHERE id = $delete_id”;

mysqli_query($dbc, $remove)

or die(‘Error querying database.’);

echo ‘Movie(s) removed.

  if(isset($_POST['searchRemove'])) {
   $search = $_POST['searchText'];
   $search = preg_replace("#[^0-9a-z]i#","", $search);
   $query = "SELECT * FROM MOVIES WHERE TITLE LIKE '%".$search."%'" or die ("Could not search");
   $result = mysqli_query($dbc, $query)
   or die ('Error, Error');

while ($row = mysqli_fetch_array($result)) {
echo ‘’;
echo $row[‘TITLE’];
echo ’ on ’ . $row[‘TYPE’];
echo ‘


Can you provide more information related to how you wanted it to work and the current result? Because it is hard to understand what you want to achieve using the current php if we don’t know it.

So far I understand that connectVars.php hold any information related to the mysqli_connect function parameters. The rest of the code is related to query / delete from your database.


So, right now it will search the database and return titles entered into the search, the deletion of rows is broken.! This is what it does, the search works :

But when you click remove:

it gives these errors, the todelete and foeach aren’t functioning.


It seems that the error is related to the following block of code:

// You check if the variable $_POST["remove"] is defined on your PHP code.
// If that is TRUE, then you execute the foreach loop.
if (isset($_POST["remove"])) { 
  // Here comes the problem (what I think):
  // You loop inside $_POST["todelete"] variable but you don't know if it exist.
  foreach ($_POST["todelete"] as $delete_id) {
    $remove = "DELETE FROM email_list WHERE id = $delete_id";
    mysqli_query($dbc, $remove) or die("Error querying database.");
  echo "Movie(s) removed.";

In order to debug your code, I recommend placing the next code to checkout the value of $_POST["todelete"] and $_POST["remove"] are defined on your code. Then hit F12 to see the output on the console.

// Before the foreach:
echo '<script>console.log('.$_POST["remove"].')</script>';
echo '<script>console.log('.$_POST["todelete"].')</script>';
// Inside the foreach:
echo '<script>console.log('.$delete_id.')</script>';

Then show us the screenshot with the output.


Remove is not defined!



Hello! Seem like the remove statement isn’t being found on your isset call, let me explain how it should work:

I suppose you have your index.php file where you have a div or something like that with a id="remove". You have a Javascript file that executes a function everytime you press the remove button that triggers the js event remove. Now using AJAX (Asynchronous JavaScript And XML) you could either call a php file where you have the code related to remove your movies record from your db or do something else.

In that order, what I have done in a personal project:


  // Tons of html coded here ...
    <td id="your_movie_name_field">2017-01-01 12:00</td>
    // A table field that has a string (DATETIME)
    // Can be other html tags, the important thing is the id name.


// Everytime you click the button with the associated ID, the function inside click event will be triggered.
$("#your_button_id_here").click(function() {
  // You find the ID from your HTML document and store it as a variable.
  var example_var = $('#your_movie_name_field').html();
  // Now we execute the AJAX call using POST type and another php file.
    type: "POST",
    url: 'another_php_file.php',
    // We send the data "remove" to our php file.
    data: {
      remove: example_var
    // If everything was OK inside our php file, then the success event will be executed.
    success: function(response_data){
      // Do something here, like a alert saying "Movie remove successful".



  // Calling the data from jsfile.js
  if (isset($_POST['remove'])) {
    // Store your movie name (obtained using javascript).
    $random_var_name = $_POST['remove'];
    // Do something related to Database here.


You can checkout about how AJAX works here and here too!

Maybe there are better solutions for your problem, but so far this is what I would suggest to solve it. In addition, you said that the search tool works, you could check that there is a difference between your code to remove movies and the search in order to use the same procedure to remove them.