用PHP删除行 - 网页上的PDO

时间:2014-12-20 07:45:59

标签: php mysql pdo sql-delete

我试图在列出输入数据库的行的页面上使用PHP(PDO)从表中删除一行。我一直在修补delete.php代码,试图让它工作,但无济于事。我感谢任何帮助。

以下是我的代码:

listview.php      

   session_start(); 
   include_once('../includes/connection.php'); 
   include_once('../includes/events.php'); 
   $event = new Event; 
   $events =$event->fetch_all(); 


   if(isset($_SESSION['logged_in'])) { 
   //display index


   ?> 
   <html>
   <head>
<meta charset="utf-8">
<title>Welcome to the admin page</title>
</head>

<body>
  <div class="container">
     <h1>The List of Events</h1>

    <ol>
    <?php foreach ($events as $event) { ?> 
      <li> 

      <?php echo $event['event_name']; ?> 
      <?php echo $event['event_date']; ?>
      <?php echo $event['event_location']; ?>
      <?php echo $event['description']; ?>
      <?php echo $event['start_time']; ?>
      <?php echo $event['end_time']; ?>
       <?php echo $event['poc_name']; ?>
      <?php echo $event['poc_email']; ?>
      <?php echo $event['poc_number']; ?>  

       <!--edit/delete links--> 
       <a href="events.php?action=edit&event=<?php echo $event['event_id']; ?>">Edit</a>
       <a href="delete.php?id=<?php echo $event['event_id']; ?>">Delete</a>
       <!--end edit/delete links--> 

      </li>
     <?php } ?> 
    </ol>

  </div> 

</body>
</html>  




  <?php 
 } else {  
    if(isset($_POST['username'], $_POST['password'])) { 
       $username = $_POST['username']; 
       $password = $_POST['password']; 

       //check the fields in the login form
       if(empty($username) or empty($password)) { 
       $error = 'All fields are required'; 
       } else { 
         $query = $dbh->prepare("SELECT * FROM admin WHERE username = ? AND userpassword = ?");    
         $query->bindValue(1, $username); 
         $query->bindValue(2, $password); 

         $query->execute(); 

         $num = $query->rowCount(); 

         if($num == 1) { 
           //correct
           $_SESSION['logged_in'] = true; 
           header('Location: index.php'); 
           exit(); 

         } else { 
            //incorrect
            $error = 'Incorect details'; 
         } 

       } 

 } 

   ?> 
   <html>
<head>
<meta charset="utf-8">
<title>Squeegee Admin Login</title>
</head>

<body>

  <div class="container">
    <a href="index.php" id="logo">Squeegee Admin</a>
    <br/>  

    <?php if (isset($error)) { ?> 
      <small style="color:#aa000; "><?php echo $error; ?> </small>
    <?php } ?> 

    <form action="index.php" method="post" autocomplete="off"> 
       <input type="text" name="username" placeholder="Username" /> 
         <input type="password" name="password" placeholder="Password" />
     <input type="submit" value="Login" />
    </form>

  </div> 
</body>
</html>


 <?php } ?>  

连接

<?php
// mysql hostname
$hostname = 'localhost';
// mysql username
$username = 'root';
// mysql password
$password = '';
// Database Connection using PDO
try {
$dbh = new PDO("mysql:host=$hostname;dbname=squeegee", $username, $password);
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }
?>

events.php

    <?php 
    class Event {  

      //queries from database
      public function fetch_all() { 
        global $dbh; 

        $query = $dbh->prepare("SELECT * FROM events"); 
        $query->execute(); 

        return $query->fetchAll(); 
      } 

      //queries specific article via id 
      public function fetch_data($event_id) { 
        global $dbh;  
        $query = $dbh->prepare("SELECT * FROM events WHERE event_id = ? ");
        $query->bindValue(1, $event_id);  
        $query->execute(); 

        return $query->fetch(); 
      } 
    }  


    ?> 

delete.php

<?php
    include('../includes/connection.php');
$event_id=$_GET['event_id'];
$result = $dbh->prepare("DELETE FROM events WHERE event_id= :event_id");
$result->bindParam(':event_id', $event_id);
$result->execute();
header("location: index.php");

?> 

1 个答案:

答案 0 :(得分:1)

正如您的问题所示,您似乎正在访问错误的索引。

在您的链接中,它定义为id

<a href="delete.php?id=<?php echo $event['event_id']; ?>">Delete</a>
                  // ^

但随后在您的PHP文件中访问:

$event_id=$_GET['event_id'];

必须是:$event_id = $_GET['id'];

您可以在锚点中将网址更改为?event_id,或更改PHP $event_id = $_GET['id'];中的数组索引。重要的是它们必须匹配。