【问题标题】:List Table A, Search Table B and update Table A with result列出表 A、搜索表 B 并使用结果更新表 A
【发布时间】:2021-03-10 15:17:25
【问题描述】:

对于我的论文,我需要对图像进行分类。图像链接存储在表“ocr”中。在我的搜索页面中,我按行显示所有图像。使用 AJAX 我正在搜索表“diss_kategorien”并在表 OCR 的下拉列表中显示结果。现在我需要用结果更新表 ocr,但我不知道如何使用正在处理的当前图像从行中传递变量

这是我的搜索页面:

<div class="card-body">
            <table width="95%" class="table table-bordered tablehover">
                <tr>
                    <th width="40">ID</th>
                    <th>Image</th>
                    <th width="200">Hauptgegenstand</th>
                    <th width="130">Save</th>
                    <th width="137">Löschen</th>
                    <th width="137">Update</th>
                </tr>
                <?php
                foreach($result as $r)
                {
                ?>
                <form action="<?php echo $_SERVER['PHP_SELF'];?>"  method="post" enctype="application/x-www-form-urlencoded">
                <tr>
                    <td><input readonly name="id" type="text" class="form-control input-sm" value="<?php echo($r->id); ?>" id="id" /></td>
                    <td width="232"><img style="max-width: 400px" src="uploads/<?php echo($r->file_name);?>"></td>
                    <td>
                         <input type="text" name="search" id="search" autocomplete="off" placeholder="Gegenstand suchen">
                         <div id="output"></div>
                    </td>
                    </div>    
                    <td><input type="submit" class="btn btn-primary" name="submit"></td>
                    <td><input type="submit" class="btn btn-primary" name="delete"></td>
                    <td width="5%"><a href="index.php?ocr_id=<?php echo($r->id);?>">Update</a> </td>
                    
                </tr>
                
                </form>
                <?php }?>
            </table>
        </div>

这里是 AJAX 代码:

<script type="text/javascript">
    $(document).ready(function(){
       $("#search").keyup(function(){
          var query = $(this).val();
          if (query != "") {
            $.ajax({
              url: 'ajax-db-search.php',
              method: 'POST',
              data: {query:query},
              success: function(data){
 
                $('#output').html(data);
                $('#output').css('display', 'block');
 
                $("#search").focusout(function(){
                    $('#output').css('display', 'none');
                });
                $("#search").focusin(function(){
                    $('#output').css('display', 'block');
                });
              }
            });
          } else {
          $('#output').css('display', 'none');
        }
      });
    });
  </script>

这是我的 ajax-db-show.php

