简体   繁体   中英

Using slashes in the where clause while comparing values in active record codeigniter

I have the following query

$query="DELETE FROM salesinvoiceitems WHERE invoiceNumber=".$this->put('invoiceNumber');

Here $this->put('invoiceNumber'); always have values like "M\\34\\SD". Due to slashes in values it doesn't work as expected.

I researched and found the mysql_escape_string can be used for this purpose but its deprecated now as per the manual. So whats my best bet here?

Why not use Codeingiter Active Record instead? An example:

$this->db->where('invoiceNumber', $this->put('invoiceNumber'));
$this->db->delete('salesinvoiceitems');

Taken from Codeigniter documentation:

Beyond simplicity, a major benefit to using the Active Record features is that it allows you to create database independent applications, since the query syntax is generated by each database adapter. It also allows for safer queries, since the values are escaped automatically by the system.

There's a method in the activerecord called escape , so you should :

$invoice = $this->db->escape($yourVar);
$query = "DELETE FROM salesinvoiceitems WHERE invoiceNumber=$invoice";

Which will protect against sql injection as it escapes the var.

尝试这个

$query="DELETE FROM salesinvoiceitems WHERE invoiceNumber=".addslashes($this->put('invoiceNumber'));

尝试带斜线

$query="DELETE FROM salesinvoiceitems WHERE invoiceNumber='".($this->put('invoiceNumber')). "'";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM