-1

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

Ullas Pv
  • 5
  • 2

3 Answers3

2

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;
Chris Stadler
  • 1,563
  • 1
  • 15
  • 17
1

I think your ID will be a text string and not an integer so the bind_param() function will fail. Try casting the id as an integer.

$id=$_GET['id']; //id fetched from the URL
$id = (int) $id;
Fergal Andrews
  • 316
  • 2
  • 11
1

My guess is your redirect command (header("Location: forum.php");), It's in the wrong place there

Miguel
  • 1,579
  • 5
  • 18
  • 31