gpt4 book ai didi

php - mysql php搜索表单-想要在选择查询之前从用户输入中删除特殊字符

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

我试图弄清楚如何在运行 SQL SELECT 查询之前从 PHP 搜索表单中的用户输入中删除特殊字符。

我有一个 PHP 搜索表单,它将用户输入提交到选择查询 > mysql 5 > Linux。选择查询查看某一列中的 LIKE 匹配项,并将结果返回到结果页。数据库中的列没有特殊字符 - 只有字母数字值。

我的数据库字段值如下所示:33345678DEP

如果用户输入 333-456*78DEP ,我将如何去除特殊字符?

我读到了有关 REGEX 的内容 - 看起来它可能是答案,但我不知道如何实现它。

我执行搜索的 PHP 代码如下:

if (isset($_GET['InventoryByVendorForm'])) {
$colname_InventoryByVendorResults = $_GET['InventoryByVendorForm'];
}
mysql_select_db($database_ic3, $ic3);
$query_InventoryByVendorResults = sprintf("SELECT icCombinedSupplier_wrk.icMaster_icmKEY, icCombinedSupplier_wrk.icsSupplierID, icCombinedSupplier_wrk.icsPartNum, icCombinedSupplier_wrk.icsQuantityOnHand, icCombinedSupplier_wrk.icsCost, icCombinedSupplier_wrk.icsCore, icCombinedSupplier_wrk.icsLastInventoryUpdate, icMaster.icmLineCode, icAttributes.`icaPartslink#` AS icaPartslink FROM icMaster INNER JOIN (icCombinedSupplier_wrk INNER JOIN icAttributes ON icCombinedSupplier_wrk.icMaster_icmKEY = icAttributes.icMaster_icmKEY) ON icMaster.icmKEY = icCombinedSupplier_wrk.icMaster_icmKEY WHERE `icCombinedSupplier_wrk`.`icMaster_icmKEY` Like %s ORDER BY icMaster_icmKEY ASC", GetSQLValueString("%" . $colname_InventoryByVendorResults . "%", "text"));

我最好的猜测是我需要在语句周围实现 REGEX [[alnum]] 的使用:

$colname_InventoryByVendorResults = $_GET['InventoryByVendorForm']

如何从搜索框中的用户输入中删除所有非字母/数字字符?

最佳答案

preg_replace是执行此操作的正确工具:

$validated = preg_replace("/[^A-Za-z0-9]/","",$colname_InventoryByVendorResults);

字母数字字符集有快捷方式,但这样就可以很直观地理解。

“^”否定选择,因此任何非字母数字的字符都将被替换为空字符串。

祝你有美好的一天,斯特凡

关于php - mysql php搜索表单-想要在选择查询之前从用户输入中删除特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10268914/

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