我目前有这两个select语句,但是第一个语句工作正常并填充下面的字段,但是第二个语句没有填充所需的字段。
<php?
if(isset($_GET['id']))
{
$articleID = $_GET['id'];
$stmt = $pdo->query('SELECT * FROM article WHERE articleID = "' . $articleID . '"');
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt2 = $pdo->query('SELECT * FROM comments WHERE artID = "' . $articleID . '"');
$result2 = $stmt->fetch(PDO::FETCH_ASSOC);
}
?>
<tr>
<td>
<label class="labels">Article Category:</label>
</td>
<td>
<input type="text" name="articlecate" value="<?php echo $result['articlecate'] ?>" />
</td>
</tr>
这个工作正常,文章类别已填充。
<tr>
<td>
<label class="labels">Article Name:</label>
</td>
<td>
<input type="text" name="articlename" value="<?php echo $result2['name'] ?>" />
</td>
</tr>
这不会抛出任何错误,只是不在字段中显示任何内容。
我做错了吗?
答案 0 :(得分:0)
改变这个,
$stmt2 = $pdo->query('SELECT * FROM comments WHERE artID = "' . $articleID . '"');
$result2 = $stmt->fetch(PDO::FETCH_ASSOC);
到此,
$stmt2 = $pdo->query('SELECT * FROM comments WHERE artID = "' . $articleID . '"');
$result2 = $stmt2->fetch(PDO::FETCH_ASSOC);
你在这里做错 $ stmt2-&gt; fetch(PDO :: FETCH_ASSOC)。
答案 1 :(得分:0)
$stmt2 = $pdo->query('SELECT * FROM comments WHERE artID = "' . $articleID . '"');
$result2 = $stmt->fetch(PDO::FETCH_ASSOC);
替换为此行
$stmt2 = $pdo->query("SELECT * FROM comments WHERE artID = '$articleID'");
$result2 = $stmt2->fetch(PDO::FETCH_ASSOC);