我正在处理我的网络项目,我正在尝试使用php和MySql数据库创建一个搜索栏。但每次我收到警告信息时,我已经尝试了Stack Overflow上的几乎所有解决方案。
但是任何一个都不会给我任何改进。在我的数据库中有一个名为'project_2013'的表,它有4列'ID,NAME,PROJECT,TITLE'。这是我用过的php代码。
<?php
if(isset($_POST['search'])){
$searchTerm = $_POST['search'];
'%$searchTerm%'";
$query = "SELECT * FORM project_2013 WHERE TITLE LIKE
'%".mysqli_real_escape_string($con, $searchTerm)."%'";
$result = mysqli_query($con,$query);
$count = mysqli_num_rows($result);
if ($count == 0){
echo 'SORRY Nothing in our Server...';
}
else{
while($row = mysqli_fetch_array($query)){
$title = $row['TITLE'];
$projectName = $row['PROJECT'];
$name = $row['NAME'];
echo '<br>';
echo '<div class = "row">';//row start
echo '<div id = "content" class = "col-lg-12 col-md-12 col-sm-12 col-xs-12">'; //div1
echo '<b>Title:</b> ' . $row['TITLE'] . '<br>';
echo '<b>Research Paper:</b> ' . '<a href = "documents/2013_2014/' . $row['PROJECT'] .'" target = "_blank">'. $row['TITLE'] . '</a>' . '<br>';
echo '<b>Conducted By:</b> ' . $row['NAME'] . '<br>';
echo '</div>';//end of div1
echo '</div>';//row end
echo '<hr>';
}
}
}?>
这是表格标签部分。
<form action = "Home.php" method = "post">
<div class="input-group">
<input type="text" class="form-control" name = "search" placeholder="Search for Research...">
<span class="input-group-btn">
<button class="btn btn-default" type="submit" name="submit">
<span class="glyphicon glyphicon-search"></span></button>
</span>
</div><!-- /input-group -->
答案 0 :(得分:0)
使用:
"SELECT * FROM"
在SQL而不是:
"SELECT * FORM
注意:
在将SQL添加到代码中之前,您应该验证它。
不应在此处发布带语法问题的代码。
答案 1 :(得分:0)
首先,您应该更改该查询部分中的&#34; SELECT * FROM&#34; 。
部分还有一个人认为你应该改变 mysqli_fetch_array($ query)而不是 mysqli_fetch_array($ result)。
检查下面正确的代码
<?php
if(isset($_POST['search'])){
$searchTerm = $_POST['search'];
//$searchTerm = preg_replace("#[^0-9 a-z]#i","",$searchTerm);
//$query = "SELECT * FROM project_2013 WHERE TITLE LIKE '%$searchTerm%'";
$query = "SELECT * FORM project_2013 WHERE TITLE LIKE '%".mysqli_real_escape_string($con, $searchTerm)."%'";
$result = mysqli_query($con,$query);
$count = mysqli_num_rows($result);
if ($count == 0){
echo 'SORRY Nothing in our Server...';
}
else{
while($row = mysqli_fetch_array($result)){
$title = $row['TITLE'];
$projectName = $row['PROJECT'];
$name = $row['NAME'];
echo '<br>';
echo '<div class = "row">';//row start
echo '<div id = "content" class = "col-lg-12 col-md-12 col-sm-12 col-xs-12">'; //div1
echo '<b>Title:</b> ' . $row['TITLE'] . '<br>';
echo '<b>Research Paper:</b> ' . '<a href = "documents/2013_2014/' . $row['PROJECT'] .'" target = "_blank">'. $row['TITLE'] . '</a>' . '<br>';
echo '<b>Conducted By:</b> ' . $row['NAME'] . '<br>';
echo '</div>';//end of div1
echo '</div>';//row end
echo '<hr>';
}
}
}?>