ScaperExe ScaperExe - 7 months ago 25
PHP Question

Select statement fails to populate textbox

I've currently got these two select statements however the first statement works perfect and populates the fields below however the second one doesn't populate the desired field.

<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>


This one works fine and the article category is populated.

<tr>
<td>
<label class="labels">Article Name:</label>
</td>
<td>
<input type="text" name="articlename" value="<?php echo $result2['name'] ?>" />
</td>
</tr>


This don't throw any error just simply don't show anything in the field.

Am i doing something wrong?

Answer

change this,

$stmt2 = $pdo->query('SELECT * FROM comments WHERE artID = "' . $articleID . '"');
   $result2 = $stmt->fetch(PDO::FETCH_ASSOC);

to this,

$stmt2 = $pdo->query('SELECT * FROM comments WHERE artID = "' . $articleID . '"');
   $result2 = $stmt2->fetch(PDO::FETCH_ASSOC);

you are doing wrong $stmt2->fetch(PDO::FETCH_ASSOC) here.