When I run my code I either get an empty query, or I change the code and I get error messages. I can get the post table data and put it into the form. I can get the topic table info for all of my topics. I just can’t get the posttopic lookup table to return anything so I can see if I need to create a checked checkbox or not. Echo statements are used for debugging. The last echo statement is never run due to the empty query.
Echo output:
[code]
Post ID: 5 Topic ID: 1
SELECT topicid FROM posttopic WHERE postid = :id
Post ID: 5 Topic ID: 2
SELECT topicid FROM posttopic WHERE postid = :id
Post ID: 5 Topic ID: 3
SELECT topicid FROM posttopic WHERE postid = :id
Post ID: 5 Topic ID: 4
SELECT topicid FROM posttopic WHERE postid = :id
Post ID: 5 Topic ID: 5
SELECT topicid FROM posttopic WHERE postid = :id
Post ID: 5 Topic ID: 6
SELECT topicid FROM posttopic WHERE postid = :id
posttopic table:
[code]
postid topicid
5 3
7 3
13 1
PHP/MySQL:
[code]
$id = $_GET[‘id’];
// Grabs data from the post table
try
{
$pdo = new PDO(DSN, DBUSER, DBPASS);
$pdo->setAttribute(PDO::ATTR_ERRMODE, DO::ERRMODE_EXCEPTION);
$sql = “SELECT * FROM post WHERE id = :id”;
$stmt = $pdo->prepare($sql);
$stmt->bindParam(‘:id’, $id, PDO::PARAM_STR);
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$subject = stripslashes($row[‘subject’]);
$content = stripslashes($row[‘content’]);
} // while
}
catch (PDOExecption $e)
{
$markup->content = “<p>PDO Execption caught.</p>n
<p>Error with database:</p>n
<p>SQL Query: “.$sql.”</p>n
<p>Error: “.$e->getMessage().”</p>n”;
}
// Places the retrieved post data into the form
$markup->content = “<form action = “.$_SERVER[‘SCRIPT_NAME’].” method = post’>n
<p><label for ‘subject’>Subject:</label><br /><input type = ‘text’ name = ‘subject’ value = ‘”.$subject.”‘ /></input>/p>n
<p><label for=’content’>Content:</label><br /><textarea name = ‘content’ rows = ‘5’ cols = ’45’/>”.$content.”</textarea></p>n
<p>Place in categories:<br />n”;
// Grabs all of the topics
try
{
$pdo = new PDO(DSN, DBUSER, DBPASS);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = $pdo->query(“SELECT id, name FROM topic”);
while($topics = $sql->fetch(PDO::FETCH_ASSOC)){
$topicid = $topics[‘id’];
echo “Post ID: “.$id. ” Topic ID: “.$topicid.”<br />”;
$name = htmlspecialchars($topics[‘name’]);
// Grabs the related topics from the posttopic lookup table
try
{
$pdolu = new PDO(DSN, DBUSER, DBPASS);
$pdolu->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sqllu = “SELECT topicid FROM posttopic WHERE postid = :id”;
echo $sqllu.”<br />”;
$stmt = $pdolu->prepare($sqllu);
$stmt->bindParam(‘:id’, $postid, PDO::PARAM_STR);
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$lookuptopic = $row[‘topicid’];
echo “Post ID: “.$id. ” Topic ID: “.$topicid.” Lookup Topic: “. $lookuptopic.”<br />”;
if ($topicid = $lookuptopic)
$markup->content .= “<label><input type = ‘checkbox’ name = ‘topics[]’ value = ‘”.$topicid.”‘ checked />”.$name.”</label><br />n”;
else
$markup->content .= “<label><input type = ‘checkbox’ name = ‘topics[]’ value = ‘”.$topicid.”‘ />”.$name.”</label><br />n”;
} // while
}
catch (PDOExecption $e)
{
$markup->content = “<p>PDO Execption caught.</p>n
<p>Error with database:</p>n
<p>SQL Query: “.$sql.”</p>n
<p>Error: “.$e->getMessage().”</p>n”;
}
} // while
}