encrypted21 encrypted21 - 7 months ago 12
PHP Question

How this PHP code should look like to filter out displayed SQL rows if link is clicked?

I have a homework where I have to call all the rows from the database. When an id of each row is clicked, there should be only this that row displaying.
There are three rows which contain text and two columns: id INT NOT NULL AUTO_INCREMENT and content VARCHAR(255).


I have this code:

file: db.php

<?php

define("dbserver", "127.0.0.1");

define("dbuser", "root");

define("dbpass", "");

define("dbname", "dbtesting");




$db = new PDO(

"mysql:host=" .dbserver. ";dbname=" .dbname,dbuser,dbpass,

array(

PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",

PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET utf8"

)

);

?>


file: index.php

<?php

include "db.php";

function articleList($get = 0) {

global $db;

if (empty($get["p"])) {
$sql = "SELECT * FROM dbtesting.articles";
}
else {
$get2 = $get["p"];
$sql = "SELECT * FROM dbtesting.articles WHERE id = '$get2'";
}

$result = $db->query($sql);

foreach ($result as $value) {
echo "<div style='background-color:#dedede;margin-top:10px'>";
echo "<a href='index.php?p=".$value["id"]."'>"."ID"."</a>";
echo " - ";
echo $value["content"];
echo "</div>"."<br>";
}

}

articleList();

?>


The target is to make SQL to display only one article whom "ID" link has been clicked as shown in the picture below. No JavaScript or other languages are allowed. I have no idea how this should work.
This code has been written by my teacher and there is something I should fix for this to work.
I only remember him telling that this should be done by using get parameter or something. I didn't really understand. Could you please help to make it work?

Rows (articles) are displayed. When I click on the ID link there is no effect except the URL change. But content isn't changing.



All articles are displayed - working


all articles




Only clicked article should be displayed (
?p=id
in URL) - not working
Clicked

Answer

try use $_GET["p"] not $get...

Comments