[英]Trying to use variable in MySQL INNER JOIN
I am a php and MySQL newbie.我是一个 php 和 MySQL 新手。 What I have done is created an html form with a <select>
dropdown.我所做的是创建一个带有<select>
下拉列表的 html 表单。 Based on the selection from the form, it changes the $_SESSION[campaignID]
variable.根据表单中的选择,它会更改$_SESSION[campaignID]
变量。 Changing the selection in the form is supposed to then change what displays on the page.更改表单中的选择应该会更改页面上显示的内容。 The page consists of a forum style post that allows users to fill out a textarea and then submit it into the MySQL database into a table called "posts."该页面由一个论坛风格的帖子组成,允许用户填写一个文本区域,然后将其提交到 MySQL 数据库中的一个名为“帖子”的表中。 On this same page I then display the contents of the "posts" table.在同一页面上,我然后显示“posts”表的内容。
What I have done is in the posts MySQL table, I have added a campaignID row.我所做的是在帖子 MySQL 表中,我添加了一个活动 ID 行。 Then in my campaigns table I also have a campaignID row.然后在我的活动表中,我还有一个活动 ID 行。 The campaignID in campaigns table auto increments every time a campaign is created.每次创建活动时,活动表中的活动 ID 都会自动增加。 Then with the earlier mentioned dropdown I can select the campaign I want, and it should then show all posts with the same campaignID as the campaign I selected.然后使用前面提到的下拉菜单,我可以选择我想要的活动,然后它应该显示与我选择的活动具有相同活动 ID 的所有帖子。
I can verify that the $_SESSION[campaignID]
is changing when i select the various options.当我选择各种选项时,我可以验证$_SESSION[campaignID]
是否正在更改。 Because when I do that and then save another post, it takes the session variable campaignID and saves it properly in the posts table.因为当我这样做然后保存另一个帖子时,它会使用会话变量 CampaignID 并将其正确保存在帖子表中。
Now what I need it to do, is when I change the drop down (which then changes $_SESSION[campaignID]
) I need it to display the posts that share the same campaignID as the selected campaign.现在我需要它做的是,当我更改下拉列表(然后更改$_SESSION[campaignID]
)时,我需要它显示与所选活动共享相同活动$_SESSION[campaignID]
的帖子。 I am just not able to get it to display when trying to put a variable in my INNER JOIN query.当我试图在我的 INNER JOIN 查询中放置一个变量时,我只是无法让它显示出来。 I have a feeling the info I have provided may not be enough, but not sure what else I need to include here.我有一种感觉,我提供的信息可能不够,但不确定我还需要在这里包含什么。 Help?帮助?
<?php
$con=mysqli_connect("localhost","dorians","ds2953!b67P$","aldentec");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$campaignID = $_SESSION['campaignID'];
$result = mysqli_query($con,"SELECT posts.postDate, posts.postName, posts.postEntry FROM posts INNER JOIN campaigns ON posts.campaignID=" . $campaignID);
while($row = mysqli_fetch_array($result))
{
echo "<div id='campaignPostContainer'>";
echo "<ul class='campaignPostBox'>";
echo "<p class='postInfo'>";
echo "Posted on:";
echo "<li>" . $row['postDate'] . "</li>";
echo "</p>";
echo "<p class='postInfo'>";
echo "Posted by:";
echo "<li>" . $row['postName'] . "</li>";
echo "</p>";
echo "<li class='postEntry'>" . $row['postEntry'] . "</li>";
echo "</ul>";
echo "</div>";
echo "<hr>";
}
mysqli_close($con);
?>
Without going into a use-prepared-statements rant...没有进入使用准备的语句咆哮......
You're not pulling the session variable, but assigning $campaignId
to literally the string $_SESSION[campaignID]
.您不是在拉会话变量,而是将$campaignId
分配给字符串$_SESSION[campaignID]
。
Change your line:更改您的线路:
$campaignID = '$_SESSION[campaignID]';
to:到:
$campaignID = $_SESSION['campaignID'];
Also, your query is going to generate a cross product unless you define something in your ON clause like:此外,除非您在 ON 子句中定义以下内容,否则您的查询将生成交叉产品:
SELECT posts.postDate, posts.postName, posts.postEntry FROM posts
INNER JOIN campaigns ON posts.campaignID= $campaignID
AND posts.campaignID= campaigns.id
The value should be in single quote '
so you may try this值应该是单引号'
所以你可以试试这个
posts.campaignID='" . $campaignID ."'"
instead of代替
posts.campaignID=" . $campaignID
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.