Hello ,
I have a form in post.php page . When i submit that , its inserting only the comments not the id of the post .
This is my form in post.php page .
This is func.php code which is in post.php page . This retrieves the id of the post title and displays the details of the post in post.php page .
This is the comment.php page to insert form details .
Not able to insert post-id ( pid ) and user-id ( id ) into comment's table . What am i missing?
Do help to solve this ?
I have a form in post.php page . When i submit that , its inserting only the comments not the id of the post .
This is my form in post.php page .
Code:
//Details of post.php
<form action="comment.php" method="post">
<input type="hidden" name="pid" value="<? $row['pid'];?>"/>
<input type="hidden" name="id" value="<? $row['id'];?>"/>
<input type="text" name="comment" value=""/>
<input type="submit" name="submit" Value="Submit"/>
</form>
<?php include('[B]func.php[/B]'); ?>
//End details of post.php
Code:
<?php
// Connection data (server_address, database, username, password)
$dbhost = 'localhost';
$dbname = 'project';
$dbuser = 'root';
$dbpass = '';
$pid=$_GET['pid'];
// Display message if successfully connect, otherwise retains and outputs the potential error
try
{
$conn = new PDO("mysql:host=$dbhost; dbname=$dbname", $dbuser, $dbpass);
//echo 'Connected to database';
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
try
{
$query = $conn->prepare("SELECT * FROM post WHERE pid ='$pid'");
$query->execute();
}
catch (PDOException $e)
{
error_log($e->getMessage());
die($e->getMessage());
}
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
echo "<div id='box' >";
echo "<h3 class='title'>" .$row['title']. " – <span class='landmark'>".$row['area']."</span></h3>";
echo "<p class='address'>".$row['address']."</p>";
echo "</div>";
}
$conn = null;
?>
Code:
<?php
// Connection data (server_address, database, username, password)
$dbhost = 'localhost';
$dbname = 'project';
$dbuser = 'root';
$dbpass = '';
// Display message if successfully connect, otherwise retains and outputs the potential error
try {
$conn = new PDO("mysql:host=$dbhost; dbname=$dbname", $dbuser, $dbpass);
//echo 'Connected to database';
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
try
{ $lid = $conn->lastInsertId();
$query = $conn->prepare("INSERT INTO `comment` (pid,comment,id,date) VALUES (:pid, :comment, :id, :date)");
$query->execute(
array(
'pid' => $_POST['pid'],
'comment' => $_POST['comment'],
'id' => $_POST['id'],
'date' => $_POST['date'],
));
$lastId = $conn->lastInsertId();
}
catch (PDOException $e)
{
error_log($e->getMessage());
die($e->getMessage());
}
$conn = null;
header('location:home.php');
exit();
?>
Do help to solve this ?
Comment