我做了一个LEFT JOIN从数据库的两个表中获取值。
查询如下:

SELECT *
FROM thread
  LEFT JOIN comments ON thread.id_thread = comments.id_thread
WHERE id_type = '1'
ORDER BY data DESC, hour DESC

然后我按如下方式输出值:
<?

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);
?>

然后在commit.php(表单操作)中:
<?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");
}


 ?>

我的问题是,隐藏的输入框正在将表id_thread中的字段comments传递给表单操作,但我希望它将表id_thread中的字段threads传递给表单操作,我该怎么做??

最佳答案

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

使用表指定列名并将其别名。
因此,对于所有列,SELECT *和以前一样,现在取thread.id_thread并将其别名为mycol。这将是现在可用的mycol和没有更多的名称冲突。

10-06 09:08