简体   繁体   中英

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. I'm trying to show Film_Title (the name of the row in MySQL) using the Film_ID. In this case the Film_ID is coming in from the URL like this: /film.php?id=58.

How do I use that id to select 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. 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”;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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