Jump to content

More PHP INSERT Help!


twilitegxa

Recommended Posts

I am trying to make a reply type of form, and I want the reply form to insert the form data to the table and detect what post it is replying to. So far I have this form:

 

<td>
<form name="comment" id="comment" action="reply_form.php" method="post">
<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="reply">Reply:</label>
</td>
<td>
<textarea name="reply" id="reply" 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>

 

And I have this insert statement on my page:

 

<?php

//connect to server and select database
$conn = mysql_connect("localhost", "username", "password")
    or die(mysql_error());
$db = mysql_select_db("webdes17_testimonials", $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']); 
$reply=mysql_real_escape_string($_POST['reply']);
$reply_comment_id = $_GET['comment_id'];
$sql="INSERT INTO replies (reply_id, comment_id, reply, reply_create_time, reply_owner, reply_owner_email) VALUES ('', '$reply_comment_id', '$reply', now(), '$name','$email')"; 

mysql_query($sql,$conn) or die(mysql_error());

//create nice message for user
$msg = "<p>Your comment has been added. Thank you for your feedback! You will be redirected to the Testimonials page in a moment. If you aren't forwarded 
to the new page, please click <a href=\"http://www.webdesignsbyliz.com/testimonials.php\">
here</a>. 
</p>";
?>

 

And I have a link on the main page that takes the user to the reply page that sends the comment_id like this:

 

<a href=\"reply.php?comment_id=$comment_id\">Reply</a>

 

But my insert statement is not inserting anything. What am I doing wrong? It's not giving the error; It's outputting my message saying that the reply has been added. Here is my table sql statement, just in case:

 

CREATE TABLE IF NOT EXISTS `replies` (
  `reply_id` int(11) NOT NULL auto_increment,
  `comment_id` int(11) NOT NULL,
  `reply` longtext,
  `reply_create_time` datetime default NULL,
  `reply_owner` varchar(150) NOT NULL,
  `reply_owner_email` varchar(255) NOT NULL,
  PRIMARY KEY  (`reply_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

 

Can anyon help me with what I am doing wrong this time? I know I must be missing something simple, but I can figure it out! Please help!

Link to comment
Share on other sites

change:

$sql="INSERT INTO replies (reply_id, comment_id, reply, reply_create_time, reply_owner, reply_owner_email) VALUES ('', '$reply_comment_id', '$reply', now(), '$name','$email')"; 

mysql_query($sql,$conn) or die(mysql_error());

into:

$sql="INSERT INTO replies (reply_id, comment_id, reply, reply_create_time, reply_owner, reply_owner_email) VALUES ('', '$reply_comment_id', '$reply', now(), '$name','$email')"; 

if (mysql_query($sql,$conn))
{
   // code to do if insert succeeds
}
else
{
   print('Could not INSERT comment to database. Error given: '.mysql_error().'<br/>'.'Query sent: '.$sql.'<br/>'."\n");
   // other code to do if insert fails (if any)
}

and you will be able to see if the INSERT query succeeds or fails; in the event of failure: what the SQL query was and what error mysql returned.

 

without the exact query being sent and the error mysql is throwing, there's not much we can do to help you.

 

if it is not inserting data, and not throwing errors, then there must be something else wrong. you need to check every step of your code and verify all the data used etc. to trace where a problem may be.

Link to comment
Share on other sites

When you are sending them to the reply form you need to either pass the $_GET['comment_id'] into the form action

action="reply_form.php?comment_id=<?php echo $_GET['comment_id']; ?>" OR alternatively you can create a hidden field with the comment_id in it and reference it with post on submit..

 

 

Link to comment
Share on other sites

Okay, my insert statement is working except for my $comment_reply_id, which I send from the main page to the second page, but I need to know how I can send it to the third page from the second page. I send it to the second page from the first page like this:

 

<a href=\"reply.php?comment_id=$comment_id\">Reply</a>

 

How do I send it from the second page to the third? The second page is the form:

 

<form name="comment" id="comment" action="reply_form.php" method="post">
<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="reply">Reply:</label>
</td>
<td>
<textarea name="reply" id="reply" 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>

 

And the third page is the insert page:

 

<?php

//connect to server and select database
$conn = mysql_connect("localhost", "username", "password")
    or die(mysql_error());
$db = mysql_select_db("webdes17_testimonials", $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']); 
$reply=mysql_real_escape_string($_POST['reply']);
$reply_comment_id = $_GET['comment_id'];
$sql="INSERT INTO replies (reply_id, comment_id, reply, reply_create_time, reply_owner, reply_owner_email) VALUES ('', '$reply_comment_id', '$reply', now(), '$name','$email')"; 

if (mysql_query($sql,$conn))
{
   // code to do if insert succeeds
}
else
{
   print('Could not INSERT comment to database. Error given: '.mysql_error().'<br/>'.'Query sent: '.$sql.'<br/>'."\n");
   // other code to do if insert fails (if any)
}
//create nice message for user
$msg = "<p>Your comment has been added. Thank you for your feedback! You will be redirected to the Testimonials page in a moment. If you aren't forwarded 
to the new page, please click <a href=\"http://www.webdesignsbyliz.com/testimonials.php\">
here</a>. 
</p>";
?>

 

Link to comment
Share on other sites

As you can see, I've tried using

 

$reply_comment_id = $_GET['comment_id'];

 

But it's not getting the comment_id from the previous page. When I send the user from the main page to the reply form, I send the comment id with the link, so it should be able to be picked up on the reply form page, but I can't remember how to do this. Can anyone help?

Link to comment
Share on other sites

Make a hidden input field in the form on the second page and echo the contents of the $_GET into the value

 

<input type='hidden' name='comment_id' value='<?php echo $_GET['comment_id']; ?>' />

 

Then in the third page, it is accesible as $_POST['comment_id']

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.