在PHP中发生非对象错误时调用成员函数exec() [英] Call to a member function exec() on a non-object error in PHP

查看:54
本文介绍了在PHP中发生非对象错误时调用成员函数exec()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function add_member($idmembers, $firstname, $lastname,  $email, $phone, $level, $password) {
global $db;
$query = "INSERT INTO members
             (idmembers,  first, last, email, phone, level, password)
          VALUES
                ($idmembers,  '$firstname', '$lastname', '$email', '$phone', '$level', '$password')";
var_dump($query);
$db->exec($query);

}

此代码为我提供了此错误和var转储中的信息:

This code gives me this error and information from the var dump:

string(224)"INSERT INTO成员(idmembers,first,last,email,电话,级别,密码)VALUES(,'Gina','Hill','hill@me.com','7778889898','m','803591803e3d6e646cd3ee4a35fee6dd')致命错误:在第26行的J:\ XAMPP \ htdocs \ WEBSITE \ register.php中以null调用成员函数exec()

string(224) "INSERT INTO members (idmembers, first, last, email, phone, level, password) VALUES (, 'Gina', 'Hill', 'hill@me.com', '7778889898', 'm', '803591803e3d6e646cd3ee4a35fee6dd')" Fatal error: Call to a member function exec() on null in J:\XAMPP\htdocs\WEBSITE\register.php on line 26

我已经检查过将查询添加到数据库中,以确保查询正常运行并且可以正常工作.

I've checked adding the query to the database to make sure the query is working and it works fine.

推荐答案

我怀疑 $ db 不是对象(如果它实际上不是NULL)或 $ db 没有名为 exec 的函数.

I suspect that $db is either not an object (if it's not actually NULL), or $db doesn't have a function named exec.

我建议您验证与数据库的连接是否成功,并确认 $ db 是连接对象.确认 exec 是该对象的有效函数.

I recommend you verify that your connection to the database is successful, and verify that $db is the connection object. Verify that exec is a valid function for the object.

此外,您的代码似乎容易受到 SQL注入的攻击.(我们看不到传递给函数的参数已被转义"以安全地包含在SQL文本中.)我们更希望看到具有 bind占位符<的准备好的语句/strong>.

Also, your code appears to be vulnerable to SQL Injection. (We don't see that the arguments passed to the function have been "escaped" to be safe for inclusion in SQL text.) We much prefer to see a prepared statements with bind placeholders.

这篇关于在PHP中发生非对象错误时调用成员函数exec()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