我知道这一定只是一个小错误,但我找不到它。
我的功能:
function del_mysql($table,$id)
{
$id = $_GET['id'];
$exec = mysqli_query($con, "delete from $table where id = '$id'");
return $exec;
}
在代码中:
if ($_GET['action'] == 'delete')
{
del_mysql("awsome","$id");
}
如果在函数中使用:
$id = $_GET['id'];
echo $table;
echo $id;
我得到了正确的表和 ID。
有人看到这个错误了吗? 我已经删除了 $exec 和 return 部分,只留下 mysqli_query 命令。但不想工作。
最佳答案
问题在于,在 del_mysql 函数中,您引用了连接对象 $con,该对象不存在于该函数的范围内。要么将其作为参数传递到函数中,如下所示:
function del_mysql($table, $id, $con) {
或将其作为全局变量访问,如下所示:
function del_mysql($table, $id) {
global $con;
希望对您有所帮助。
问候, 拉尔夫
关于PHP:简单的 mysql 查询功能 - 无法让它工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20994760/