mysql can't find record in_Can not find record in database

In addition to the other answers I would add this simple method to your models,

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));

}

Right now you have only the fact that you hard coded this, in your controller:

$table = "sales";

All it would take is to one day make this mistake in a 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);

}

And you would be open to SQL Injection even if you prepare the query.

Right now it's just Improbable that, that will happen, we should make this impossible.

Also, this is basically what you are doing:

//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

So why not just do this

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

Basically I am just rewording your code into just SQL, you can do it however you want. I think maybe ordering will be an issue here with Limit 1 if your order is different and you have multiple number rows for the same value. But I don't know what your DB looks like to say for sure, this is true with your original code as well.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值