Basically what I have is facebook-like DB, with posts and comments. What I'm trying to do is having the possibility to add a specific comment to a post. When pressing the submit of the appropriate comment - the data should go into the DB. But no matter what I do, even though I can make each submit button relate to a specific comment - I can't make the INSERT INTO work.
This is the problematic code:
echo '<div style="clear:both;"></div>';
echo '<h5 style="text-align: left; color:red;">Here you can comment on this post:</h4>';
$iter=$name1;
$iter2=$name1*2;
echo "<form action='' method='Post'>";
echo "<input type='hidden' name='ToSubmit' value='".$name1."'>";
echo "<input class='z' type = 'submit' name ='$iter' value ='Insert'>";
echo "Please insert the comment (You are limited to 250 chars): <input type='text' name='$iter2'><br>";
echo "</form>";
if (isset($_POST["$iter"]))
{
$date2=date("Y-m-d H:m:s");
$neg= -1;
echo $neg;
$CommentContent=$_POST["$iter2"];
echo $CommentContent;
echo $date2;
echo $USERID;
echo $USERNAME;
echo $name1;
$sqlComment="INSERT INTO FComment (comment, tStamp, userId, uName, comment_commented, post_commented ) VALUES('".$_POST['$iter2'] ."', '".$date2."', '". $USERID ."', '".$USERNAME."', '".$neg."', '".$name1."');";
$res55=sqlsrv_query($conn, $sqlComment );
}
I tested the INSERT INTO query on its on, it's working fine. All the echos show that the information is stored properly into the variables, it just won't add it to the DB.
Any ideas?
EDIT: So I figured half o the problem - the SQL queries had the same name, so I'd get duplicated queries. I tried solving this by going to dynamic variable:
${sqlComment}="INSERT INTO FComment (comment, tStamp, userId, uName, comment_commented, post_commented ) VALUES('".$CommentContent ."', '".$date2."', '". $USERID ."', '".$USERNAME."', '".$neg."', '".$name1."');";
${res55}=sqlsrv_query($conn, ${sqlComment});
But now a new problem arises - I need to press the submit button twice for an entry to be added, and it adds a blank record with it. Any ideas?