gpt4 book ai didi

php - 在数据库中找不到记录

转载 作者:行者123 更新时间:2023-11-29 05:02:41 25 4
gpt4 key购买 nike

我有以下问题:我在 url 中收到一个 GET 变量。如果变量 GET 到达,我将变量的内容发送到我的 Controller 。

我的 Controller 首先获取整个“sales”表,然后我在一列中查找与 GET 变量内容相同的记录。最后,我更新了我找到的那条记录的状态。

但是没有任何反应,我也不知道我做错了什么。

我留下代码:

接收变量 GET 的 PHP 文件:

if(isset( $_GET['number'])){

$number = $_GET['number'];

$response = CartController::ctrShowSales($number);

echo $response;

}

PHP Controller :

static public function ctrShowSales($number){

$table = "sales";

$respuesta = CartModel::mdlShowSales($table);

$find = 0;

foreach ($response as $key => $value) {

if ($value["number"] == $number) {

$find = 1;
$id = $value["id"];

break;

}

}

if ($find == 1){

$response2 = CartModel ::mdlUpdateRecord($table, $id);
return $response2;

} else { return "Did not find";}

}

PHP 模型:

static public function mdlShowSales($table){

$stmt = Conection::conect()->prepare("SELECT * FROM $table");

$stmt -> execute();

return $stmt -> fetch();

$stmt -> close();

$tmt =null;

}

static public function mdlUpdateRecord($table, $id) {

$stmt = Conection::conect()->prepare("UPDATE $table SET status = :status WHERE $id = :$id");

$stmt->bindParam(":id", $id, PDO::PARAM_INT);
$stmt->bindParam(":status", "Verified", PDO::PARAM_STR);

if($stmt -> execute()){

return "ok";

}else{

return "error";

}

$stmt -> close();

$stmt = null;

}

最佳答案

除了其他答案之外,我还会将这个简单的方法添加到您的模型中,

protected static $tables  = ['sales'];

final static public function ckTable($table){
if(false !== ($index = array_search($table, static::$tables, true))){
return $tables[$index]; //return your table value
}
throw new Exception('Unknown Table');
}

static public function mdlShowSales($table){
//here you can clearly see the table is being handled
$safeTable = self::ckTable($table); //use a different var here


$stmt = Conection::conect()->prepare("SELECT * FROM $safeTable");
....

//or $stmt = Conection::conect()->prepare("SELECT * FROM ".self::ckTable($table));
}

现在你只知道你在你的 Controller 中硬编码了这个:

$table = "sales";

只需要有一天在 Controller 中犯这个错误

//here you cannot tell if this is safe to do or not as you cannot see how the query is done.
static public function somepage($table){
$respuesta = CartModel::mdlShowSales($table);
}

即使您准备了查询,您也会对 SQL 注入(inject)持开放态度。

现在这不太可能发生,我们应该让这成为不可能。

此外,这基本上就是您正在做的:

//everything under PHP Controller can be done with this sql:
SELECT id FROM sales WHERE number = :number LIMIT 1
/*
SELECT * FROM sales
foreach ($response as $key => $value) {
if ($value["number"] == $number) { //-- WHERE number = :number
$find = 1;
$id = $value["id"]; //-- SELECT id
break; //-- LIMIT 1
}
}
*/

//mdlUpdateRecord
UPDATE sales SET status = :status WHERE id = :id

为什么不这样做

UPDATE sales SET status = :status WHERE number = :number LIMIT 1

基本上我只是将您的代码重写为 SQL,您可以随心所欲地进行。我认为,如果您的订单不同并且同一值有多个 number 行,那么 Limit 1 可能会出现排序问题。但是我不确定您的数据库看起来像什么,您的原始代码也是如此。

关于php - 在数据库中找不到记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55071934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com