2016-09-27 76 views
0

我使用搜索表单来搜索与数据基础值,但我没能得到充分利用PHP中存储的数据的基础数据结果我的表名是数据基础“用户”请帮助如何使用搜索框

data.html 我的代码看起来像

<form action="data2.php" class="navbar-form navbar-right"> 
    <div class="input-group"> 
     <input type="Search" name="user_search" value="" placeholder="Search..." class="form-control" /> 
     <div class="input-group-btn"> 
      <button class="btn btn-info"> 
      <span class="glyphicon glyphicon-search"></span> 
      </button> 
     </div> 
    </div> 
</form> 
现在

data2.php类似的代码

<?php 
       include("db/opendb.php"); 
       $username=$_POST['user_search']; 

    $query = "SELECT * FROM user WHERE user_name=='" .$username"'"; 
    $result = $conn ->query($query) or die("Query error");s 
    ?> 
    <table width="983" border="1" cellpadding="5" class="insertTable"> 
     <tr> 

     <td width="50"><strong>user id</strong></td> 
     <td width="109"><strong>user name</strong></td> 
     <td width="40"><strong>user adress</strong></td> 
     <td width="38"><strong>user phone no</strong></td> 
     <td width="99"><strong>dept id</strong></td> 
     </tr> 
     <?php 
     foreach($result as $row) 
     { 
     ?> 

     <tr> 

     <td><?php echo $row['user_name']; ?></td> 
     <td><?php echo $row['user_adress']; ?></td> 
     <td><?php echo $row['user_phone_no']; ?></td> 
     <td><?php echo $row['dept_id']; ?></td> 
     </tr> 
     <?php 
     } 
     ?> 

请任何机构可以帮助我,我正在努力工作,但没有什么好办法结果不显示 我正在使用搜索表单来搜索数据库中的值,但我没有得到结果我的表名称是数据库中的“用户”请帮助 我会对你非常满意

+1

'其中user ==''一个'=这里也和许多失踪' ' “$用户名”''点。检查错误将避免这种调试/错字问题。并在这里''或死(“查询错误”); s'不应该在那里。 –

+0

感谢它现在的工作 –

+0

不客气。我发布了一个可以接受的(社区维基)答案,以便将问题标记为已解决。请参阅http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work关于如何做到这一点。 –

回答

1

发表我的评论作为社区维基;我不希望这个代表。

WHERE user_name==还有一个=这里太多,缺少点'" .$username"'。检查错误会在这里帮助你。

此外,s这里die("Query error");s不应该在那里。

您的代码也倾向于SQL注入;使用准备好的声明。

参考文献:

您也应该检查对你的POST阵列空字段:

+0

非常感谢它为我工作 –

+0

@SomiRana不客气。 –

相关问题