我想回显“提示”列等于用户选择的表单输入选项的行。
我尝试了replace (tipster ' '. '') LIKE '$tipster'
,但未返回任何结果...有些字段的名称中有空格,有些则没有。例如'John Jones Smith'
,'John Jones'
和'JohnJones'
....我需要搜索与html表单中选择的选项匹配的列(检查带空格和不带空格的行)
如果我只是进行单个单词搜索(例如,示例)...其 Like 查询的结果就很好。我想评论一下GET函数是否起作用,但是正在将输入作为示例+一个传递给url。...不确定+是否会影响mysqli结果?
HTML
<form id="tipster_search" method="get" action="example.php">
<label for="tipster">Select Tipster</label>
<select class="form-control" name="tipster" id="tipster">
<option>Example One</option>
<option>ExampleTwo</option>
<option>Example Option Three</option>
</select>
<br>
<button class="btn btn-info" name="submit">Search</button>
</form>
PHP
function processForm() {
$tipster = $_GET['tipster'];
$url = "example.php?tipster=".$tipster."";
header("Location: $url");
exit;
}
$tipster = $_GET['tipster'];
$q = "SELECT * FROM bets WHERE `tipster` LIKE '%$tipster%' ORDER BY betDate DESC LIMIT 25";
$query = mysqli_query($connection,$q);
$x = 1;
echo "<table class='table'><tr>";
echo "<th>ID</th>";
echo "<th>Bet Date</th>";
echo "<th>Tipster</th>";
echo "<th>Sport</th>";
echo "<th>Meeting</th>";
echo "<th>Time</th>";
echo "<th>Stake Name</th>";
echo "<th>Odds</th>";
echo "<th>Stake Type</th>";
echo "<th>Stake Placed</th>";
echo "<th>Result</th>";
echo "<th>Return</th>";
echo "<th>Profit</th>";
if($query === FALSE) {
die(mysqli_error($connection)); // better error handling
}
while($res = mysqli_fetch_array($query)){
$id = $res['id'];
$betDate = $res['betDate'];
$tipster = $res['tipster'];
$sport = $res['sport'];
$meeting = $res['meeting'];
$time = $res['time'];
$stakeName = $res['stakeName'];
$odds = $res['odds'];
$stakeType = $res['stakeType'];
$stakePlaced = $res['stakePlaced'];
$result = $res['result'];
$return = $res['return'];
$profit = $res['profit'];
echo "<tr><td><p>$id</p></td>";
echo "<td><p>$betDate</p></td>";
echo "<td><p>$tipster</p></td>";
echo "<td><p>$sport</p></td>";
echo "<td><p>$meeting</p></td>";
echo "<td><p>$time</p></td>";
echo "<td><p>$stakeName</p></td>";
echo "<td><p>$odds</p></td>";
echo "<td><p>$stakeType</p></td>";
echo "<td><p>£$stakePlaced</p></td>";
echo "<td><p>$result</p></td>";
echo "<td><p>£$return</p></td>";
echo "<td><p>£$profit</p></td></tr>";
}
?>
答案 0 :(得分:1)
怎么样:
WHERE REPLACE(tipster, ' ', '') LIKE CONCAT('%', REPLACE('$tipster', ' ', ''), '%')
注意:SO上的任何人都强烈建议 use prepared statements and parameterized queries ,以保护您的代码免遭SQL注入,并使查询更具可读性和可维护性。使用参数化查询时,这种错别字要容易得多。