Beginner needs help getting a comment box to input data into database


#1

I have been trying to create a simple comments box by following tutorials online but I cant seem to get my comment box to input any data into my database. I set the database up on phpMyAdmin.

I can’t understand what isn’t functioning correctly as all the code im sure is correct.

Any help somone has would be greatly appreciated.

[php]<?php
mysql_connect(“localhost”,“root”,“root”);
mysql_select_db(“commentbox”);
$name=$_POST[‘name’];
$comment=$_POST[‘comment’];
$submit=$_POST[‘submit’];

$dbLink = mysql_connect(“localhost”, “root”, “root”);
mysql_query(“SET character_set_client=utf8”, $dbLink);
mysql_query(“SET character_set_connection=utf8”, $dbLink);

if($submit)
{
if($name&&$comment)
{
$insert=mysql_query("INSERT INTO commenttable (name,comment) VALUES (’$name’,’$comment’) ");
}
else
{
echo “Please fill out all fields.”;
}
}
?>

Comment box
Name:
Comment:
<?php $dbLink = mysql_connect("localhost", "root", "root"); mysql_query("SET character_set_results=utf8", $dbLink); mb_language('uni'); mb_internal_encoding('UTF-8'); $getquery=mysql_query("SELECT * FROM commenttable ORDER BY id DESC"); while($rows=mysql_fetch_assoc($getquery)) { $id=$rows['id']; $name=$rows['name']; $comment=$rows['comment']; echo $name . '
' . '
' . $comment . '
' . '
' . '
' ;} ?> [/php]

#2

i don’t see anythingn wrong, but try this:
[php]

<?php mysql_connect("localhost","root","root"); mysql_select_db("commentbox"); $name=$_POST['name']; $comment=$_POST['comment']; $dbLink = mysql_connect("localhost", "root", "root"); if(isset($_POST['submit'])) { if(!empty($name) && !empty($comment)) { $insert=mysql_query("INSERT INTO commenttable (name,comment) VALUES ('$name','$comment')") or die(mysql_error()); if(!insert) { echo "Something went wrong"; } else { echo "Comment has been added for $name"; } } else { echo "Please fill out all fields."; } ?>[/php]

#3

I also see that you make two separate connections to your database and never close them.
You should close a connection when done if you are planning to open it a second time.

Also, did you check your database to see if the comments were already there?
You can’t write the same record twice. You can if the two fields written are not indexes.
Might be an issue there…


#4

Closing the connections made no difference and that code snippet unfourtunately made no difference also.

Perhaps I have set up phpMyAdmin wrong, when I enter a name and a comment into the boxes and click submit, the comment appears on the html page as a displayed comment. Also when I have closed and reloaded the page my previous comments are still viewable on the page. Does this not mean they are being stored somewhere?

Thanks for your help, it is greatly appreciated.


#5

Perhaps we do not understand what you are asking help for!

You said you type a name and comment and it saves.
And, when you reopen the page the data is there.
You showed code that saves the data to the database.
Data is returned and shown to you. So, it sounds like your project is working. So what are you asking of us?

We can help, but, not sure what you need.


#6

Unfourtunately it seems it was just a case of me not understanding phpMyAdmin and getting a bit confused. I couldnt understand where the data was entering into.

Beginner indeed. Hit a block with a similar comment box I think id rather use than the one i posted up here.

[php]<?php
require(‘connect.php’);
$name=$_POST[‘name’];
$comment=$_POST[‘comment’];
$submit=$_POST[‘submit’];

if($submit)
{
if($name&&$comment)
{
$insert=mysql_query(“INSERT INTO comment (name,comment) VALUES (’$name’,’$comment’)”);
}
else
{
echo “Please fill out all of the fields!”;
}
}
?>

Comment Box
Name:
Comment:
<?php $getquery=mysql_query("SELECT * FROM comment ORDER BY id DESC"); while($rows=mysql_fetch_assoc($getquery)) { $id=$rows['id']; $name=$rows['name']; $comment=$rows['comment'];
echo $name . '<br />' . $comment . '<br />' .'<br />'

;}
?>

[/php]

In this case the data enters the database but isnt be relayed to the screen. Once again any advice is so greatfully welcomed!

Sorry for the earlier post, was quiet a simple mistake in the end…


#7

Okay, well, you have nice, standard php code.
But, where you display the line of comments, you are missing a semi-colon at the end of the line.
Then, the next line is just a semi-colon. Move the semi-colon to the end of the echo line!

Should work…


#8

Almost, needs to move the variables inside the if statement. Otherwise, its capturing nothing but air


#9

I added a semi-colon to the end of the echo line and removed it from the line below but it’s still not posting on screen.

[php]<?php>
$getquery=mysql_query(“SELECT * FROM comment ORDER BY id DESC”);
while($rows=mysql_fetch_assoc($getquery));
{
$id=$rows[‘id’];
$name=$rows[‘name’];
$comment=$rows[‘comment’];

echo $name . '<br />' . $comment . '<br />' .'<br />';

}
?>[/php]

Could you clarify what you mean richei by moving the variables inside the if statement, I thought that only the code above was neccesary for posting the comment on screen?


#10

In that top section of php, have your name and comment variables outside the if statement does nothing, its setting them to empty space, or possibly to something that’s unintended. move them inside the if statement. $submit isn’t needed at all.

Your first section of php (on top of the page) should be:

[php]

<?php require('connect.php'); if(isset($_POST['submit'])){ $name=$_POST['name']; $comment=$_POST['comment']; if($name && $comment) { $insert=mysql_query("INSERT INTO comment (name,comment) VALUES ('$name','$comment')") or die(mysql_error()); } else { echo "Please fill out all of the fields!"; } }?>

[/php]

Since you don’t have any confirmation or error messages, i’d check to see if there’s anything in the comments table. The only thing i see wrong is the php page, and you have a semi-colon where the { should be on the while line. I’m surprised it didn’t give you a warning. if it didn’t then you need to turn error reporting on.


#11

I understand what you meant Richei.

I updated my code to match you code but for some reason once I had done this my comments box was no longer visable on screen. Even stranger was when I changed my code back to the original code I was using the comments box still was not visable.

Also, I have been following a tutorial to create a content managment system and I had created a login.php page with a form which was visable when I initially created it. Then after trying to implement the php code needed to check my database for the login details the form was suddenly no longer visable on my login.php page.

I only mentioned this other case as I wondered if it might be something to do with MAMP that I havent set up correctly or something universal between the comment box and login page.

Thank you again for your help. Any advice is grealty appreciated!


#12

Well, if want, ill do some 1on 1 with,u to figure this out


#13

If it helps to figure it out, I removed all the php from the login.php page I created and that allowed my form to become visable again. Any ideas?


#14

Well, there have been many many requests to help with login projects. We can help.
But, it is much easier to see the code. Please post the login page and the PHP code page.
Just make sure not to show your log-in connection info for your database.

Then, we can suggest how to fix it.


#15

Thank you for the offer, feel a bit lost with problem solving as im a little new to PHP and MySQL.

Ill give you the three pages ive been working with:

login.php
[php]

Basic CMS - Admin Area - Login <?php session_start(): if(isset($_SESSION['user'])) { header("Location: index.php"); } else { ?>
Username:
Password:
<?php } ?> [/php]

index.php
[php]

Basic CMS - Admin Area <?php session_start(): if(isset($_SESSION['user'])) { ?>

Logged In! Welcome <?php> echo $_SESSION['user']; ?>

<?php } else { header("Location: admin/login.php"); } ?> [/php]

dologin.php
[php]<?php
include(‘includes/functions.php’);

if(isset($_POSRT([‘login’]))

{



	if($_POST['username']))

	{
	
		if($_POST['password']))
	
		{
		
			$query = mysql_query("SELECT * users WHERE Username = mysql_real_escape_string ($_POST['username'])") or die(mysql_error());
		
			$user = mysql_fetch_array($query);

	
	if(md5($_POST['password'] == $user['Password'])
		{
		echo "Login successful";

		$_SESSION['user'] = $user['Username'};

		header("Location: index.php");

		}
		else

		{
		echo "Please check your login details!";

		include('login.php');

		}

	} else

		{

		echo "Please check your password is correct!";

		include('login.php');

	}

}  else


	{

	echo "Please check your username is correct!";

	include('login.php');

} else

{

echo “Please check you filled out the login form!”;

include('login.php');

?>
[/php]

Hope these are laid out well enough, thanks for taking the time to help me out!


#16

First, in the login.php, change this:
if(isset($_SESSION[‘user’]))
{
header(“Location: index.php”);
} else
{
to this:
if(isset($_SESSION[‘user’]))
header(“Location: index.php”);

(No else needed! As you just want an already logged in user to move on…)
And, remove this line near the end:

<?php } ?>

(not needed)

In dologin.php, the first test for the submit is wrong… This:

if(isset($_POSRT([‘login’]))

Should be:

if(isset($_POST([‘login’]))

So, the dologin.php file would never do anything… Perhaps that is your problem!

Lastly, in index.php, the first section will not work. So, in login.php, if a user is logged in it sends them to the index.php file. All ok with that. In the index.php file, if they are not logged in you try to send them to the login.php page. BUT, you do this with headers which will not work after you have HTML on the page. Therefore, I suggest changing this to something more like this version:
[php]

<?PHP session_start(); if(!isset($_SESSION['user'])) header("Location: admin/login.php"); ?> Basic CMS - Admin Area Logged In! Welcome <?php> echo $_SESSION['user']; ?> [/php] NOTE: I changed the check for user logged in to NOT-user-logged-in... And, removed all the useless code. The new version of index.php, checks for a user NOT logged in and redirects them. If logged in, it goes on jsut as normal. Much simpler and easy to follow code... No if-else's... Hope that helped! Good luck...

#17

Amazing help, thank you so much, im grasping more as I go, it’s just a been a bit of a challenge getting up and running.

Im sure I’ll have some more issues as I progress with this cms system. I hope you don’t mind advising me a bit more as I go.

Thanks again to all of you!


#18

i’ll take a more indepth look when i get home, but one glaring error i see is in dologin, that very first POST is spelled wrong.


#19

Gilestodd, no problem… If the next problem is in the same line, just continue here.
If it is a new problem, create a new post with a title that indicates the new problem.

Also, just for your info, you can search this site for answers to similarly and previously asked questions.
(Might save you a lot of time!)

Good luck…


#20

I may have spoken to soon!

As I said im following a tutorial and I believe I need to get to the index.php page after logging in. Which I gather is the admin area. After logging in I am redirected to the dologin.php page and as such no message is displayed letting me know ive logged in correctly.