Jump to content

Why wont this form data post to DB?


bran

Recommended Posts

Ok, I'm trying to make a very simple form to add comments to images in a gallery. I pretty much copied the form and the code to insert it into the db from a form I have that already works. For some reason, though, this one will not. It isn't giving errors or anything. I click submit, it reloads the page, and nothing is in the db. Can you spot any eerrors in the code? I know its going to be something dumb when I figure it out. Thanks for any help.

[code]echo ('<form action="http://www.iwannabeaculjan.com/test/test2.php" method="post">');
echo ('Submitted By:<input type="text" name="submitted" size="30"><br>');
echo ('Comment:<textarea name="comment" rows=3 cols=30></textarea><br>');
echo ('<input type="submit" value="Add Comment">');
echo ('</form>');

$submitted=$_POST['submitted'];
$comment=$_POST['comment'];
$time=time;

if (isset($comment)) {
$sql="INSERT INTO piccomments (picid, comment, when, who) VALUES ('$picid', '$comment', '$time', '$submitted')";
$result=mysql_query($sql);
}
else {
exit();[/code]
Link to comment
Share on other sites

[code]
echo ('<form action="http://www.iwannabeaculjan.com/test/test2.php" method="post">');
echo ('Submitted By:<input type="text" name="submitted" size="30"><br>');
echo ('Comment:<textarea name="comment" rows=3 cols=30></textarea><br>');
echo ('<input type="submit" value="Add Comment">');
echo ('</form>');[/code]
You don't need to enclose the arguments to echo in parens.  This will work just fine:
echo "hello, world";

[code]$submitted=$_POST['submitted'];
$comment=$_POST['comment'];[/code]
You probably want to clean this data before submitting it:
$submitted = "'" . addslashes($_POST['submitted']) . "'";
$comment = "'" . addslashes($_POST['comment']) . "'";

[code]$time=time;[/code]
It might be optional, but I'd include the parens here:
$time = "'" . addslashes(time()) . "'";

[code]if (isset($comment)) {
$sql="INSERT INTO piccomments (picid, comment, when, who) VALUES ('$picid', '$comment', '$time', '$submitted')";
$result=mysql_query($sql);
}
else {
exit();[/code]
Where did you set $picid?  Further, picid is probably an INT column, thus single parens are not necessary.
I included the single parents for the other column values in the preceding lines, so you can drop them from the SQL statement.
Where is the closing curly brace for your if statement?
Link to comment
Share on other sites

$picid is set higher up in the file. it, along with the clsing brace for the if statement, got chopped off when I cut/pasted. The rest of the info sounds like good advice for neatening everything up, but if I understand all you're saying correctly, it all should work as it is (and it does on the other form, all I did was change the variables).
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.