我的代码发生了什么?

时间:2018-04-20 05:55:12

标签: php sql mysqli

我在博客中使用了类似的语法。但是,在我的论坛上,没有任何反应!这一直是一个令人愤怒的事情,因为一切似乎都像我的博客一样。这是我传递的代码并调用delete_post页面

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>';

删除帖子功能

<?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);
}
?>

现在它按预期显示ID,它只是不删除帖子。这是一个如此简单的查询,我不知道我的语法不匹配!

编辑 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));
?>

3 个答案:

答案 0 :(得分:2)

好的,我看到了这个,我想建议如下:

一般

当您重复使用代码并复制粘贴它时,您总是存在忘记编辑应该更改的部分以使代码在新上下文中工作的危险。你实际上不应该使用这样的代码。

您的代码中也有硬编码配置。您应该将所有配置上移到一个中心位置。从不在功能代码中包含硬编码值。

通过阅读代码异味,编程模式和mvc ,了解更多相关信息。

找到问题

现在要解决您的问题,请分析以delete_post.php

开头的代码

首先检查我们是否真的在delete_post.php内结束。只需将echo "hello world bladiebla"放在文件顶部,然后退出。这看起来很愚蠢但是因为我在代码中看不到路径是否匹配,请检查这个。

现在我们必须确保正确包含所需的引用。你从php的include功能开始。这当然有效,但当解析你的脚本时,dbconnect.php内部出现问题时,它将继续运行。使用require可以解决这个问题。要防止文件加载两次,您可以使用require_once。检查您是否确实包含dbconnect.php。您可以通过检查dbconnect.php中的变量是否存在来执行此操作。

现在我们知道我们有权访问数据库,确认delete_post.php已收到forumpost_ID参数。只需print_r($_GET)然后退出。检查字段是否设置为 ,如果设置了值 。同时检查值 实际上是否为正确值

如果以上都很好,我们可以继续。在您的代码中,检查是否设置了forumpost_ID,但是您没有检查forumpost_ID是否具有实际值。在上面的步骤中,我们已经验证了这一点,但仍然如此。验证您的if  语句实际上通过回显yes和no来起作用。然后使用不同的输入测试您的网址。

现在我们知道代码是否实际上是用所有必需的资源执行的。您有一个专门用于删除内容的文件。不需要使用函数,因为这会创建一个新的上下文,并且必须进行调用并检查函数上下文是否可以访问您在上层上下文中使用的所有变量。在您的情况下,我将删除该函数,并将代码直接放在else语句中。

然后检查以下内容:

  1. 您是否连接到正确的数据库
  2. 查询是否正确(回显)
  3. 查看mysqli_query
  4. 的结果

    请注意!不久前我用php编程,所以我假设从代码行为中注意到。这总是很方便。您可以检查服务器上的php版本,这也可能是问题所在。从长远来看,尝试学习和使用MVC。您还可以使用已经实现MVC设计模式的codeigniter之类的框架。

答案 1 :(得分:0)

您必须将$connectDB声明为全局函数。

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);
}

请参阅此处有关变量范围的参考: http://php.net/manual/en/language.variables.scope.php

答案 2 :(得分:0)

请尝试使用以下解决方案。

<?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);
        }
?>

我希望这个解决方案能为你带来好运!