2017-04-22 89 views
0

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

我需要帮助如何做到这一点。当用户按下“管理帖子”按钮时,我想要一个输入字段,用户可以在其中键入Book_ID,并在“删除”按钮中单击该字段以将书籍从系统中删除。

现在,我无法将它设置到添加书籍的位置,它将其链接到登录的特定用户(不知道该如何操作),因此用户将能够删除任何书。我在这个项目上耗尽了时间,所以我现在不会为此担心。我只需要用户能够通过表格上的字段查看数据库中的所有书籍:Book_ID,ISBN,标题,作者 - 然后用户将Book_ID输入到输入字段中,单击“删除”按钮,然后书籍由用户从数据库中删除。

数据库名称:下一本书 表:书 领域: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> 

回答

0
<form method="post" action="delete.php"> 

<input type="text" placeholder="Enter the book ID to delete" name="getdeleteid"> 

<button type="submit" value="Delete book"> 

</form> 

PHP:

<?php 

$getdelete = $_POST['getdeleteid']; 

    $pdo = new PDO('mysql:host=yourhost;dbname=nextbook ','user','password'); 
    $statement = $pdo->prepare("DELETE FROM books WHERE book_ID = ".$getdelete.""); 
    $statement->execute(array(1)); 


?> 
+0

它说:“警告:mysqli_fetch_array()预计参数1被mysqli_result,在C中给出的对象:\ XAMPP \ htdocs中\ ITSS4312 \ mwa4 \ Manage_Posts.php上线93" 第93行是现在在这个第一行: ​​<? php echo $ row ['Book_id'];?> ​​<?php echo $ row ['ISBN'];?> ​​ ​​ ccny18

+0

我不知道,删除代码是好的,你的错误似乎是,你没有得到任何结果,很难看到错误,因为我看不到你的整个代码 – RzeIMz

+0

我试图有一个表显示表格,以便用户可以看到要删除的图书。我把“$查询=”选择*从'书籍'“;”删除SQL语句之上的语句,还是在if或之后? – ccny18

0

你应该向下突破你的脚本为多个部分,以使视图更容易使用。你也应该在自己的页面上放置所有的类,并使用自动加载器(spl_autoload_register()或类似的)来自动加载类。我已将所有内容放在一个看起来比实际更复杂的页面上。最后是有帮助的使用行为动词的形式告诉你的程序,你ARET试图做一些事情:

<?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>