Home  >  Article  >  Backend Development  >  Why can't I delete the data?

Why can't I delete the data?

WBOY
WBOYOriginal
2016-08-18 09:15:431477browse

I just want to query on this page and then delete a certain piece of data on this page. Now I can query it but I can’t delete it. What’s written wrong?

<code>

<div id="div1">
 <form method="post">
    <input type="text" name="num">
    <button type="submit">提交</button>
 </form>
</div>

<div id="div2">
  <form method="post">
    <?php
      $pdo=new PDO("mysql:host=localhost;dbname=t1","root","");
      $num=$_POST['num'];
      $res=$pdo->query("SELECT * FROM class WHERE num=$num");
      foreach($res as $v){
        echo '<span name="cm">'.$v['comment'].'</span>'.'<button  type="submit">删除</button>'.'<br />';
      }
    ?>
  </form>
</div>

<?php
$pdo2=new PDO("mysql:host=localhost;dbname=t1","root","");
$cm=$_POST['cm'];
$sql="delete from table class where comment=$cm";
$res2=$pdo2->exec($sql);
?>
</code>

Reply content:

I just want to query on this page and then delete a certain piece of data on this page. Now I can query it but I can’t delete it. What’s written wrong?

<code>

<div id="div1">
 <form method="post">
    <input type="text" name="num">
    <button type="submit">提交</button>
 </form>
</div>

<div id="div2">
  <form method="post">
    <?php
      $pdo=new PDO("mysql:host=localhost;dbname=t1","root","");
      $num=$_POST['num'];
      $res=$pdo->query("SELECT * FROM class WHERE num=$num");
      foreach($res as $v){
        echo '<span name="cm">'.$v['comment'].'</span>'.'<button  type="submit">删除</button>'.'<br />';
      }
    ?>
  </form>
</div>

<?php
$pdo2=new PDO("mysql:host=localhost;dbname=t1","root","");
$cm=$_POST['cm'];
$sql="delete from table class where comment=$cm";
$res2=$pdo2->exec($sql);
?>
</code>

First, echo your delete sql and see if it is the result you want
Then go to the database to execute it and see if there is any effect.
Then there are still problems, we are troubleshooting

$cm is written like this '{$cm}'

There is no input field in your deletion, so there is no value submitted to the form, so nothing is deleted

Statement:
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