用户在PHP中删除帖子/条目

时间:2017-04-22 00:11:33

标签: php html sql phpmyadmin

我正在设计一个简单的图书应用程序。我有用户登录,销售或购买图书,他们也有一些帐户设置,包括管理帖子,用户可以删除他们添加到系统中的图书。

我需要帮助来解决这个问题。当用户按下“管理帖子”按钮时,我想要一个输入字段,用户可以在其中键入Book_ID,并且"删除"按钮,他们可以单击它以从系统中删除该书。

现在,我无法将其设置为添加图书的位置,它将其链接到登录的特定用户(不知道如何操作),因此用户将能够删除任何书籍。我没有时间在这个项目上,所以我现在不用担心。我只需要用户能够通过表格中的字段查看数据库中的所有书籍:Book_ID,ISBN,Title,Author - 然后用户将Book_ID输入到输入字段,点击"删除&# 34;按钮和用户从数据库中删除该书。

数据库名称:nextbook 表:书籍 字段:book_ID,ISBN,作者,标题(想要查看这些)

以下是我认为与其他页面相似的代码模板。除此之外,我需要删除SQL放在某处:

<?php


if(isset($_POST['search']))
{
    $valueToSearch = $_POST['valueToSearch'];

    $query = "SELECT * FROM books";

    $search_result = filterTable($query);

}
else {
    $query = "SELECT * FROM books";
    $search_result = filterTable($query);
}

// function to connect and execute the query
function filterTable($query)
{
    $connect = mysqli_connect("localhost", "Admin", "Password", "nextbook");
    $filter_Result = mysqli_query($connect, $query);
    return $filter_Result;
}

?>

<!--Html -->

<!DOCTYPE html>
<html lang="en">
<head>

    <meta charset="UTF-8" >

    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <script src="http://ie7-js.googlecode.com/svn/version2.1(beta4)/IE9.js"></script>


    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">


    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
    <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>


    <style>
        table {
            border-collapse: collapse;
            width: 30%;
        }

        th, td {
            text-align: left;
            padding: 5px;
        }

        tr:nth-child(even){background-color: #f2f2f2}

        th {
            background-color: #007d5f;
            color: white;
        }
    </style>
    <link rel="stylesheet" href="NextBook1.css"/>


    </head>
    <body>

    <div data-role="page" id="Manage_Posts">
    <div data-role="header" data-theme="b">
        <h1>NextBook</h1>
        <a href="Login.php" data-icon="power" class="ui-btn-right" data-theme="a" data-mini="true">Sign Out</a>
    </div>


    <br>

    <div class="logo" align="center">
        <img src="Images/image1%20-%20Copy.PNG" width="100" height="100" ">
    </div>

    <div data-role="content" align="center">



        <!--<form action="View_Search_Results_Table.php" method="post" align="center"> -->
            <input type="text" name="deletepost" placeholder="Enter ISBN you want to delete">
            <input type="submit" name="delete" value="Delete Post"><br><br>

            <div style="overflow-x:auto;">
                <table border="1px solid black;" align="center">
                    <tr>
                        <th>Book ID</th>
                        <th>ISBN</th>
                        <th>Title</th>
                        <th>Author</th>

                    </tr>
            </div>


            <!-- populate table from mysql database -->
            <?php while($row = mysqli_fetch_array($search_result)):?>
                <tr>
                    <td><?php echo $row['Book_id'];?></td>
                    <td><?php echo $row['ISBN'];?></td>
                    <td><?php echo $row['Title'];?></td>
                    <td><?php echo $row['Author'];?></td>
                </tr>
            <?php endwhile;?>
            </table>


        <div data-role="footer" data-position="fixed" data-id="nav" data-theme="b">
            <div data-role="navbar">
                <ul>
                    <li><a href="Home_Page.php" data-icon="home" class="ui-btn-active ui-state-persist"></a></li>
                    <li><a href="#anylink" data-icon="alert"></a></li>
                    <li><a href="#anylink" data-icon="mail"></a></li>
                    <li><a href="Manage_User_Accounts.php" data-icon="gear"></a></li>
                </ul>
            </div>
        </div>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

您应该将脚本分成多个部分,以使视图更易于使用。此外,您应该在自己的页面中拥有所有类,并使用自动加载器(spl_autoload_register()或类似的)来自动加载类。我已将所有内容放在一个看起来比实际更复杂的页面上。最后,在表单中使用动作词来告诉你的程序你试图做某事是有帮助的:

<?php
/*
**  @description    It's helpful to have a class that just does some general "stuff"
**                  that all classes could potentially use
*/
class App
    {
        protected   static $singleton;

        public  function __construct()
            {
                if(!(self::$singleton instanceof \App))
                    self::$singleton    =   $this;

                return self::$singleton;
            }

        # Retrieve the $_POST array or a key from it
        public  function getPost($key=false)
            {
                if(!empty($key))
                    return (isset($_POST[$key]))? $_POST[$key] : false;

                return $_POST;
            }
    }
/*
**  @description    It's helpful to have a database class for consistent database retrieval and querying
*/
class Database extends \App
    {
        protected   static  $con;
        protected   $query;
        # Create and retrieve database connection
        public  function getConnection()
            {
                # Create connection if not already set
                if(!(self::$con instanceof \PDO))
                    self::$con  =   new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME,DB_USER,DB_PASS);
                # Return the connection
                return self::$con;
            }
        # Query database
        public  function query($sql,$bind=false)
            {
                # Bind parameters for public requests
                if(!empty($bind)) {
                    foreach($bind as $key=>$value) {
                        $bKey           =   ":{$key}";
                        $bArray[$bKey]  =   $value;
                    }
                }
                # Prepare sql
                if(!empty($bArray)) {
                    $this->query    =   $this->getConnection()->prepare($sql);
                    $this->query->execute($bArray);
                }
                else
                    # Do a straight query
                    $this->query    =   $this->getConnection()->query($sql);
                # Send back the object for chaining
                return $this;
            }
        # Use with the query to retrieve database results
        public  function getResults()
            {
                while($row = $this->query->fetch(\PDO::FETCH_ASSOC)) {
                    $new[]  =   $row;
                }

                return (!empty($new))? $new : false;
            }
    }

