twilitegxa Posted January 14, 2010 Share Posted January 14, 2010 I have the following form: <table class="style2" align="center"> <tr> <td> <form name="comment" id="comment" action="comment_form.php" method="post" enctype="text/plain"> <table border="0" cellspacing="0" cellpadding="5" width="662" class="style2"> <tr> <td align="left"><label for="name"> Name:</label></td> <td> <div class="c2"><input type="text" name="comment_owner" id="comment_owner" size="30" /></div> </td> </tr> <tr> <td align="left"><label for="email">E-mail:</label></td> <td> <div class="c2"><input type="text" name="comment_owner_email" id="comment_owner_email" size="30" /></div> </td> </tr> <tr> <td align="left"><label for="url">URL:</label></td> <td> <div class="c2"><input type="text" name="url" id="url" size="30" /></div> </td> </tr> <tr> <td align="left"> <label for="comment">Comments:</label> </td> <td> <textarea name="comment" id="comment" rows="5" cols="30"> </textarea></td> </tr> <tr> <td colspan="4"> <div class="c1"><input name="submit" type="submit" value="Submit" /> <input type="reset" name="reset" id="reset" value="Reset" /></div> </td> <td width="2"></td> </tr> </table> </form> </td> </tr> </table> And for some reason, my php insert statement is inserting blank data from the form into the table in my database. All it's inserting is the date correctly. Here is my sql statement: <?php session_start(); //connect to server and select database $conn = mysql_connect("localhost", "webdes17_twilite", "minimoon") or die(mysql_error()); mysql_select_db("webdes17_testimonials",$conn) or die(mysql_error()); //create and issue the first query $add_comment = "insert into testimonials values ('', '".mysql_real_escape_string($_POST['comment'])."', now(), '$_SESSION[comment_owner]', '$_SESSION[comment_owner_email]', '$_SESSION[url]')"; mysql_query($add_comment,$conn) or die(mysql_error()); //create nice message for user $msg = "<p>Your comment has been added. Thank you for your testimonial!</p>"; ?> Can anyone help me with what I'm doing wrong? Here is my table statement as well, in case I have something wrong: CREATE TABLE IF NOT EXISTS `user_comments` ( `comment_id` int(11) NOT NULL auto_increment, `comment` longtext, `comment_create_time` datetime default NULL, `comment_owner` varchar(150) default NULL, `comment_owner_email` varchar(150) default NULL, `url` longtext NOT NULL, PRIMARY KEY (`comment_id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=25 ; I can't figure out what I'm doing wrong? :-( The page directs to the php page properly and displays the message for the user stating that the comment was successfully added, so it THINKS it's being added, but the fields are coming in blank. Can anyone help me with why? Quote Link to comment https://forums.phpfreaks.com/topic/188420-php-insert-help/ Share on other sites More sharing options...
joel24 Posted January 14, 2010 Share Posted January 14, 2010 try doin print_r($_POST); to see what information is being posted to the page. also with your array indexes you need to quote them if they're not integers i.e. $_SESSION[comment_owner] should be $_SESSION['comment_owner'] ... another thing, are you sure you want to be pulling these values (comment_owner etc) from the session? You have name and email fields in your form named "comment_owner" etc... Quote Link to comment https://forums.phpfreaks.com/topic/188420-php-insert-help/#findComment-994712 Share on other sites More sharing options...
twilitegxa Posted January 14, 2010 Author Share Posted January 14, 2010 Well, it's been a while since I have worked with PHP and MySQL, so maybe I don't need to get the field values from the session. LOL I was just pulling the same basic principle from another "working" message board I have created in the past, but I just couldn't get this one to work for some reason. How should I change it? I will first try changing to put the quotes around the ones you suggested, but should I be changing them from being session values? Quote Link to comment https://forums.phpfreaks.com/topic/188420-php-insert-help/#findComment-994719 Share on other sites More sharing options...
twilitegxa Posted January 14, 2010 Author Share Posted January 14, 2010 I tried changing the insert statement to this: <?php //connect to server and select database $conn = mysql_connect("localhost", "username", "password") or die(mysql_error()); $db = mysql_select_db("database_name", $conn) or die(mysql_error()); //create and issue the first query $name=mysql_real_escape_string($_POST['comment_owner']); $email=mysql_real_escape_string($_POST['comment_owner_email']); $url=mysql_real_escape_string($_POST['url']); $comment=mysql_real_escape_string($_POST['comment']); $sql="INSERT INTO user_comments (comment_id, comment, comment_create_time, comment_owner, comment_owner_email, url) VALUES ('', '$comment', now(), '$name','$email', '$url')"; mysql_query($sql,$conn) or die(mysql_error()); //create nice message for user $msg = "<p>Your comment has been added. Thank you for your testimonial!</p>"; ?> But it's still generating blank fields when I submit the form. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/188420-php-insert-help/#findComment-994733 Share on other sites More sharing options...
oni-kun Posted January 14, 2010 Share Posted January 14, 2010 Why do you not post a var_dump on the variables being inputted into your database? It's of obvious help to debug sources, as it could be a clue to your blank insertion problem. Quote Link to comment https://forums.phpfreaks.com/topic/188420-php-insert-help/#findComment-994792 Share on other sites More sharing options...
Buddski Posted January 14, 2010 Share Posted January 14, 2010 Im not sure why you are refering to the $_POST values with $_SESSION (it might be some other code you are using but I would be using $_POST instead of $_SESSION if you want to insert the posted data.. Also remove the enctype="text/plain" from your form tag and it should work. Quote Link to comment https://forums.phpfreaks.com/topic/188420-php-insert-help/#findComment-994799 Share on other sites More sharing options...
twilitegxa Posted January 14, 2010 Author Share Posted January 14, 2010 Thank you! Removing the enctype from the form worked perfectly! Thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/188420-php-insert-help/#findComment-995096 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.