Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用准备好的语句筛选值以保持安全性?_Php_Mysql_Prepared Statement_Sql Injection - Fatal编程技术网

Php 如何使用准备好的语句筛选值以保持安全性?

Php 如何使用准备好的语句筛选值以保持安全性?,php,mysql,prepared-statement,sql-injection,Php,Mysql,Prepared Statement,Sql Injection,我使用准备好的语句主要是为了防止SQL注入。现在我还需要过滤ENUM类型。但是我应该如何在我准备好的声明中使用它来维护安全呢 我有一个地址表,需要过滤用户的发票地址。我如何做到这一点并保持安全?还是没关系 我能想到两个选择。 数组中的“发票”作为字符串 public function getCustomerInvoiceAddresses($customerNumber) { $query = 'SELECT contactPerson, company, street, zipCode

我使用准备好的语句主要是为了防止SQL注入。现在我还需要过滤
ENUM
类型。但是我应该如何在我准备好的声明中使用它来维护安全呢

我有一个地址表,需要过滤用户的发票地址。我如何做到这一点并保持安全?还是没关系

我能想到两个选择。 数组中的“发票”作为字符串

public function getCustomerInvoiceAddresses($customerNumber)
{
    $query = 'SELECT contactPerson, company, street, zipCode, city, deliveryMethod 
                FROM address 
                where FK_customerNumber = ? 
                AND addressType = ?';
    $paramType = 'is';
    $paramValue = array(
        $customerNumber,
        "Invoice"
    );
    $invoiceAddressArray = $this->ds->select($query, $paramType, $paramValue);
    return $invoiceAddressArray;
}
在“选择”对话框中选择发票

public function getCustomerInvoiceAddresses($customerNumber)
{
    $query = 'SELECT contactPerson, company, street, zipCode, city, deliveryMethod 
                FROM address 
                where FK_customerNumber = ? 
                AND addressType = "Invoice"';
    $paramType = 'is';
    $paramValue = array(
        $customerNumber
    );
    $invoiceAddressArray = $this->ds->select($query, $paramType, $paramValue);
    return $invoiceAddressArray;
}
或者我应该在调用函数时传递字符串“Invoice”

$invoiceAddresses = $customer->getCustomerInvoiceAddresses($customerNumber, "Invoice");

如果您正在解析数据并使用prepare语句,我不相信SQL注入会发生。所以我会使用最灵活的选择和使用

$invoiceAddresses=$customer->getCustomerInvoiceAddresses($customerNumber,“发票”)

将函数重命名为getCustomerAddresses

,这样哪一个可以工作就无所谓了,尽管第一个更合理,最后一个最不合理。所有功能都可以工作,但根据安全性我不知道。你应该学习一些安全性。至少在某种程度上,安全性只对变量重要,而SQL的常量部分显然是安全的,这不是灵活的,而是过分的。该方法被称为getCustomerInvoiceAddresses,重复两次“Invoice”没有任何意义。尽管整个函数过于复杂,但它实际上可能是一个两行程序,一个用于查询,另一个用于函数调用$invoiceAddressArray=$this->ds->select($query,'is',array($customerNumber));整个功能不是