1

I want to use a variable that comes from a table i MySQL and pass it to Another SQL-Query with PHP. Can´t get it to work and I can´t find out why.

Here is the code:

<html>
<head><title></title></head>
<body>
<div>

<?php

if (isset($_GET['read_blog_posts_scrolling']))
{

$result = mysql_query("SELECT blogpost.Blogpost_title, blog.Blogwriters_name, blogpost.Date 
FROM blog 
INNER JOIN blogpost ON blog.BlogID=blogpost.BlogID
WHERE blog.BlogID='$blogs_profile_id' // Here it is, it says undefined variable
ORDER BY blogpost.Date DESC")
or die(mysql_error());

while ($row = mysql_fetch_array($result)) {
			
			echo '<p>';
			echo "Titel: " . "<strong>" . $row['Blogpost_title'] . "</strong>" . " - Bloggare " . $row['Blogwriters_name'] . " " . $row['Date'] . '<br />';
			echo '<hr />';
			echo '</p>';

			}
}

?>

<?php

$result = mysql_query("SELECT BlogID, Blogwriters_name FROM blog")
or die(mysql_error());

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

$blogs_profile_id = $row['BlogID']; // I want to pass this value to above and use it in the query

echo '<p>';

echo $row['Blogwriters_name'] . '<br />';

//When clicking in this link I want the query to execute and values in BlogID to be passed

echo '<a href="?read_blog_posts">Choose blogwriter</a>';

echo '</p>';

?>

</div>
</body>
</html>

it says the variable is undefined. How can I define it and pass the value when the a href-link is clicked?

2 Answers 2

1

Error is clear. Undefined variable:

You didn't defined this variable anywhere before select statement

$blogs_profile_id

I think you need to add this variable in query string and get from $_GET.

UPDATE 1:

You have following issues in your code.

  1. Missing blog_profile_id in your query string.
  2. Undefined variable means you are using a variable but didn't defined.
  3. Using mysql_* extension its deprecated

Solution:

Replace this:

echo '<a href="?read_blog_posts">Choose blogwriter</a>';

With:

echo '<a href="?blog_id=$blogs_profile_id">Choose blogwriter</a>';

And than use that:

if (intval($_GET['blog_id']) > 0) 
{ 
      $blogs_profile_id = intval( $_GET['blog_id']);
      $result = mysql_query("SELECT blogpost.Blogpost_title, blog.Blogwriters_name, blogpost.Date FROM blog INNER JOIN blogpost ON blog.BlogID=blogpost.BlogID WHERE blog.BlogID=".$blogs_profile_id."  ORDER BY blogpost.Date DESC") 
or die(mysql_error());
.....
Sign up to request clarification or add additional context in comments.

Comments

0

Change the order of your queries. The second query code has to be coming first in order as below

<html>
<head><title></title></head>
<body>
<div>

<?php

$result = mysql_query("SELECT BlogID, Blogwriters_name FROM blog")
or die(mysql_error());

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

$blogs_profile_id = $row['BlogID']; // I want to pass this value to above and use it in the query

echo '<p>';

echo $row['Blogwriters_name'] . '<br />';

//When clicking in this link I want the query to execute and values in BlogID to be passed

echo '<a href="?read_blog_posts">Choose blogwriter</a>';

echo '</p>';

?>


<?php

if (isset($_GET['read_blog_posts_scrolling']))
{

$result = mysql_query("SELECT blogpost.Blogpost_title, blog.Blogwriters_name, blogpost.Date 
FROM blog 
INNER JOIN blogpost ON blog.BlogID=blogpost.BlogID
WHERE blog.BlogID='"+$blogs_profile_id+"' // Here it is, it says undefined variable
ORDER BY blogpost.Date DESC")
or die(mysql_error());

while ($row = mysql_fetch_array($result)) {
			
			echo '<p>';
			echo "Titel: " . "<strong>" . $row['Blogpost_title'] . "</strong>" . " - Bloggare " . $row['Blogwriters_name'] . " " . $row['Date'] . '<br />';
			echo '<hr />';
			echo '</p>';

			}
}

?>



</div>
</body>
</html>

1 Comment

Can you give me an example? Cut&Paste from above?

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.