This code when opened in the browser, displays "Reply button not pressed!" at first when the page loads, and After text is entered and Post
button is pressed, The text isn't inserted into the database:
Here's the code:
<html>
<head><title>Some Title</title></head>
<body>
<div class="bottom">
<form action="#reply" method='post'>
<input type="submit" class="button" value="Post">
</form>
</div>
<a id="reply" href="#" class="popup"></a>
<div class="popup">
<div class="title"><h3>Reply to this Topic</h3></div>
<div class="itopic">
<p>What would you think about this?</p>
<form name="pform" method="post" action="">
<tr>
<td><textarea name="text" placeholder="Enter your thoughtful response here!" cols="50" rows="20"></textarea></td>
</tr>
<br>
<tr>
<input type="submit" name="reply" class="tbutton" value="Reply"/>
</tr>
</form>
</div>
<a class="close x" href="#">x</a>
</div>
<?php
$id=$_GET['id']; //id fetched from the URL
if(isset($_POST['reply']) && !empty($_POST['reply']))
{
$conn=new mysqli('localhost','root','','forum') or die(mysql_error());
if(!strlen(trim($_POST['text']))>0)
{
echo "Reply!";
}
else
{
$stmt= $conn->prepare("INSERT into messages(id,text) VALUES(?,?)");
$stmt->bind_param('is',$id1,$text);
if(isset($_POST['text'])){ $tag = $_POST['text']; }
$id1=$id;
$stmt->execute();
$stmt->close();
$conn->close();
}
header("Location: forum.php");
}
else{
echo"Reply button Not pressed!";
}
?>
</body>
</html>
What might be wrong in this php code? Database details:
- Database Name: forum
- Table name: messages
columns-
- id (foreign key referring to topicID in a different table)
- time (Default being set to CURRENT_TIMESTAMP)
- text
PS: I'm new to php. So help is really appreciated