FGOD
FGOD

Reputation: 103

using the MySQL id that is shown as a onclick/checkbox id

I'm working on a webpage that needs to show database content and let you click on them to show more info from the database related to that id or even linking a checkbox next to the content to the same databse id so I can delete the post if needed, my code for showing the content so far work perfectly,

but I just don't have a clue on how I could link the id/info that is related to the id to a checkbox or onclick command

my code so far:

<?php 
error_reporting(E_ALL); 
ini_set('display_errors', 1);

// Get our database connector
require("includes/conn.php");
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>

<link href="css/styles.css" rel="stylesheet" type="text/css" />
</head>

<body>
    <div id="featured">
    <h2></h2>

    <div>

   <?php 
   error_reporting(E_ALL); 
   ini_set('display_errors', 1); 

    // Grab the data from our people table

    $sql = "SELECT * FROM people ORDER BY ID";
    $result = mysql_query($sql) or die ("Could not access DB: " . mysql_error());

    while ($row = mysql_fetch_assoc($result))
    {
        echo "<div class=\"picture\">";
        echo "<p>";

        // Note that we are building our src string using the filename from the database
        echo "<img src=\"content/uploads/" . $row['filename'] . "\" alt=\"\" height=\"219\" width=\"350\" /><br />" . "<br />";
        echo $row['fname'] . " " . "<br />" . "<br />";
        echo "</p>";
        echo "</div>";
    }

    ?>

    </div>

</div>

</body>
</html>

Upvotes: 2

Views: 359

Answers (1)

Kevin
Kevin

Reputation: 41885

No need for a checkbox, you can just wrap the image with an anchor

And with that, put a url with it including the id inside the query string:

echo "
<a href='moreinfo.php?id=".$row['id']."'>
    <img src=\"content/uploads/" . $row['filename'] . "\" alt=\"\" height=\"219\" width=\"350\" />
</a>
 <br />" . "<br />";
echo $row['fname'] . " " . "<br />" . "<br />";

Obligatory note:

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

So now, inside moreinfo.php with using PDO:

<?php
$results = array();
if(isset($_GET['id'])) {
    $id = $_GET['id'];
    $db = new PDO('mysql:host=localhost;dbname=DATABASE_NAME', 'username', 'password');

    $sql = 'SELECT * FROM people WHERE `id` = :id';
    $select = $db->prepare($sql);
    $select->bindParam(':id', $id, PDO::PARAM_INT);
    $select->execute();

    $results = $select->fetch(PDO::FETCH_ASSOC);
} else {
    header('Location: go_back_to_index.php');
}

?>

<?php if(!empty($results)): ?>
<h1>Results</h1>
<table cellpadding="10">
    <tr>
        <td>First Name:</td><td><?php echo $results['fname']; ?></td>
    </tr>
    <tr>
        <td>Last Name:</td><td><?php echo $results['lname']; ?></td>
    </tr>
    <tr>
        <td>Photo: </td><td><img src="content/uploads/<?php echo $results['filename']; ?>" alt="profile pricture" /></td>
    </tr>
    <!-- and so on -->
</table>
<?php endif; ?>

Upvotes: 2

Related Questions