Jump to content

PHP INSERT Help!


twilitegxa

Recommended Posts

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?

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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. :)

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.