/*
**  @description    Because you are wanting to get database info, may as well extend the Database class
**                  and use it's querying features
*/
class Books extends Database
    {
        # Retrieve one or more books
        public  function getBook($id = false,$type='Book_id')
            {
                $id     =   trim($id);
                $sql    =   "SELECT * FROM `books`";
                if(!empty($id)) {
                    $sql        .=  " WHERE `{$type}` = :0";
                    $results    =   $this->getConnection()->query($sql,array($id))->getResults();
                    return (is_array($results) && count($results) == 1)? $results[0] : $results;
                }

                return $this->getConnection()->query($sql)->getResults();
            }
        # Delete book
        public  function deleteBook($id,$type='ISBN')
            {
                $this->getConnection()->query("DELETE FROM books WHERE `{$type}` = :0",array($id));
            }
    }

class View extends Database
    {
        public static function createSrc($path,$type='js')
            {
                if($type == 'js')
                    return '<script type="text/javascript" src="'.$path.'"></script>';
                elseif($type == 'css')
                    return '<link rel="stylesheet" href="'.$path.'" />';
            }
    }


# Should put these defines into a config.php file that you load at the top of every page
define('DB_HOST','localhost');
define('DB_NAME','nextbook');
define('DB_USER','root');
define('DB_PASS','');
session_start();

# Create instance of Books
$App    =   new Books();
# Creaet the book list (could be based on the search)
$search =   $App->getBook($App->getPost('search'));
# Check if the user is trying to delete a book
if($App->getPost('action') == 'delete_isbn') {
    $App->deleteBook($App->getPost('deletepost'));
}

?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" >
<?php echo View::createSrc('http://html5shiv.googlecode.com/svn/trunk/html5.js') ?>
<?php echo View::createSrc('http://ie7-js.googlecode.com/svn/version2.1(beta4)/IE9.js') ?>
<?php echo View::createSrc('http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css','css') ?>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<?php echo View::createSrc('http://code.jquery.com/jquery-1.11.1.min.js') ?>
<?php echo View::createSrc('http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js') ?>
<style>
table {
    border-collapse: collapse;
    width: 30%;
}

th, td {
    text-align: left;
    padding: 5px;
}

tr:nth-child(even){background-color: #f2f2f2}

th {
    background-color: #007d5f;
    color: white;
}
</style>
<link rel="stylesheet" href="NextBook1.css"/>
</head>
<body>
<div data-role="page" id="Manage_Posts">
    <div data-role="header" data-theme="b">
        <h1>NextBook</h1>
        <a href="Login.php" data-icon="power" class="ui-btn-right" data-theme="a" data-mini="true">Sign Out</a>
    </div><br>
    <div class="logo" align="center">
        <img src="Images/image1%20-%20Copy.PNG" width="100" height="100" />
    </div>
    <div data-role="content" align="center">
        <form action="" method="post" align="center">
            <input type="hidden" name="action" value="delete_isbn" />
            <input type="text" name="deletepost" placeholder="Enter ISBN you want to delete">
            <input type="submit" name="delete" value="Delete Post">
        </form>
        <br /><br />
        <table border="1px solid black;" align="center">
            <tr>
                <th>Book ID</th>
                <th>ISBN</th>
                <th>Title</th>
                <th>Author</th>
            </tr>
            <!-- populate table from mysql database -->
            <?php foreach($search as $row) { ?>
            <tr>
                <td><?php echo $row['Book_id'];?></td>
                <td><?php echo $row['ISBN'];?></td>
                <td><?php echo $row['Title'];?></td>
                <td><?php echo $row['Author'];?></td>
            </tr>
            <?php } ?>
        </table>
        <div data-role="footer" data-position="fixed" data-id="nav" data-theme="b">
            <div data-role="navbar">
                <ul>
                    <li><a href="Home_Page.php" data-icon="home" class="ui-btn-active ui-state-persist"></a></li>
                    <li><a href="#anylink" data-icon="alert"></a></li>
                    <li><a href="#anylink" data-icon="mail"></a></li>
                    <li><a href="Manage_User_Accounts.php" data-icon="gear"></a></li>
                </ul>
            </div>
        </div>
    </div>
</div>
</body>
</html>