I have made a LEFT JOIN
to get the values from 2 tables from my database.
The query is like this:
SELECT *
FROM thread
LEFT JOIN comments ON thread.id_thread = comments.id_thread
WHERE id_type = '1'
ORDER BY data DESC, hour DESC
Then I output the values this way:
<?
while($row = mysqli_fetch_array($query))
{
echo '<div class="col-md-1"></div>';
echo '<div class="col-md-11">';
echo $row['title'] ."<br><br>".$row['content']."<br><br>";
echo $row['content_com'];
echo '<div class="col-md-2 pull-right">'. "date: ".$row['data']."<br>"."author: ".'<a href ="/user.php?id='.$row['username'].'">'.$row['username'].'</a>'.'</div>' ."<br><br>";
echo '<form role="form" action="commit.php" method="post"><div class="col-md-offset-1 col-md-9"><input class="form-control" type="text" name="comm"><input type="hidden" name="thread_id" value="'.$row['id_thread'].'"></div></form> <br><br><hr><br>';
echo '</div>';
}
mysqli_close($connect);
?>
Then in the commit.php (form action):
<?php
session_start();
if(isset($_SESSION['id']))
{
$servername = "mysql9.000webhost.com";
$username = "a5461665_admin";
$password = "xenovia1";
$dbname = "a5461665_pap";
$connect = mysqli_connect($servername, $username, $password, $dbname);
$id = (isset($_GET['id'])) ? $_GET['id'] : $_SESSION['id'];
$ctn = $_POST["comm"];
$com = mysqli_query($connect,"INSERT INTO comments(content_com,id_thread) values ('".$ctn."', '".$_POST['thread_id']."')");
header("location:javascript://history.go(-1)");
if (!$connect) {
die("Connection failed: " . mysqli_connect_error());
}
}
else
{
header(" url=index.php");
}
?>
My problem is that the hidden input box is passing to the form action the field id_thread
from the table comments
but I want it to pass the field id_thread
from the table threads
, how do I do that ??
SELECT *, thread.id_thread as mycol
FROM
thread LEFT JOIN comments
ON thread.id_thread=comments.id_thread
WHERE thread.id_type = '1'
ORDER BY data desc, hour desc
Specify column name with the table and alias it. So, SELECT *
for all columns as before, now taking thread.id_thread
and alias it to mycol
. This will be now available as mycol
and no more-name clashing.
you can use "alias" or the table name - then specify which column you want to use
SELECT T.*, comments.id_thread AS comment_thread_id
FROM thread T
LEFT JOIN comments
ON thread.id_thread=comments.id_thread
WHERE id_type = '1' ORDER BY data desc, hour desc
see, T
is alis for table name, thread, T.*
will select all cols from thread
table, comments.id_thread
will take just column ID from table comments
named as comment_thread_id
Next to using aliases/tablename you can also use USING()
instead of ON
to join the tables.
SELECT T.*, comments.id_thread AS comment_thread_id
FROM thread T
LEFT JOIN comments
USING(id_thread)
WHERE id_type = '1' ORDER BY data desc, hour desc
Here is a nice explanation of the difference between both methods: https://stackoverflow.com/a/11367066/3595565
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.