Ullas Pv Ullas Pv - 7 months ago 6
SQL Question

What might be wrong in this php code?

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:


  1. Database Name: forum

  2. Table name: messages



columns-


  1. id (foreign key referring to topicID in a different table)

  2. time (Default being set to CURRENT_TIMESTAMP)

  3. text



PS: I'm new to php. So help is really appreciated

Answer

I believe the error is in this line:

if(isset($_POST['text'])){ $tag = $_POST['text']; }

it should be

if(isset($_POST['text'])){ $text = $_POST['text']; }

instead, since you require the parameter $text and not $tag.

I would also set this line

$stmt->bind_param('is',$id1,$text);

after this line:

$id1=$id;
Comments