$conn=mysqli_connect($servername,$username,$password,$dbname);
        mysqli_query($conn,"SET CHARACTER SET 'utf8'");
        mysqli_query($conn,"SET SESSION collation_connection ='utf8_unicode_ci'");
    
          if(!$conn){
              die('Could not Connect MySql Server:' .mysql_error());
            }


    if (isset($_POST['query'])) {

    $val = '%' . $_POST['query'] . '%';
    
    $stmt = $conn->prepare("SELECT * FROM diss_kategorien WHERE ebene_1_txt LIKE ? OR ebene_2_txt LIKE ? or ebene_3_txt like ?");
    $stmt->bind_param("sss", $val, $val, $val);
    $stmt->execute();
    $result = $stmt->get_result();

 
  if (mysqli_num_rows($result) > 0) {
     while ($user = mysqli_fetch_array($result)) {
     // echo "Ebene 1: " . $user['ebene_1_txt']. "Ebene 2: " . $user['ebene_2_txt'] . "Ebene 3: ". $user['ebene_3_txt'] ."<br/>";
    print "<td><a href='show_ocr_files_mainitem.php?param1=" . $user['ebene_1_nr'] . "&param2=" . $user['ebene_2_nr'] . "&param3=" .$user['ebene_3_nr']. "&param4=" .$_POST['id']."'>ID: " . $_POST['id'] . " Ebene 1: " . $user['ebene_1_txt']. " Ebene 2: " . $user['ebene_2_txt'] . " Ebene 3: ". $user['ebene_3_txt'] . "<br/></a></td>"; 
    }
  } else {
    echo "<p style='color:red'>Nichts gefunden..</p>";
  }

当我在“Hauptgegenstand”字段中输入搜索词时,我会生成一个超链接,该超链接应该将变量传递到搜索页面以更新表格 OCR:

print "<td><a href='show_ocr_files_mainitem.php?param1=" . $user['ebene_1_nr'] . "&param2=" . $user['ebene_2_nr'] . "&param3=" .$user['ebene_3_nr']. "&param4=" .$_POST['id']."'>ID: " . $_POST['id'] . " Ebene 1: " . $user['ebene_1_txt']. " Ebene 2: " . $user['ebene_2_txt'] . " Ebene 3: ". $user['ebene_3_txt'] . "<br/></a></td>"; 

当我使用搜索结果的ID进行更新时,这是错误的,因为我需要OCR表的ID

我试过了

data: {query:query, id: <?php echo $r->id; ?>},

但是显示的 ID 是 470 而不是 111。我没有得到我正在搜索的 OCR 字段的 ID,我只得到 OCR 表搜索中最后一个条目的 ID。我已将搜索限制为 50,所以最后一个 ID 是 470

所以我得到了正确的搜索结果,但我不知道如何将表 OCR 的 ID 传递到 Ajax 文件并从那里返回到搜索页面以更新表 OCR。

感谢您的帮助和建议如何解决这个问题。

一切顺利,

斯蒂芬

【问题讨论】:

  • 这很容易受到 sql 注入只使用 带参数的准备好的语句 参见stackoverflow.com/questions/60174/…
  • 嗨 nbk - 我知道它不正确,但它在本地主机上而不是在线......
  • 当问题未被删除时,有人可能会找到并可能会复制它,因此我们敦促人们始终正确地这样做,因为它不花钱,而且也是一种很好的培训。其次,由于我们没有您的数据,我们无法帮助您,我们需要的是 minimal reproducible example 或直接在服务器上运行您的代码,看看它会得到什么并更改选择直到它适合
  • 著名的遗言。
  • 请立即执行准备好的语句,然后看看它是否有效。如果在那之后您仍然面临问题,那么您可以返回并使用新代码更新问题。见stackoverflow.com/questions/28385145/…

标签: php jquery mysql ajax


【解决方案1】:

您不能将相同的 id 用于多个元素,而是使用 class 。因此,将 id="search" 更改为 class="search" 并将 id="output" 更改为 class="output" 。然后,您可以使用$(this).closest("tr").find("input[name=id]").val() 获取当前正在执行搜索的行的id

演示代码

$(document).ready(function() {
  $(".search").keyup(function() {
    var $this = $(this); //for refering `this` inisde success fn
    var selector = $(this).closest("tr"); //get closest tr
    var query = $(this).val();
    var id = $(this).closest("tr").find("input[name=id]").val(); //get id
    console.log("Id -- " + id)
    if (query != "") {
      /*$.ajax({
        url: 'ajax-db-search.php',
        method: 'POST',
        data: {
          query: query,
          id:id
        },
        success: function(data) {*/
      //selector.find(".output").html(data);
      selector.find(".output").html("somethinfsfsfsf"); //just for demo..
      selector.find(".output").css('display', 'block');

      $this.focusout(function() {
        selector.find(".output").css('display', 'none');
      });
      $this.focusin(function() {
        selector.find(".output").css('display', 'block');
      });
      /* }
      });*/
    } else {
      selector.find(".output").css('display', 'none');
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="card-body">
  <table width="95%" class="table table-bordered tablehover">
    <tr>
      <th width="40">ID</th>
      <th>Image</th>
      <th width="200">Hauptgegenstand</th>
      <th width="130">Save</th>
      <th width="137">Löschen</th>
      <th width="137">Update</th>
    </tr>


    <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" enctype="application/x-www-form-urlencoded">
      <tr>

        <td><input readonly name="id" type="text" class="form-control input-sm" value="1" id="id" /></td>
        <td width="232"><img style="max-width: 400px" src="uploads/<?php echo($r->file_name);?>"></td>
        <td>
          <input type="text" name="search" class="search" autocomplete="off" placeholder="Gegenstand suchen">
          <div class="output"></div>
        </td>

        <td><input type="submit" class="btn btn-primary" name="submit"></td>
        <td><input type="submit" class="btn btn-primary" name="delete"></td>
        <td width="5%"><a href="index.php?ocr_id=<?php echo($r->id);?>">Update</a> </td>

      </tr>
    </form>

    <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" enctype="application/x-www-form-urlencoded">
      <tr>

        <td><input readonly name="id" type="text" class="form-control input-sm" value="2" id="id" /></td>
        <td width="232"><img style="max-width: 400px" src="uploads/<?php echo($r->file_name);?>"></td>
        <td>
          <input type="text" name="search" class="search" autocomplete="off" placeholder="Gegenstand suchen">
          <div class="output"></div>
        </td>

        <td><input type="submit" class="btn btn-primary" name="submit"></td>
        <td><input type="submit" class="btn btn-primary" name="delete"></td>
        <td width="5%"><a href="index.php?ocr_id=<?php echo($r->id);?>">Update</a> </td>

      </tr>
    </form>


  </table>
</div>

【讨论】:

    猜你喜欢
    • 2021-03-16
    • 2016-03-17
    • 1970-01-01
    • 2011-09-28
    • 2018-10-08
    • 1970-01-01
    • 2013-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多