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