简体   繁体   English

使用来自URL的$ _GET ['id']显示php中的mysql数据

[英]Show mysql data in php using the $_GET['id'] coming in from the URL

I'm getting the 'id' from the URL, but I can't seem to use it to select other pieces of data from the same row. 我从URL获得了“ id”,但似乎无法用它从同一行中选择其他数据。 I'm trying to show Film_Title (the name of the row in MySQL) using the Film_ID. 我正在尝试使用Film_ID显示Film_Title(MySQL中行的名称)。 In this case the Film_ID is coming in from the URL like this: /film.php?id=58. 在这种情况下,Film_ID是从这样的URL中输入的:/film.php?id=58。

How do I use that id to select Film_Title? 如何使用该ID选择Film_Title? Here's my code: 这是我的代码:

if (isset($_GET['id'])) {
    $id = trim($_GET['id']); 
    $Film_Title = trim($_POST['Film_Title']);

    $sql = "SELECT Film_Title FROM Films WHERE Film_ID=?";

    require_once 'includes/MySQL.php';  
    require_once 'includes/db.php';

    $db = new MySQL($dbconfig['host'], $dbconfig['user'], $dbconfig['password'], $dbconfig['database']);
    $stm = $db->dbConn->prepare($sql);
    $stm->execute(array($id, $Film_Title));
    echo "<h3>film record $Film_Title has been selected</h3>";

}

You are putting only a ? 你只放一个? in your query but two elements in your execute() array. 在查询中,但是在execute()数组中有两个元素。 I prefer to put the query just before where I'm using it, or just inside the prepare, since it's closer to the execute thus less error-prone. 我更喜欢将查询放在我使用它的地方之前,或者放在准备内部,因为它离执行更近,因此不易出错。 Besides, you were not fetching your data. 此外,您没有获取数据。

if (isset($_GET['id'])) {
  $id = trim($_GET['id']); 
  $Film_Title = trim($_POST['Film_Title']);

  require_once 'includes/MySQL.php';    
  require_once 'includes/db.php';

  $db = new MySQL($dbconfig['host'], $dbconfig['user'], $dbconfig['password'], $dbconfig['database']);
  $stm = $db->dbConn->prepare("SELECT Film_Title FROM Films WHERE Film_ID=?");
  // There's only one placeholder, so only 1 element in the array is needed
  $stm->execute(array($id));
  // Also you need to fetch the results
  $result = $stm->fetch();
  echo "<h3>film record " . $result['Film_Title'] . " has been selected</h3>";
  }

$ sql =“从电影中选择Film_Title,而Film_ID = $ id”;

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

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