将值从SQL查询传递到php中的另一个SQL查询

时间:2021-01-08 17:04:11

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.

我想使用一个来自MySQL表的变量,并使用PHP将其传递给另一个SQL-Query。不能让它工作,我不知道为什么。

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?

它说变量是未定义的。如何在单击href-link时定义它并传递值?

2 个解决方案

#1


1  

Error is clear. Undefined variable:

错误很明显。未定义的变量:

You didn't defined this variable anywhere before select statement

您没有在select语句之前的任何位置定义此变量

$blogs_profile_id

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

我认为您需要在查询字符串中添加此变量并从$ _GET获取。

UPDATE 1:

You have following issues in your code.

您的代码中存在以下问题。

  1. Missing blog_profile_id in your query string.
  2. 在查询字符串中缺少blog_profile_id。

  3. Undefined variable means you are using a variable but didn't defined.
  4. 未定义的变量意味着您正在使用变量但未定义。

  5. Using mysql_* extension its deprecated
  6. 使用mysql_ *扩展名已弃用

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());
.....

#2


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


1  

Error is clear. Undefined variable:

错误很明显。未定义的变量:

You didn't defined this variable anywhere before select statement

您没有在select语句之前的任何位置定义此变量

$blogs_profile_id

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

我认为您需要在查询字符串中添加此变量并从$ _GET获取。

UPDATE 1:

You have following issues in your code.

您的代码中存在以下问题。

  1. Missing blog_profile_id in your query string.
  2. 在查询字符串中缺少blog_profile_id。

  3. Undefined variable means you are using a variable but didn't defined.
  4. 未定义的变量意味着您正在使用变量但未定义。

  5. Using mysql_* extension its deprecated
  6. 使用mysql_ *扩展名已弃用

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());
.....

#2


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>