Jump to content

User Comments...Help


unsider

Recommended Posts

Ok, so I know you have all seen this before. The user is only allowed to post a comment after being logged in, and once logged in he can post the comment, and after the comment is input into the DB it is output with his username, and a link to his profile. and that is exactly what I'm trying to do.

 

A question. How do you keep the the users input in the form, assuming you submit it, but you are redirected back to the form because you were not logged in?

 

I am going to accomplish this with two scripts, and a DB table.

 

main.php

 


<form action="comment_process.php" method="post" id="commentform">

<p><textarea name="commenttext" id="commenttext" cols="60" rows="10" tabindex="4"></textarea></p>

<p><input name="submit" type="submit" id="submit" tabindex="5" value="Submit Comment" />
<input type="hidden" name="comment_post_ID" value="13" /></p>

</form>

 

 

comment_process.php

(processes the form, and inputs the info, redirecting it back to main.php)

 

 


if(!$session->username) {
header('main.php');
}

// HELP!!!! 

// i need to input the $session->username into the DB as the 'username', and once complete redirect back to main.


 

then back to main.php

 

$query  = "SELECT username FROM comments";
$result = mysql_query($query) or die('Error, query failed');

while($row = mysql_fetch_array($result))
{
// MORE HELP!
echo '<a href=\"userinfo.php?user=$session->username\">' . '$username '  </a>; 
// echo the $session->username with it linked to their profile
}

 

If I'm not clear, please ask questions, I'm going to work on it in here, so any help is appreciated.

Link to comment
Share on other sites

if(!$session->username) {
header('main.php');
}
else
{
//do your connection to the database here

$username=$session->username;
$comment=$_POST["commentarea"];

insert into db(user,comment) values('$username','$comment');
header('main.php');

}

try this

Link to comment
Share on other sites

Alright, well I went to bed last night, and have come to work on it again this morning. How is everyone ;D

 

main.php (form works just fine, no errors)

 

comment_process (has an error) - Parse error: syntax error, unexpected $end in /home/unsider/www/andy/comment_process.php on line 30

 

Only has 30 lines, so can someone run my code for errors?

 


<?php

include("include/session.php");


if(!$session->username) {
header('main.php');
}
else
{

if (!@mysql_select_db("mtlc?old")) {
		echo 'Unable to locate the ' . 'database.';
}

if(isset($_POST['commenttext'])) {

$commenttext = $_POST['commenttext'];
$username = $session->username;`

$sql ="INSERT INTO comments VALUES ('$username','$commenttext')";

if (!@mysql_query($sql)) {
		echo 'Error adding query: ' . mysql_error();

header('main.php');

}
}
?>

Link to comment
Share on other sites

I really need some help.

 

Parse error: syntax error, unexpected $end in /home/unsider/www/andy/comment_process.php on line 31

 

<?php

include("include/session.php");


if(!$session->username) {
header('main.php');
}
else
{

if (!@mysql_select_db("mtlc?old")) {
		echo 'Unable to locate the ' . 'database.';
}

if(isset($_POST['commenttext'])) {

$commenttext = $_POST['commenttext'];
$username = $session->username;`

$sql ="INSERT INTO comments VALUES ('$username','$commenttext')";

if (!@mysql_query($sql)) {
		echo 'Error adding query: ' . mysql_error();

header('main.php');

}
}
}
?>

 

Link to comment
Share on other sites

fixed!

<?php

include("include/session.php");


if(!$session->username) {
header('main.php');
}
else
{

if (!@mysql_select_db('mtlc?old')) {
		echo 'Unable to locate the ' . 'database.';
}

if(isset($_POST['commenttext'])) {

$commenttext = $_POST['commenttext'];
$username = $session->$username;

$sql="INSERT INTO comments VALUES ('$username','$commenttext')";

if (!@mysql_query($sql)) {
		echo 'Error adding query: ' . mysql_error();

header('main.php');

}
}
}
?>

Link to comment
Share on other sites

You are my hero, although this error is returned, and it has to do with my DB obviously, but I've never seen it before, I'm going to google search some solutions because I want to learn, but if anyone has any ideas, let me know why. Possible solutions, etc..

Link to comment
Share on other sites

The method below excuted properly, although this is the input for my DB, it's seriously flawed, and it does not redirect me back to main.php as it's supposed to.

 

id      commenttext        commentdate         username

48                                0000-00-00 00:00:00    = NOW()

 

 

And I'm not quite sure how to define NOW(), partly because it's not a PHP function.

 

<?php

include("include/session.php");


if(!$session->username) {
header('main.php');
}
else
{

if($session->username) {
echo "session->username set." . "<br>";
}

if (!@mysql_select_db('mtlc?old')) {
		echo 'Unable to locate the ' . 'database.';
}

if (mysql_select_db('mtlc?old')) {
		echo 'Able to locate database.' . "<br>	";
}


if(isset($_POST['commenttext'])) {

echo "post array was set" . "<br>";

$commenttext = $_POST['commenttext'];
$username = $session->$username;

$sql="INSERT INTO comments VALUES ('id','$username','$commenttext', '$commentdate = NOW()')";

if (!@mysql_query($sql)) {
		echo 'Error adding query: ' . mysql_error();

header('main.php');

	}
}
}
?>

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.