0
<form method="post" action="post.php">
<input type="text" name="code"/>
</form>

<?php
$code = $_POST['code'];
include ("db.php");
$query = mysql_query("SELECT * FROM t_code WHERE code LIKE '$code'");
if(mysql_num_rows($query))
{
echo "Got data";
}
else
echo "No data";
}

在表 t_code

code_id | code
1       | GG125-0015
2       | BA512-1152

现在我想当用户有这样的组合代码:GG1250015--0021255 或 BA5121152--52211554,它会回显得到数据,因为我们看到代码包含在数据库中。

那么如何使用 LIKE 将其设置到我的 SQL 中呢?有什么建议吗?

4

3 回答 3

1
$code=substr($mainSub,0,5)."-".substr($mainSub,5,4);
$query = mysql_query("SELECT * FROM t_code WHERE code='$code'");
于 2013-07-23T07:25:31.163 回答
1
$query = mysql_query("SELECT * FROM t_code WHERE '$code' like CONCAT(\"%\",REPLACE(CODE,\"-\",\"\"),\"%\")");
于 2013-07-23T07:30:06.857 回答
0

Morning,

use a wildcard search on the like variable..

if you want to search after the code use $code% which allows anything after the code or use %$code% if you want the entered code to match anywhere..

you will need to remove the - from the stored code if you are matching against your suggestion though as it will be looking for gg1250015 not gg125-0015

The only thing with this if someone enters just gg125 they will also get a result so may have to validate that the code is 9 characters using STRLEN first

于 2013-07-23T06:42:22.977 回答