简体   繁体   English

将SQL查询与jquery变量一起使用

[英]Use sql query with jquery variable

So I am trying to do an history for my site but when i do the query select with the jquery variable it doesn't work. 所以我试图为我的网站做一个历史记录,但是当我使用jquery变量进行查询选择时,它不起作用。 HERE is the table that shows the values from db and here is the pop up box that opens to show details but I want to show the values from each row that I click 这里是显示db值的表, 是一个弹出框,用于显示详细信息,但我想显示单击的每一行的值

Here is the jquery code: 这是jQuery代码:

var idocorrencia;
$(document).on("click","#listagem tr td a", function(e){
e.preventDefault();
idocorrencia = $(this).parent().attr("idlista");
$("#listagem caption").text($(this).text());
console.log(idocorrencia);
alert(idocorrencia);
$.post( "historico.php", { idoc: idocorrencia })

   $.ajax({
   method:"POST",
   url:"historico.php",
   data:{idoc : "idlista"},
   dataType: 'json',
       });
   });      

Here is the php: 这是PHP:

$id = $_POST['idoc'];
$result = mysqli_query($conn, "SELECT id FROM ocorrencia where id=$id");
$row = mysqli_fetch_assoc($result);
$idoc = isset($_POST['idoc']) ? $_POST['idoc'] : $row['id']; 

Try to do it like this: 尝试这样做:

if (isset($_POST['idoc'])) {
    $id = $_POST['idoc'];
    $result = mysqli_query($conn, "SELECT id FROM ocorrencia where id='" . mysqli_real_escape_string($conn, $id) . "'");
    if($result!==false && mysqli_num_rows($result)>0){
        $row = mysqli_fetch_assoc($result);
        $idoc = $row['id'];
    }
}

UPDATE 更新

and here is the same script with prepared statements: 这是带有准备好的语句的相同脚本:

if (isset($_POST['idoc'])) {
    $statement = mysqli_prepare($conn, "SELECT id FROM ocorrencia where id=?");
    mysqli_stmt_bind_param($statement, 's', $id);
    $id = $_POST['idoc'];
    mysqli_stmt_execute($statement);
    $result = mysqli_stmt_get_result($statement);
    if ($result !== false && mysqli_num_rows($result) > 0) {
        $row = mysqli_fetch_assoc($result);
        $idoc = $row['id'];
    }
}

Here I have used procedural style, as the original script was like that. 在这里,我使用了过程样式,因为原始脚本就是这样。 But it can be easily rewritten in object oriented style. 但是可以很容易地以面向对象的风格重写它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM