Ask for advice on native PHP PDO deleting a record has been unsuccessful. I don’t know where the mistake is. I have been troubled for a long time. Please give me some advice.
You need to delete a piece of data through GET,
<a href="localhost/admin/cmd.php?act=delInfo&t=link&id=60"> Delete</ a>
<?php //连接数据库 $servername = 'localhost'; $charset = 'utf8mb4'; $username = 'sa'; $password = '123456'; $dbname='touying'; $conn = new PDO("mysql:host=$servername;dbname=$dbname;charset=$charset", $username, $password); $act=$_GET["act"]; switch ($act) { case 'AdminLogin': AdminLogin(); break; case 'delInfo': delInfo(); break; default:; } // get请求删除信息,localhost/admin/cmd.php?act=delInfo&t=link&id=60 // 参数中的 act=操作的方法, t=link是要操作的表名link,id=60 是要操作的id. Function delInfo(){ $t=$_GET["t"]; $tid=$_GET["id"]; $sql="DELETE From ".$t." where id=".$id; $count=$conn->exec($sql); print("Deleted $count rows.\n"); } ?>
The result is wrong, the error message is:
Fatal error: Uncaught Error: Call to a member function exec() on null in D:\phpstudy_pro\WWW\admin\cmd. php:32 Stack trace: #0 D:\phpstudy_pro\WWW\admin\cmd.php(22): delInfo() #1 {main} thrown in D:\phpstudy_pro\WWW\admin\cmd.php on line 32
志远电脑小马哥2022-05-11 20:48:01
$sql="DELETE From ".$t." where id=".$tid;
The id here is an input error. Normally it should be tid. The problem is not here. After the interruption, the sql is normal. DELETE From link where id=69 The database has this record.
$count=$conn->exec($sql); //这个是出错行。