【发布时间】: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'] . "¶m2=" . $user['ebene_2_nr'] . "¶m3=" .$user['ebene_3_nr']. "¶m4=" .$_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'] . "¶m2=" . $user['ebene_2_nr'] . "¶m3=" .$user['ebene_3_nr']. "¶m4=" .$_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/…