Get simple html text field to post to mysql database
I feel dumb asking this question. I'm having a heck of a time trying to get a simple one text field in an html form to pass over to the MySql database via PHP. The id and date_time field are getting inserted but the text from the form text field isn't.
Here is what I got.
FORM.HTML:
<form action="php/comments.php" method="post" /> <input type="text" id="comment" /> <input type="submit" class="submit" value=" Submit Comment " /> </form>
COMMENTS.PHP:
<?php mysqli_query($db_conx, "INSERT INTO comments(comment, date_time) VALUES('$comment',now())"); header ("location: form.html"); ?>
COMMENTS TABLE (rows):
id comment date_time
Like I said the "id" which is set up as AUTO_INCREMENT and "date_time" rows are getting inserted. Comment, however, is coming up blank even though something was typed into the form.
Any help/ideas would be greatly appreciated. Can't quite figure out what I'm missing.
Answers
You need the name="blahblah" attribute when submitting forms.
On the PHP side, the name is how PHP receives the variable's "key" (var name) and then the field contents are the variable's value.
On the HTML side:
<input type="text" id="comment" name="myvarname" />
On the PHP side:
<?php $comment = $_POST['myvarname'];