PHP Like函数与组合代码

<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";
}

in table t_code

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

Now I want when user have the combination code like this : GG1250015--0021255 or BA5121152--52211554, it will echo Got data, because we see the code include is on database.

So how can I set that into my SQL using LIKE ? Any advice ?

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

$code=substr($mainSub,0,5)."-".substr($mainSub,5,4);
$query = mysql_query("SELECT * FROM t_code WHERE code='$code'");
$query = mysql_query("SELECT * FROM t_code WHERE '$code' like CONCAT(\"%\",REPLACE(CODE,\"-\",\"\"),\"%\")");