Jump to content

Recommended Posts

Hello everyone!

 

I am using WAMP SERVER 1.7.2 with PHP ver: 5.2.3 and MYSQL ver: 5.0.41-community-nt

 

Im trying to get a comments form to submit the comment and carry on the post id that the comment is for...

 

Its like a blog script... the admin writes the the post, post is displayed in index page, from the i pass on the post id to the comments form,

 

the user  enters in their name, email and comment, form submitted with their name, email, entry, timestamp, and the post id for which the comment is for...

 

My trouble is im not entirely sure how im supposed to query the DB for the insertion, and how to query the DB to call up the specific post?

 

Also im not sure how to go from there once im done with that... e.g. from the query to displaying the form....

 

heres the code:

VERSION 1

<?php #script - Edit Existing Records from PHP and MYSQL for Dynamic Websites pg. 289.

/*
//check for a valid ID, through GET or POST
if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) { //accessed through view_posts.php
$id = $_GET['id'];

}elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) ) { //accessed through view_posts.php
$id = $_POST['id'];

} else { //no valid ID or not reading it from view_posts.php.
echo '<p><strong>Either there was NO ID to pull or Something didnt get passed through 
      <i>view_users.php</i></strong></p>';

}
*/
require_once ('./connect/dbc.php'); //Connect to the db.


//Check if the form has been submitted
if (isset($_POST['submitted'])) { //submit conditional

if ($_POST['sure'] == 'Yes') { //add comment

		//Define the query
		$query = "INSERT INTO comments (comment_id, post_id={$_GET['id']},
		name, email, entry, date_entered) VALUES (0, '{$_GET['id']}',
		'{$_POST['name']}', '{$_POST['email']}', '{$_POST['entry']}, NOW())";




		//Retrieve the post's details
		$query1 = "SELECT * FROM posts WHERE post_id=$id";


		//Execute the query
		if (@mysql_query ($query1)) {
			print '<p>The blog entry has been added.</p>';
		} else {
			print "<p>Could not add the entry because: <b>" . mysql_error() . "</b>. The query was $query.</p>";
		}

	}	
} //end of main submit conditional

mysql_close(); 
?>
						<form  class="form" action="comment.php" method="post">
						<table  align="center" width="90%" border="0" cellspacing="0" cellpadding="0">
						<caption align="top">
						ADD A COMMENT
						</caption>
						<tr>
						<th scope="row"> </th>
						<td> </td>
						</tr>
						<tr>
						<th align="right" scope="row">name:</th>
						<td><input type="text" name="name" size="40" maxsize="100" /></td>
						</tr>
						<tr>
						<th scope="row"> </th>
						<td> </td>
						</tr>
						<tr>
						<th align="right" scope="row">email:</th>
						<td><input type="text" name="email" size="40" maxsize="100" /></td>
						</tr>
						<tr>
						<th scope="row"> </th>
						<td> </td>
						</tr>
						<tr>
						<th valign="top" align="right" scope="row">POST:</th>
						<td><textarea name="entry" cols="40" rows="10"></textarea></td>
						</tr>
						<tr>
						<th scope="row"> </th>
						<td> </td>
						</tr>
						<tr>
						<th scope="row"> </th>
						<td>
						<input type="submit" name="submit" value="Add the Comment!" />
						<input type="hidden" name="submitted" value="TRUE" />
						<input type="hidden" name="id" value="{$_GET['id']}" />
						</td>
						</tr>
						</table>
						</form>

 

 

 

 

VERSION 2

<?php #script - Add comment to specific post

	//address error handling
	ini_set ('display_errors', 1);
	error_reporting (E_ALL & ~E_NOTICE);


	//Check if the form has been submitted
	if (isset($_POST['submitted'])) { //submit conditional

		if ($_POST['sure'] == 'Yes') { //add comment

		//Define the query
		$query = "INSERT INTO comments (comment_id, post_id={$_GET['id']},
		name, email, entry, date_entered) VALUES (0, '{$_GET['id']}',
		'{$_POST['name']}', '{$_POST['email']}', '{$_POST['entry']}, NOW())";	

		} else {

			//report on insertion
			if (mysql_affected_rows () == 1) {
				print '<p>The comment has been added.</p>';
				} else {
					print "<p>Could not be added because: "  . mysql_error() . ". The query was $query.";





			//check for a valid ID, through GET or POST
			if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) { //accessed through view_posts.php
				$id = $_GET['id'];

				}elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) ) { //accessed through view_posts.php
					$id = $_POST['id'];

			} else { 

				//Retrieve the post's details
				$sql = "SELECT post_id FROM posts WHERE post_id=$id";
				if ($r = mysql_query ($sql)) { //Run Query

				//print form with the post id in hidden form input
				while ($row = mysql_num_rows ($r)) {
				print "
				<form  class=\"form\" action=\"comment.php\" method=\"post\">
						<table  align=\"center\" width=\"90%\" border="0" cellspacing="0" cellpadding="0">
						<caption align=\"top\">
						ADD A COMMENT
						</caption>
						<tr>
						<th scope=\"row\"> </th>
						<td> </td>
						</tr>
						<tr>
						<th align=\"right\" scope=\"row\">name:</th>
						<td><input type=\"text\" name=\"name\" size="40" maxsize="100" /></td>
						</tr>
						<tr>
						<th scope=\"row\"> </th>
						<td> </td>
						</tr>
						<tr>
						<th align=\"right\" scope=\"row\">email:</th>
						<td><input type=\"text\" name=\"email\" size="40" maxsize="100" /></td>
						</tr>
						<tr>
						<th scope=\"row\"> </th>
						<td> </td>
						</tr>
						<tr>
						<th valign=\"top\" align=\"right\" scope=\"row\">POST:</th>
						<td><textarea name=\"entry\" cols="40" rows="10"></textarea></td>
						</tr>
						<tr>
						<th scope=\"row\"> </th>
						<td> </td>
						</tr>
						<tr>
						<th scope=\"row\"> </th>
						<td>
						<input type=\"submit\" name=\"submit\" value=\"Add the Comment!\" />
						<input type=\"hidden\" name=\"submitted\" value=\"TRUE\" />
						<input type=\"hidden\" name=\"id\" value=\"{$_GET['id']}\" />
						</td>
						</tr>
						</table>
						</form>";


					} else { //query didnt run

						die ('<p>Could not retieve the data because: <b>' . mysql_error() . "</b>. 
						The query was $query.</p>");
				} // end of IF query.
			}
			} else {
				echo '<p><strong>Either there was NO ID to pull or Something didnt get passed through 
      			<i>view_users.php</i></strong></p>';
			}
		}
mysql_close();
?>

 

*note that only the script is showing.*

 

Thank you in advance for any help!!!!

Link to comment
https://forums.phpfreaks.com/topic/65212-cant-get-form-to-post-comments/
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.