简体   繁体   English

HTML形式的MySQL查询-输入可以使用通配符吗?

[英]MySQL Query from HTML form - can an input use wildcards?

I've had a look, and while I've seen lots of explanations for using wildcards in the original PHP, I've not found anything to help me when the user input wants to use wildcards. 我已经看过了,虽然我看到了很多在原始PHP中使用通配符的解释,但是当用户输入要使用通配符时,我发现没有什么可以帮助我的。 I am very new to PHP and MYSQL. 我对PHP和MYSQL非常陌生。 Which symbol should be used? 应该使用哪个符号?

Basically, the user requests data from the MYSQL database for a given grade, which can be a simple number or a number followed by a letter, or a plus sign. 基本上,用户从MYSQL数据库请求给定等级的数据,可以是简单数字或数字后跟字母或加号。 I'd like them to be able to request specifically 6a (which works fine) or 6 followed by a wildcard to return 6a, 6a+, 6b etc (which I don't know how to do). 我希望他们能够特别要求6a(工作正常)或6,然后再使用通配符来返回6a,6a +,6b等(我不知道该怎么做)。

For further detail: 有关更多详细信息:

I have the following form: 我有以下表格:

<form name="gradereturn" method="POST" action="gradequery.php">
<input type="text" name="Grade" value="Grade">
<input type="submit" name="Submit1" value="Retrieve">

and PHP: 和PHP:

<?php
$grade = $_POST['Grade'];
if (isset($_POST['Submit1'])) {
    mysql_connect("localhost", "**details**", "**details**") or die(mysql_error());
    mysql_select_db("**details**") or die(mysql_error());
    $result = mysql_query("SELECT * FROM routes WHERE Grade='$grade'") or die(mysql_error());  
    $row = mysql_fetch_array( $result );
    echo "Line - Colour - Grade<br />";
    while($row = mysql_fetch_array($result)){
        echo $row['Line']. " - ". $row['Colour']. " - ". $row['Grade'];
        echo "<br />";
    }
}
else print ("Please enter your required grade above");
?>

Thank you in advance for any help. 预先感谢您的任何帮助。

Use LIKE and make sure you escape $grade as well: 使用LIKE并确保您也逃脱了$grade

$grade = mysql_real_escape_string($grade);
$sql = "SELECT * FROM routes WHERE Grade LIKE '{$grade}'";
$result = mysql_query($sql);

They can then specify 6 or 6% or 6? 然后,他们可以指定66%还是6? etc. 等等

You will want to use the LIKE command. 您将要使用LIKE命令。

$result = mysql_query("SELECT * FROM routes WHERE Grade LIKE '$grade%'")

This tells SQL to find all entries in routes where Grade starts with $grade . 这告诉SQL在以$grade开头Grade routes中查找所有条目。

For more information on LIKE - MySQL Reference 有关LIKE更多信息-MySQL参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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