Search code examples
phphtmlmysqlrecord

Data wasn't able to retrieve from the previous page (PHP and MYSQL)


I researched here in stackoverflow trying to find whether someone is also encountering the same problem. I know it's kind of easy and even I really don't know what's the error because there's no problem with my query.

On the previous page, here's my code to retrieve the ID Number so I'll be able to select the data with that ID number:

<a href="package.php?place_id=<?php $row['place_id'];?>"><?php echo $row['place_name'];?></a>

I tried first to print the value of the place id and it works fine. But when it was being called to the Package page, the data I want to show weren't displayed. I look at the URL and it shows this after the package.php

place_id=

I don't know why it is blank, please check my code if there's missing or just wrong.

In my package page, here's the PHP code:

    <?php
        include("common/connect.php");
        $place_id = $_GET['place_id'];
        $result = mysql_query("SELECT * FROM package_items WHERE place_id = '$place_id'");
        $row1 = mysql_fetch_array(mysql_query("SELECT place_name FROM packages WHERE place_id = '$place_id'"));
        if($result === FALSE) {
        die(mysql_error()); // for better error handling
    }
?>

In HTML Code:

<h1><?php echo $row1['place_name'];?></h1>
       <?php while($row=mysql_fetch_array($result)) {?>
       <?php echo $row['item_title'];?>
       <br>
        <a href="package-places.php"> Back </a>
        <?php } ?>

Please check my codes. Thanks.


Solution

  • You are not printing it.

    Change

    <?php $row['place_id'];?> // It will output nothing as no echo or print.
    

    To

    <?php echo $row['place_id'];?>
    

    Rest of the code looks fine.

    Three suggestions: 1) $place_id = $_GET['place_id'];

    Change to

    $place_id = ! empty($_GET['place_id']) ? $_GET['place_id'] : ''; // To avoid any warning.

    2) Don't feed variable from $_GET or $_POST to any SQL.

    3) Don't use mysql_ functions as they are deprecated and will be remove in future versions of PHP.