1

I am using similar syntax in my blog. However, On my forum, nothing happens! This has been such an infuriating thing to tackle, as everything seems to be working exactly as my blog did. Here's my code I pass through and call the delete_post page

CHUNK FROM VIEWPOST.PHP

        while($row = mysqli_fetch_array($result)){    
            echo '<tr>';
                echo '<td class="postleft">';
                    echo date('F j, Y, g:i a', strtotime($row['forumpost_Date'])) . "<br>" .$row['user_Name']. "<br>" .$row['forumpost_ID'];
                echo '</td>';
                echo '<td class="postright">';
                    echo $row['forumpost_Text'];
                echo '</td>';
                if(isset ($_SESSION['loggedin']) && ($_SESSION['user_AuthLvl']) == 1){
                echo '<td class="postright">';
                    echo '<a class= "btn btm-default" href="#">Edit</a>';
                    echo '<a class= "btn btm-default" href="delete_post.php?forumpost_ID='.$row['forumpost_ID'].'">Delete</a>';
                echo '</td>';}
                else if(isset ($_SESSION['loggedin']) && ($_SESSION['user_ID']) == $row['forumpost_Author']){
                echo '<td class="postright">';
                    echo '<a class= "btn btm-default" href="#">Edit</a>';
                    echo '<a class= "btn btm-default" href="delete_post.php?forumpost_ID='.$row['forumpost_ID'].'">Delete</a>';
                echo '</td>';}
            echo '</tr>';
        }echo '</table>';

DELETE POST FUNCTION

<?php
include ('header.php');
include ('dbconnect.php');

//A simple if statement page which takes the person back to the homepage
//via the header statement after a post is deleted. Kill the connection after.

if(!isset($_GET['forumpost_ID'])){
    header('Location: index.php');
    die();
}else{
    delete('hw7_forumpost', $_GET['forumpost_ID']);
    header('Location: index.php');
    die();
}

/********************************************
delete function
**********************************************/
function delete($table, $forumpost_ID){
    $table = mysqli_real_escape_string($connectDB, $table);
    $forumpost_ID = (int)$forumpost_ID;
    $sql_query = "DELETE FROM ".$table." WHERE id = ".$forumpost_ID;
    $result = mysqli_query($connectDB, $sql_query);
}
?>

Now it is showing the ID's as intended, it just simply does not delete the post. It's such a simple Query, I don't know where my syntax is not matching up!

EDIT FOR DBCONNECT.PHP

<?php
    /*---------------------------------------
    DATABASE CONNECT PAGE
    A simple connection to my database to utilize
    for all of my pages!
    ----------------------------------------*/

    $host = 'localhost';
    $user = 'ad60';
    $password = '4166346';
    $dbname = 'ad60';

    $connectDB = mysqli_connect($host, $user, $password, $dbname);

    if (!$connectDB){
        die('ERROR:  CAN NOT CONNECT TO THE DATABASE!!!: '. mysqli_error($connectDB));
    }

    mysqli_select_db($connectDB,"ad60")  or  die("Unable to select database: ".mysqli_error($connectDB));
?>
B. Desai
  • 16,414
  • 5
  • 26
  • 47
broda
  • 37
  • 8

3 Answers3

2

Ok, I saw this and I would like to suggest the following:

In general

When you reuse code and copy paste it like you have done there is always the danger that you forget to edit parts that should be changed to make the code work within the new context. You should actually not use code like this.

Also you have hard coded configuration in your code. You should move up all the configuration to one central place. Never have hard coded values inside your functional code.

Learn more about this in general by reading up about code smell, programming patterns and mvc.

To find the problem

Now to fix your problem lets analyse your code starting with delete_post.php

First check if we actually end up inside delete_post.php. Just place an echo "hello world bladiebla" in top of the file and then exit. This looks stupid but since I can't see in your code if the paths match up check this please.

Now we have to make sure the required references are included properly. You start with the include functionality of php. This works of course, but when inside dbconnect.php something goes wrong while parsing your script it will continue to run. Using require would fix this. And to prevent files from loading twice you can use require_once. Check if you actually have included the dbconnect.php. You can do this by checking if the variables inside dbconnect.php exist.

Now we know we have access to the database confirm that delete_post.php received the forumpost_ID parameter. Just do print_r($_GET) and exit. Check if the field is set and if the value is set. Also check if the value is actually the correct value.

When above is all good we can go on. In your code you check if the forumpost_ID is set, but you do not check if the forumpost_ID has an actual value. In the above step we've validated this but still. Validate if your if statement actually functions by echoing yes and no. Then test your url with different inputs.

Now we know if the code actually gets executed with all the resources that are required. You have a dedicated file that is meant to delete something. There is no need to use a function because this creates a new context and makes it necessary to make a call and check if the function context has access to all the variables you use in the upper context. In your case I would drop the function and just put the code directly within the else statement.

Then check the following:

  1. Did you connect to the right database
  2. Is the query correct (echo it)
  3. Checkout the result of mysqli_query

Note! It was a while ago since I programmed with php so I assume noting from the codes behavior. This is always handy. You could check the php versions on your server for this could also be the problem. In the long run try to learn and use MVC. You can also use frameworks like codeigniter which already implemented the MVC design pattern.

botenvouwer
  • 4,334
  • 9
  • 46
  • 75
0

You have to declare $connectDB as global in function.

function delete($table, $forumpost_ID){
    global $connectDB;
    $table = mysqli_real_escape_string($connectDB, $table);
    $forumpost_ID = (int)$forumpost_ID;
    $sql_query = "DELETE FROM ".$table." WHERE id = ".$forumpost_ID;
    $result = mysqli_query($connectDB, $sql_query);
}

See the reference about variable scope here: http://php.net/manual/en/language.variables.scope.php

B. Desai
  • 16,414
  • 5
  • 26
  • 47
  • I've added that line of code to my function, (thank you!). However still no luck!!! – broda Apr 20 '18 at 06:24
  • check for errors. echo your query and try to run it in mysql directly – B. Desai Apr 20 '18 at 06:57
  • 1
    Not sure if encouraging people to use `global` is particularly good - https://stackoverflow.com/questions/12445972/stop-using-global-in-php – Nigel Ren Apr 20 '18 at 07:02
  • 2
    If you have some other errors they might be hard to spot, during development you should always enable error handling in your php.ini and php/mysql. I use this for my php development: `ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(-1); mysqli_report(MYSQLI_REPORT_ALL & ~MYSQLI_REPORT_INDEX);` at the start of your php script. Without that many errors and warnings might be silently ignored... – xander Apr 20 '18 at 07:26
0

please try to use below solution.

<?php
        include ('header.php');
        include ('dbconnect.php');

        //A simple if statement page which takes the person back to the homepage
        //via the header statement after a post is deleted. Kill the connection after.

        if(!isset($_GET['forumpost_ID'])){
            header('Location: index.php');
            die();
        }else{
            delete('hw7_forumpost', $_GET['forumpost_ID'], $connectDB);
            header('Location: index.php');
            die();
        }

        /********************************************
        delete function
        **********************************************/
        function delete($table, $forumpost_ID, $connectDB){
             $table = mysqli_real_escape_string($connectDB, $table);
             $forumpost_ID = (int)$forumpost_ID;
             $sql_query = "DELETE FROM ".$table." WHERE id = ".$forumpost_ID;
             $result = mysqli_query($connectDB, $sql_query);
        }
?>

I wish this solution work for you best of luck!

Kamlesh Solanki
  • 616
  • 5
  • 10