search

Home  >  Q&A  >  body text

Please tell me how to delete a record using native PHP PDO. It has been unsuccessful and I don’t know where the mistake is.

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

志远电脑小马哥志远电脑小马哥959 days ago936

reply all(2)I'll reply

  • 志远电脑小马哥

    志远电脑小马哥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); //这个是出错行。

    reply
    0
  • ABOUT

    Didn't you pass $conn into the delInfo method? Didn't you see an error saying that the exec member method could not be found?

    ABOUT · 2022-05-27 09:52:06
  • Cancelreply