0

I have a blog-type php site with mysql database. This blog have some lessons. There's a table "lessons", which contain id, title, text of lesson, etc. When I display the last lessons on the main page, it works just right.

I connect to db like this:

<?php 
$db = mysql_connect ("localhost", "root", "");
mysql_select_db("kursach", $db);
mysql_query("SET NAMES utf8");

$result = mysql_query ("SELECT title, meta_k, meta_d, text FROM settings
WHERE page='index' ", $db);

$myrow = mysql_fetch_array($result);
?>

and display them using loop:

<?php 
$result = mysql_query("SELECT id, title, date, description FROM lessons", $db);

$myrow = mysql_fetch_array($result);
do {
  printf ("<div class='right-column-content'>
    <div class='right-column-content-heading'>
      <a href='lesson_view.php?%s'><h1>%s</h1></a>
      <h2>%s </h2>
    </div>
    <div class='right-column-content-content'>
      <p>%s</p>
      <div class='button'><a href='lesson_view.php?%s' >Читати далі</a></div>
    </div>
  </div>", $myrow['id'], $myrow["title"], $myrow["date"], $myrow["description"], $myrow["id"]);
}
while($myrow = mysql_fetch_array($result))
 ?>

Screenshot

I also have a file for full content of the lesson - lesson_view.php. As you can see at the code above, i send the id of lesson to link to this lesson:

  lesson_view.php?%s
  $myrow["id"]

In the lesson_view.php I connect to db and get the id like this:

<?php 
$db = mysql_connect ("localhost", "root", "");
mysql_select_db("kursach", $db);
mysql_query("SET NAMES utf8");

if (isset($_GET['id'])) {$id = $_GET['id'];}

$result = mysql_query("SELECT * FROM lessons WHERE id = '$id' ", $db);

$myrow = mysql_fetch_array($result);
?>

And use this code to display the data:

<div class="right-column-content">
    <div class="right-column-content-heading">
      <h1><?php echo $myrow['title'] ?></h1>
      <h2><?php echo $myrow['date'] ?> </h2>
      <h2><?php echo $myrow['author'] ?> </h2>
    </div>
    <div class='right-column-content-content'>
      <p><?php echo $myrow["text"] ?></p>
    </div>
  </div>

The problem is, when I try to look the full content of lesson (for exaple, /lesson_view.php?1), it doesn't display any data: no title, no text, nothing. I've tried this query directly at MySQL and it works, so, maybe there's some error in php code that I can't find. Will be thankful for any help.

enter image description here

P.S. I'm a beginner at php.

1
  • Your if logic is flawed, in lesson_view.php your query and execute of query should be put inside of the if sentence, because if your $id isn't set in the if clause you will still get a "Notice undefined" because you are calling the $id which isn't set. (And it will most likely cause your query to fail) - Also, you should change from mysql to mysqli or PDO no reason to learn a deprecated / bad habbits from the start. Commented Dec 23, 2015 at 7:52

3 Answers 3

2

if you want to have in $_GET id then your link should be instead lesson_view.php?%s -> lesson_view.php?id=%s

for example lesson_view.php?id=5 mean that $id = $_GET['id'] will give 5, $id = 5;

Sign up to request clarification or add additional context in comments.

Comments

1
<a href='lesson_view.php?%s'><h1>%s</h1></a>

You did not name the 'id' variable.

Change the links to

<a href='lesson_view.php?id=%s'><h1>%s</h1></a>

Comments

0

The only issue is that your printf() have 5 arguments and you are using only four and fourth one is description that you are using here

<div class='button'><a href='lesson_view.php?%s' >Читати далі</a></div>

Solution is that:

printf ("<div class='right-column-content'>
<div class='right-column-content-heading'>
<a href='lesson_view.php?%s'><h1>%s</h1></a>
<h2>%s </h2>
</div>
<div class='right-column-content-content'>
<p>%s</p>
<div class='button'><a href='lesson_view.php?id=%s' >Читати далі</a></div>
</div>
</div>", $myrow['id'], $myrow["title"], $myrow["date"], $myrow["id"], $myrow["description"]);

Move $myrow["id"] in fourth position you will get the ID as query string.

And also add the id in query string.

1 Comment

@artur-komendacki: just change second arguments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.