• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

为什么这个无法删除选中的数据?

php 搞代码 4年前 (2022-01-23) 16次浏览 已收录 0个评论
文章目录[隐藏]

哪里不对呢?

<code>    <title></title>    <style type="text/css">    </style><body><?php        header('content-type:text/html;charset=utf-8;');        $pdo=new PDO("mysql:host=localhost;dbname=t1","root","<i>*本5文来源gaodai$ma#com搞$$代**码)网@</i><img>搞代码gaodaima</img>");       $stmt=$pdo->prepare("select name from class");       $stmt->execute();       $res=$stmt->fetchall(PDO::FETCH_ASSOC);       foreach($res as $v){           echo '<span>'.$v['name'].''.'</span>';       }?><button type="submit">删除</button><?phpif(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']));        }    ?></code>

回复内容:

哪里不对呢?

<code>    <title></title>    <style type="text/css">    </style><body><?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'].''.'</span>';       }?><button type="submit">删除</button><?phpif(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']));        }    ?></code>

你这代码,写的比较奇怪。
先说为什么不能删除:
你的input的框 没有 value 属性。所以你的$_POST[‘name’]拿不到值。
你可以改成

<code>echo '<span>'.$v['name'].'</span>';</code>

然后我看你的代码,你是想做多选删除吧?那么你需要post提交一个数组到php,那么你可以修改input的name属性:

<code>echo '<span>'.$v['name'].'</span>';</code>

然后用php接到的post是这样的:

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

这时你需要处理这个$name。更改你的sql语句为

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

比如下面:

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

然后,你的判断is_post的代码最好在输出列表之前,否则,先给你输出,后删除,看不到效果。

这是刚入门吗?


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:为什么这个无法删除选中的数据?

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址