2017-04-22 92 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>