S
S
SergeyQA3172020-01-26 14:48:05
PHP
SergeyQA317, 2020-01-26 14:48:05

Pass parameter for output from database[PHP,SQL]?

Hello!
I'm learning php and writing a small project, more for gaining practice.
The question is, how to display on the output page, only the desired record with a unique identifier?
Pages table schema:
id, name, content, author, date
Tables with id are new and unique every time.
Main page for displaying all entries:
index.php

....

    $sth = $dbh->prepare("SELECT * FROM `pages` ORDER BY `name`");
    $sth->execute();
    $array = $sth->fetchAll();


foreach ($array as $result_row) {
    echo "<tr>";
    echo "<table><tr><td>" . $result_row["name"]  . "<tr></td></tr>";
    echo "<td>" . $result_row["id"]  . "</td>";
    echo "<td>" . $result_row["author"]  . "</td>";
    echo "<td>" . $result_row["date"]  . "</td></table>";
    echo "</tr>";
}

...

?>

My goal:
Make a page that displays a certain news, for example, by a unique ID, getting from the table I
made the following code:
code.php
}
$idka = $_GET["id"];
$sth = $dbh->prepare("SELECT content FROM `pages` where id = '$idka'");
$sth->execute();
$array = $sth->fetchAll();


foreach ($array as $result_row) {

    echo htmlspecialchars($result_row["content"]);
}

This is a slightly modified code in the news output, I added that the ID is taken from the GET parameter....
Only in the end you have to manually enter the parameter - code.php?id=1, code.php?id=2, etc...
I want so that being on the main page, I clicked on the "record" that passed this unique ID for its subsequent output

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
FanatPHP, 2020-01-26
@SergeyQA317

echo '<td><a href="code.php?id=' . $result_row["id"]  . '">Edit</a></td>';

just for God's sake remove all these incomprehensible <table>
A's in the receiving script
$sth = $dbh->prepare("SELECT content FROM `pages` where id = ?");
$sth->execute([$_GET["id"]]);
$result_row = $sth->fetch();
echo htmlspecialchars($result_row["content"]);

because the whole point of the incomprehensible prepare / execute spells is that prepare prepares a query with markers, and the variables themselves are passed to execute ().

S
SergeyQA317, 2020-01-26
@SergeyQA317

in principle, my bro FanatPHP made the norm codec, removed the extra two quotes and earned
echo ' Delete ';
Thanks bro! hugged everyone

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question