【问题标题】:return the value of a database column from its ID从其 ID 返回数据库列的值
【发布时间】:2018-06-24 10:15:44
【问题描述】:

我尝试从 ID 中返回数据库列的值。 我想在 ID 为“#name_page”的<span> 中显示它

我通过 POST "$(this).attr("id")" 发送 ID

我有我的 AJAX 调用

$(document).on('click', '.update_btn', function(e){
   e.preventDefault();
      $.ajax({
      url:"fetch_single.php",
      type:"POST",
      data:{
        user_id: $(this).attr("id"),
      },
      success:function(data) {  
        $('#name_page').val(data.name_page);
        $('#user_id').val(user_id);            
    }
  });

然后我在 fetch_single.php 中接收它

$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "test";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare("SELECT name_page FROM table WHERE id = '".$_POST["user_id"]."' 
        LIMIT 1"); 
    $stmt->execute();
}
catch(PDOException $e) {
    echo "Error: " . $e->getMessage();
}
$conn = null;

但不可能在我的网页中恢复价值

【问题讨论】:

  • 使用 PDO 但不是 prepare() 与占位符?
  • 看起来你需要一个echo json_encode($stmt->fetch()); 在你的$stmt->execute();之后
  • @Sean 即使我的数据类型不是 json?
  • $('#name_page').val(data.name_page); 中使用data.name_page 表示您期待json
  • @sean 谢谢我正在学习,你的评论澄清了我的问题。我正在继续挖掘问题..

标签: php jquery mysql ajax pdo


【解决方案1】:

您没有获取结果,因此它将为空,jquery 也需要 json,因此您需要对您的响应进行 json_encode。

除了您使用 PDO,您还应该使用准备好的查询。

<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "test";

$json = [];

// is POST
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    // check user_id
    if (!isset($_POST["user_id"]) || !is_numeric($_POST["user_id"])) {
        exit(json_encode(['error' => 'Invalid user id']));
    }

    try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $stmt = $conn->prepare("SELECT name_page FROM table WHERE id = ? LIMIT 1"); 
        $stmt->execute([$_POST["user_id"]]);

        $json = ['name_page' => $stmt->fetchColumn()];
    }
    catch(PDOException $e) {
        $json = ['error' => $e->getMessage()];
    }
    $conn = null;

} else {
    $json = ['error' => 'Sorry expecting POST'];
}

echo json_encode($json);

为你的 jQuery 试试这个:

$(document).on('click', '.update_btn', function(e){
    e.preventDefault();

    var user_id = $(this).attr("id");

    $.ajax({
      url:"fetch_single.php",
      type:"POST",
      dataType: "json",
      data:{
        user_id: user_id,
      },
      success: function(data) {
        $('#name_page').val(data.name_page);
        $('#user_id').val(user_id);            
      }
   });
});

【讨论】:

  • @lawrence_Cherone 谢谢,如果我模拟 $_POST["user_id"] = "2";在 fetch_single.php 的顶部,但我的 AJAX 调用仍然存在问题,它不返回 fetch_single.php 值。无论如何,非常感谢,我有前进的感觉 =)
  • @Odjone 看到编辑,你有一个语法错误..以上应该可以工作。
  • @lawrence_Cherone 工作正常!!!感谢您帮助我...我将阅读有关 dataType:json 的更多信息。可能我的问题来自那里。无论如何我真的很感激!有一个美好的一天/晚上
猜你喜欢
  • 1970-01-01
  • 2017-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多