Home > Backend Development > PHP Tutorial > Why can't this delete the selected data?

Why can't this delete the selected data?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-06 13:52:30
Original
1075 people have browsed it

What’s wrong?

<code><!DOCTYPE html>
<html>
<head>
    <title></title>
    <style type="text/css">
    </style>
</head>
<body>
<form method="post">
<?php
        header('content-type:text/html;charset=utf-8;');
        $pdo=new PDO("mysql:host=localhost;dbname=t1","root","");
       $stmt=$pdo->prepare("select name from class");
       $stmt->execute();
       $res=$stmt->fetchall(PDO::FETCH_ASSOC);
       foreach($res as $v){
           echo '<span>'.$v['name'].'<input id="ipt3" type="checkbox" name="name">'.'</span>';
       }
?>
<button type="submit">删除</button>
</form>
<?php
if(isset($_POST['name'])){
        header('content-type:text/html;charset=utf-8;');
        $pdo=new PDO("mysql:host=localhost;dbname=t1","root","");
        $stmt2=$pdo->prepare("delete from class where name=?");
        $stmt2->execute(array($_POST['name']));    
    }
    ?>
</body>
</html>
</code>
Copy after login
Copy after login

Reply content:

What’s wrong?

<code><!DOCTYPE html>
<html>
<head>
    <title></title>
    <style type="text/css">
    </style>
</head>
<body>
<form method="post">
<?php
        header('content-type:text/html;charset=utf-8;');
        $pdo=new PDO("mysql:host=localhost;dbname=t1","root","");
       $stmt=$pdo->prepare("select name from class");
       $stmt->execute();
       $res=$stmt->fetchall(PDO::FETCH_ASSOC);
       foreach($res as $v){
           echo '<span>'.$v['name'].'<input id="ipt3" type="checkbox" name="name">'.'</span>';
       }
?>
<button type="submit">删除</button>
</form>
<?php
if(isset($_POST['name'])){
        header('content-type:text/html;charset=utf-8;');
        $pdo=new PDO("mysql:host=localhost;dbname=t1","root","");
        $stmt2=$pdo->prepare("delete from class where name=?");
        $stmt2->execute(array($_POST['name']));    
    }
    ?>
</body>
</html>
</code>
Copy after login
Copy after login

Your code is rather strange.
First let’s talk about why it cannot be deleted:
Your input box does not have a value attribute. So your $_POST['name'] cannot get the value.
You can change it to

<code>echo '<span>'.$v['name'].'<input type="checkbox" name="name" value="'.$v['name'].'"></span>';
</code>
Copy after login

Then I look at your code. Do you want to delete multiple selections? Then you need to post an array to PHP, then you can modify the name attribute of the input:

<code>echo '<span>'.$v['name'].'<input type="checkbox" name="name[]" value="'.$v['name'].'"></span>';
</code>
Copy after login

Then the post received using php is like this:

<code>$name = $_POST['name'];//array(name1,name2....)
</code>
Copy after login

At this time you need to deal with this $name. Change your sql statement to

<code>delete from class where name in (name1,name2,name3...)
</code>
Copy after login

For example:

<code>$sql = "delete from class where name in (" . trim(str_repeat('?,',count($name)),',') . ")";
$stmt2 = $pdo->prepare($sql);
$stmt2->execute($name);
</code>
Copy after login

Then, your code for judging is_post is best before outputting the list. Otherwise, you will be output first and then deleted, and you will not see the effect.

Are you just getting started?

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template