-1

Data is not being deleted from the database. I can add and view.

If I put print_r($submissions); I can see my data. E.g. Array ( [0] => 2 ) The $count++; is always displaying 0 unless I put the count outside of the if statement. The echo "we got an error <br>"; will show as many times as there are submissions to be removed. I tested the SQL query and that works fine. Not sure where to go with this now so any help appreciated. Related code below:

Model:

function deleteSubmission($conn,$submission_id)
{
    $query = "DELETE FROM submissions WHERE submission_id=:submission_id";
    $stmt = $conn->prepare($query);
        $stmt->bindValue(':submission_id',$submission_id);
    $affected_rows = $stmt->execute();
    if($affected_rows==1)
    {
            //echo $affected_rows;
            echo ($affected_rows) ? "true" : "false";
            echo " working ";
        return true;
    }else{
        //echo $affected_rows;
            echo ($affected_rows) ? "true" : "false";
            echo " not working ";   
            return false;
    }
}

Controller:

//they didn't submit the form
if(!isset($_POST["deleteSubmissionsBtn"]))
{
    header("Location:all-submissions-delete.php");
    exit;
}

$msg;
//validate
if(isset($_POST['submissions']))
{
    $submissions=$_POST['submissions'];
    $conn=getConn();
    $count=0;
    foreach ($submissions as $submission_id) 
    {   


        if(deleteSubmission($conn,$submission_id))
        {
            $count++;
        }
                else{
                    echo "we got an error <br>";
                }

    }
        print_r($submissions); 
        //echo $submission_id;

    $conn=NULL;
    $msg="Successfully deleted $count submissions";
}else{
    $msg="You need to select some submissions to delete";
}

include("views/delete-feedback-view.php")

?>

View:

echo "<form action='delete-submissions.php' method='POST'>";
foreach($allSubmissions as $submission)
{
    echo "<div>";
    echo "<label>".$submission->title."</label>";
    echo "<input type='checkbox' value='".$submission->submission_id."' name='submissions[]'/>";
    echo "</div>";
}
echo "<input type='submit' name='deleteSubmissionsBtn' value='delete submissions'>";
echo "</form>";
tereško
  • 58,060
  • 25
  • 98
  • 150
Dan
  • 951
  • 1
  • 23
  • 46

1 Answers1

0

Thanks tilz0R and Magnus Eriksson.

I checked for errors as suggested and got Array ( [0] => 00000 [1] => [2] => )

I added PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING and this is where I found my answer....

PDOStatement::execute(): SQLSTATE[42000]: Syntax error or access violation: 1142 DELETE command denied to user ...

I now know the issue and can go about fixing it. Thanks again!

Dan
  • 951
  • 1
  • 23
  • 46