2

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>
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
FGOD
  • 103
  • 2
  • 10

1 Answers1

2

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; ?>
Community
  • 1
  • 1
Kevin
  • 41,694
  • 12
  • 53
  • 70
  • Thanks a lot! I'm gonna try it right away :) and i know about MySQL that will not be supported in the future, i'm working on that but i wanna get everything working first ;) but i have just one more question... can this also be done the same way to make a checkbox so i can delete a entry? – FGOD Nov 13 '14 at 08:34
  • @FGOD well yes can be also use a logic with a checkbox, but i think its much better with a radio since your intent is to check a single entry – Kevin Nov 13 '14 at 08:43
  • i need it for two different functions, one page to show the content which is the one you gave me with the url part, the other one is for a user to delete content from the database which needs to let the user select one or multiple items/images to be removed, so that is why i was thinking of using a checkbox system with a remove link for all that is checked... – FGOD Nov 13 '14 at 08:48
  • @FGOD well if thats the case yes you could, just remember to get and redirect a single checkbox value, in the delete of course on another topic, you could gather those checked values then make another operation. – Kevin Nov 13 '14 at 08:49
  • thanks a lot, i'll look into that how to gather the values all together and make the delete command :) (never worked with a database so this is all new for me and gives me lots of learning time :) ) – FGOD Nov 13 '14 at 09:07
  • for some reason it keeps saying "Notice: Undefined index: ID in (webpage)/content/occasions.php on line 32", although it does display the image and fname database info... any ideas? – FGOD Nov 13 '14 at 12:14
  • @FGOD of course thats only my example, you need t substitute it to your name of the id column name of your table :) – Kevin Nov 13 '14 at 13:30
  • sorry for asking, (a maybe noob question) but which id do you mean? i did change the database stuff and the part is changed too to the id of the image... – FGOD Nov 13 '14 at 13:50
  • you have a table right? its called people. whats the name of column id? `people_id`? or just `id`? this is usually the primary key of the table – Kevin Nov 13 '14 at 13:51
  • @FGOD then just change `$row['ID']` to `$row['id']` – Kevin Nov 13 '14 at 13:58
  • hmmm weird, it does work but not with the image and it is having a hard time showing more then one row at a time... i just can put more echo $results['çolumnname'] for the other parts of the results with that id right? – FGOD Nov 13 '14 at 14:29
  • found the problem, it is having problems with showing the imgae in the results, that is why it just doesn't execute the results for fname and lname... any idea how i can get the results to show the image that is connected to the filename in database? i'm using the following now: echo "\"\""; " but i have no idea how i can implement that into the $results'[]' – FGOD Nov 13 '14 at 14:49
  • @FGOD check out me revision above – Kevin Nov 13 '14 at 15:18
  • thank you so much! finally i got it working :) btw i'm already halway through the delete part too, only problem that that gives me is that it says i forgot to determine the delete value... so i need to check some things for that – FGOD Nov 13 '14 at 15:26
  • @FGOD good to know, anyways i urge you to read that PDO tutorial above, actually its quite easy to follow since it shows you the mysql old way. then showing you the new way PDO counterpart i suggest you adapt on it as mysql is now deprecated. im glad this helped – Kevin Nov 13 '14 at 15:32