Php 运行CMS程序时出现致命错误
我在运行程序时遇到此错误 致命错误:未捕获错误:调用未定义的方法 CarModel::InsertCar()在 C:\xampp\htdocs\CoffeeWebsite\Controller\CarController.php:119堆栈 跟踪:#0 C:\xampp\htdocs\CoffeeWebsite\CarAdd.php(43): CarController->InsertCar()被抛出 C:\xampp\htdocs\CoffeeWebsite\Controller\CarController.php,第119行Php 运行CMS程序时出现致命错误,php,html,mysql,database,webpage,Php,Html,Mysql,Database,Webpage,我在运行程序时遇到此错误 致命错误:未捕获错误:调用未定义的方法 CarModel::InsertCar()在 C:\xampp\htdocs\CoffeeWebsite\Controller\CarController.php:119堆栈 跟踪:#0 C:\xampp\htdocs\CoffeeWebsite\CarAdd.php(43): CarController->InsertCar()被抛出 C:\xampp\htdocs\CoffeeWebsite\Controller\CarCon
//CarModel和CarController的源代码
请详细说明我的评论
首先,要使用准备好的语句。以下是一个例子:
/* Connection */
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* Check connection */
if ($mysqli->connect_errno)
{
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
if($stmt = $mysqli->prepare("UPDATE..."))
{
/* Bind your params */
$stmt->bind_param('ss', $username, $password);
/* Error handling if execute failed */
if (!$stmt->execute())
{
die('execute() failed: ' . htmlspecialchars($stmt->error));
}
}
else
{
/* Error handling if Prepare failed */
die('prepare() failed: ' . htmlspecialchars($DBConnect->error));
}
$stmt->close();
阅读更多关于返回结果的信息
现在,由于您希望从函数中传入PerformQuery函数不知道的参数,因此您需要动态生成Bind参数,以便使用prepared语句。我已经做了类似的事情,使用反射动态生成绑定参数
如果将Args值传递给PerformQuery函数,则可能会有一个如下所示的函数:
public function PerformQuery($sql, $args = null)
{
/* Connection */
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* Check connection */
if ($mysqli->connect_errno)
{
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
if($stmt = $mysqli->prepare($sql))
{
/* Bind your params dynamically */
if (isset($args))
{
$method = new \ReflectionMethod('mysqli_stmt', 'bind_param');
$method->invokeArgs($stmt, $this->refValues($args));
}
/* Error handling if execute failed */
if (!$stmt->execute())
{
die('execute() failed: ' . htmlspecialchars($stmt->error));
}
}
else
{
/* Error handling if Prepare failed */
die('prepare() failed: ' . htmlspecialchars($mysqli->error));
}
$stmt->close();
}
要使动态绑定正常工作,还需要以下函数
private function refValues($arr)
{
if (strnatcmp(phpversion(),'5.3') >= 0) //Reference is required for PHP 5.3+
{
$refs = array();
foreach($arr as $key => $value)
$refs[$key] = &$arr[$key];
return $refs;
}
return $arr;
}
现在,通过其他方法,例如DeleteCar,您可以按如下方式传入查询和参数:
public function DeleteCar($id)
{
$query = "DELETE FROM car WHERE id = ?"; // ? to show where mysqli will bind
$args = array('i', $id); // i means an int
$this->PerformQuery($query, args);
}
使用预先准备好的语句将使您的代码更加安全,并且动态绑定Preform查询函数中的变量意味着您不必完全重构代码来传递连接,这样您就可以使用mysqli\u real\u escape\u字符串
祝你好运:)你在InsertCar函数上面有2个}
这是关闭CarModel类非常感谢…但是我得到了这个错误警告:mysqli\u real\u escape\u string()需要2个参数,1个在第95行的C:\xampp\htdocs\CoffeeWebsite\Model\CarModel.php中给出,这个警告是:mysqli\u query()至少需要2个参数,1个在C:\xampp\htdocs\CoffeeWebsite\Model\CarModel.php第132行中给出。在CarModel类的InsertCar函数中,您使用的是mysqli\u real\u escape\u字符串的过程样式,这需要2个参数,链接和字符串。因此,您需要将连接传递到InsertCar并使用它。理想情况下,您希望重构您的代码,使您能够1。使用MySQLi准备的语句。2.您希望将查询和参数传递到PerformQuery中,并在其中动态生成perpared语句,因为这是连接/和查询的地方。
public function DeleteCar($id)
{
$query = "DELETE FROM car WHERE id = ?"; // ? to show where mysqli will bind
$args = array('i', $id); // i means an int
$this->PerformQuery($query, args);
}