doufu6130 2014-10-13 13:01
浏览 20

我的查询没有找到结果

I'm trying this query but I'm not able to get my results. I cant find the error! Here is my table structure:

id  norm(mediumtext) bohrung(int)       breite(int)     
 2  DIN 5462         26             6           
 3  DIN 5462         28             7           
 4  DIN 5462        32              6           
 5  DIN 5462        36              7           
 6  DIN 5462        42              8           
 7  DIN 5462        46              9       

This is my SQL query

<?php       
if (isset($_POST['bohrung'])) {
    $bohrung = $_POST['bohrung'];
    $result = mysqli_query ($con, "SELECT * FROM keilnaben WHERE norm  = {bohrung}");
    if($result && mysqli_num_rows($result) > 0) {
        echo '<table class="table" border="2">
                  <tr>
                     <th>norm</th>
                     <th>norm</th>
                     <th>norm</th>
                  </tr>';
        while($row = mysqli_fetch_array($result)) {
            echo "<tr>
                      <td>" . $row['norm'] . "</td>
                      <td>" . $row['bohrung'] . "</td>
                      <td>" . $row['breite'] . "</td>
                 </tr>";
        }
        echo "</table>";
    }

}

The problem is that when I enter for example DIN5462 in the text box, the query does not return anything, but if I try the same for bohrung of breite, it does return results. I don't know why.

  • 写回答

1条回答 默认 最新

  • doyrte8419 2014-10-13 13:08
    关注

    The problem is this line:

    SELECT * FROM keilnaben WHERE norm  = {bohrung}
                                           ^^^
    // its a string literal, not a variable
    

    Change it to this and at least escape your input:

    $bohrung = $con->real_escape_string($_POST['bohrung']);
    $result = mysqli_query($con,"SELECT * FROM keilnaben WHERE norm  = '$bohrung' ");  
    

    Or prepared statements:

    if (isset($_POST['bohrung'])) {
        $input = $_POST['bohrung'];
        $select = $con->prepare('SELECT * FROM keilnaben WHERE norm = ?');
        $select->bind_param('s', $input);
        $select->execute();
        if($select->num_rows > 0) {
            echo '<table class="table" border="2">
                    <tr>
                       <th>norm</th>
                       <th>norm</th>
                       <th>norm</th>
                    </tr>';
            $select->bind_result($norm, $bohrung, $breite);
            while ($select->fetch()) {
                echo "<tr>
                          <td>" . $norm . "</td>
                          <td>" . $bohrung . "</td>
                          <td>" . $breite . "</td>
                     </tr>";
            }
            echo "</table>";
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法