使用PHP删除行-网页上的PDO [英] Delete row with PHP - PDO on webpage

查看:61
本文介绍了使用PHP删除行-网页上的PDO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHP(PDO)从页面上删除表中的行,该页面列出了输入数据库的行.我一直在修改delete.php代码以尝试使其正常运行,但无济于事.感谢您的帮助.

I am trying to delete a row from a table using PHP (PDO) on a page listing the rows entered into the database. I've been tinkering with the delete.php code to try to make it work but to no avail. I appreciate any help.

下面是我的代码:

listview.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

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

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

?> 

推荐答案

从您的问题出发,看来您正在访问错误的索引.

As your question stands, it seems you're accessing the wrong index.

在您的链接中,其定义为id:

In your link it is defined as id:

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

但是随后在您的PHP文件中以以下方式访问:

But then accessed in your PHP file as:

$event_id=$_GET['event_id'];

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

在锚中将URL更改为?event_id或在PHP $event_id = $_GET['id'];中更改数组索引.重要的是它们必须匹配.

Either you change your url as ?event_id in the anchor or change the array index in your PHP $event_id = $_GET['id'];. The important things is they must match.

这篇关于使用PHP删除行-网页上的PDO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