0

I have a list of favorite cars which i have added to each favorite car a checkbox for letting the user to remove the favorite car from his favorite car list. The problem is that the checkbox is working in a different way: If I check any car (1st, second.. last or multiple cars) and after hit submit the car that will get removed is the last one added instead of removing the selected one. If I check multiple cars, happens same thing, removes only the last car added.

PHP

public function GetFavoriteCars() {
  include("inc/membersite_config.php");
  $email = $fgmembersite->UserEmail(); // this is how I take the e-mail of the 
  global $base_path;
  $FavoriteCars = $this->QueryResult("SELECT * FROM favoritecar WHERE email='$email'"); 
    if (count($FavoriteCars)) {
        $mystring='http://';
    echo '<form action="" class="deletebutton" method="post">';
    echo '<input type="submit" name="deletebtn" id="deletebtn" value="Submit">';
    echo '<div class="roster_slideri-login">';
        foreach ($FavoriteCars as $FavoriteCar) {
      $carlink = $FavoriteCar->favoritecarlink;
      echo '<div class="car-info-col-login">';
      echo '<input type="checkbox" name="checkbox" value="'.$carlink.'" class="checkbox-login">';
      $val=strpos($FavoriteCar->favoritecarimg,$mystring);
    if ($val !== false) {
      if($FavoriteCar->favoritecarimg!='') {
                echo '<a href="'.$base_path.'detail-page_'.$FavoriteCar->favoritecarlink.'">';
                echo '<img src="'.$FavoriteCar->favoritecarimg.'" alt="'.$FavoriteCar->favoritecartitle.'" width="160" height="120" />';
        echo '</a>';
                echo '<div class="name">'.substr($FavoriteCar->favoritecartitle,0,20).'</div>';
        echo '</div>'; //car-info-col-login
              }
            } else {
    echo '<a href="'.$base_path.'detail-page_'.$FavoriteCar->favoritecarlink.'">';
            echo '<img src="'.$base_path.'uploads/no-img.jpg" alt="'.$FavoriteCar->favoritecartitle.'" width="160" height="120" />';
    echo '</a>';
            echo '<div class="name">'.substr($FavoriteCar->favoritecartitle,0,20).'</div>';
            echo '</div>'; 
            } 
        }
    echo '</form>';
        if (isset($_POST["checkbox"])) {
      $this->QueryResult("DELETE from favoritecar WHERE email='$email' AND favoritecarlink='$carlink'");
      echo '<script type="text/javascript">alert("Car had been deleted");</script>';
        }
      echo '</div>'; // div roster_slideri-login
    }
}

Explaning:

$email = $fgmembersite->UserEmail(); - this is how I take the e-mail of the current logged in user. It will echo "email_of_logged_in_user@domain.com"

QueryResult is a custom function that looks like this. I usually use it for SELECTING purposes but it seams that is working for deleting purposes too.

abstract class DBDetails {

    protected $link = NULL;

    protected function connector() {
        global $DBHOSTNAME;
        global $DBUSERNAME;
        global $DBPASSWORD;
        global $DBNAME;
        $this->link = mysqli_connect($DBHOSTNAME, $DBUSERNAME, $DBPASSWORD, $DBNAME) or die("Can't connect to MySQL server on localhost");
    }

      protected function close() {
        mysqli_close($this->link);
      }
    }

     abstract class N2 extends DBDetails {
      public function QueryResult($strQuery) {
        $this->connector();
        $query = mysqli_query($this->link, $strQuery);
        $arr = array();
        if ($query) {
            while ($result = mysqli_fetch_object($query)) {
                array_push($arr, $result);
            }
        }
        $this->close();
        return $arr;
      }   
    }

Expected output

When I check the checkbox of a car, it should delete only that car. If I check the checkboxes of multiple cars, should delete the specific cars that I checked.

Please help, I am quite a noob in checkboxes. I have checked lots of questions from here, but did not find my answer.

Jenz
  • 8,280
  • 7
  • 44
  • 77
user3140607
  • 303
  • 4
  • 19

1 Answers1

1

In this line :

  echo '<input type="checkbox" name="checkbox" value="'.$carlink.'" class="checkbox-login">';
                               --------------

When using multiple checkboxes with same name , you would need to include [] in the name :

  echo '<input type="checkbox" name="checkbox[]" value="'.$carlink.'" class="checkbox-login">';
                               ----------------

Then $_POST["checkbox"] will be an array and you can use foreach on it to get all the checked values .

if( isset( $_POST["checkbox"] ) )
{
    foreach( $_POST["checkbox"] as $value )
    {
        /* $value contains $carlink */

        echo $value;    // For test purpose

        /* Sanitize and use it to identify and delete the corresponding row */
    }
}

( Rather than name="checkbox[]" it might be better to choose another name . )

Uours
  • 2,517
  • 1
  • 16
  • 21
  • and the `foreach` how would it work? I understand what you are saying but don't know where to start. – user3140607 Feb 25 '14 at 10:53
  • I tried doing `if(isset( $_POST["checkbox"])) { foreach( $_POST["checkbox"] as $value ) { $this->QueryResult("DELETE from favoritecar WHERE email='$email' AND favoritecarlink='$carlink'"); echo ''; } }` but still not working. Same thing. I really appreciate your efforts. Could you please help me based on my code, I am yawned today :(. – user3140607 Feb 25 '14 at 11:08
  • added the echo inside the foreach and selected the second car and resulted `DELETE from favoritecar WHERE email='info@email.com' AND favoritecarlink='1565924.html'`. 1565924.html is not the second car, is still the last car. So it's taking still the last car. Also ran it manually in mysql and it deleted properly, but the last car. So it's still not detecting the right checked checkbox. – user3140607 Feb 25 '14 at 11:38
  • 1
    In the `DELETE` query , instead of `$carlink` use `$value` . `$value` is the variable used in foreach . ( `$carlink` is getting the last car link due to which the last car link is getting deleted ) . That was my mistake , I have updated my answer . – Uours Feb 25 '14 at 12:01
  • it's working now, one more thing. It's much appreciat6ed I need to refresh the page manually in order to see the results, why would this happen? – user3140607 Feb 25 '14 at 12:22
  • 1
    You can try moving the Deletion related code to top just above `$FavoriteCars = $this->QueryResult ...` and below `$email = $fgmembersite->UserEmail();` . – Uours Feb 25 '14 at 12:48