Hello,
I've been trying to solve this problem that has faced me many times on my website..
I have written a forum program. It works perfectly. However, sometimes a user tells me that he tried to post his comment, but his comment didn't appear on the page, and that he didn't see the message that tells him that his comment was successfully sent! This problem happened to me too. I tried to reload the page in order to re-post the data, but it was all in vain!
I tried to edit my code and inserted after mysql_query("..") or die(mysql_error());
It was all in vain-- no MySQL error message appeared!
I'm sure the problem is not because of an error in values or MySQL syntax, or that the input may contain special characters because it doesn't.
The code is:

<?php
session_start();
$Username = $_SESSION['username'];
$commentTxt = mysql_real_escape_string($_POST['commentTxt']);
require_once('connect.php');
mysql_query("insert into ctable(Sender,Comment) values('$Username','$commentTxt')") or die(mysql_error());
echo 'Your comment was successfully sent.';
?>

Any help would be really appreciated.

I think it happens because of the web browser you're using. I guess you're using Firefox, aren't you?
It happened to me too. Using Firefox, I wrote a long comment on some page and tried to post it, but after sending the comment I realized that nothing changed in the page. It only realoded!! I also tried to reload it but nothing happened. My comment just disappeared!! :)
I tried to close the web browser then go back and post the comment again, and it worked.
I know still it is a problem, but this might be considered half or maybe quarter the solution. ;)

Yes, I'm using Firefox, and I tried what you've said, and it did work. However, I want this problem to be solved forever. I don't want to lose my website users.
Can anybody help?

Is it possible, that the user is on the page so long, or writing such a long comment that their session is timing out? I would add in some debugging / data safe to hold expected variables and see if they have a value. Never 'expect' something to be there (like a session variable) that may have gone away on its own. I'd do something like this:

<?php
    session_start();
	// set an empty variable
	$Username = '';
    $Username = $_SESSION['username'];
	$commentTxt = '';
    $commentTxt = mysql_real_escape_string($_POST['commentTxt']);
	// check if you indeed have put something useful into your expected value variables
	if ($Username != '' && $commentTxt != '') {
		require_once('connect.php');
		mysql_query("insert into ctable(Sender,Comment) values('$Username','$commentTxt')") or die(mysql_error());
		echo 'Your comment was successfully sent.';
	} else {
		// uh oh, username or commentTxt was blank  which one was it
		// failed to post, redirect or some other such code to handle the error. of a blank 
		echo "There was an error posting the comment " . $commentTxt . " for username " . $Username . "<br>";
		if ($commentTxt == '') {
			// do stuff specific if ctxt was empty....
		} else if ($Username == '') {
			// do stuff specific if username was empty...
		} else {
		    // total failure... 
		}

	}
?>
Member Avatar for diafol

Need more info on the error. Does this happen in a number of browsers, or just one? Does the error appear when a certain string is passed or every time?

try echoing out your queries and see if they make sense.
you could copy and paste them via phpmyadmin or your favourite MySQL GUI - they're usually a bit more forthcoming with errors.

In fact I did writet a bunch of lines to check these stuff, like whether the session is set or not, and whether the comment is more than 5 characters long.
The problem is not with the session 'cause the code checks at the very beginning whether or not the session is set like this:

<?php
session_start();
if(isset($_SESSION)){
//THE CODE IS HERE
}
else{
echo 'ERROR: You shoul log in so that you can send your comments!';
}

As I said, the program often works perfectly. The error rarely occurs, and when it occurs I, or any other user don't know what's wrong. When it happened to me, I tried to re-write the comment, I tried writing a random set of characters like: aaaaaaaa, or bbbbbbb. However, nothing changed and still the comment was not sent.

Since when I try closing the web browser and re-opening it in order to post the comment again it works, then I can't try sending the comment on another web browser because the porblem appeared accidently.

Member Avatar for diafol

Is it that your field size in the DB is too small. Is it set to 'text' datatype? It should be.

It is set to 'text' datatype. I don't think it has to do with text length because I did try to send short values when the error occured. The solution was to close the browser and re-open it. The same solution worked for the other users, but I don't know what their browser was.

Member Avatar for diafol

Could the username have ' or " in them to kill the query?

can you replicate this problem right now? since you have debugging code in there what are the values of username, and commenttxt when it fails, you should be able to tell us this. Perhaps its a mysql connections issue where you are attempting to connect to the database 'too' often and have too many connections open, but I would assume if that was the case you would get a value in mysql_error().
make sure all error reporting is turned on. drop this in after session_start();

ini_set('display_errors',1); 
error_reporting(E_ALL);

More feedback is needed of what your actual problem is.
Since this works 'most' of the time there should only be a handful of questions.
1. do you receive all of the 'expected' data (are both fields set).
2. what is the actual query that you are sending to your db? (as ardav speculated, perhaps user input is breaking the query itself, so you may need to sanitize the commentTxt before sending to db) maybe escape all ' marks, as a matter of fact why don't you try addslashes:

function slashes($str) {
     if (!empty($str)) {
	 return addslashes($str);
     } else {
	 return $str;
     }
}
// or change your line to 
  $commentTxt = mysql_real_escape_string(addslashes($_POST['commentTxt']));

again, it is hard to speculate without any more knowledge of what is going wrong.

can you replicate this problem right now? since you have debugging code in there what are the values of username, and commenttxt when it fails, you should be able to tell us this. Perhaps its a mysql connections issue where you are attempting to connect to the database 'too' often and have too many connections open, but I would assume if that was the case you would get a value in mysql_error().
make sure all error reporting is turned on. drop this in after session_start();

ini_set('display_errors',1); 
error_reporting(E_ALL);

More feedback is needed of what your actual problem is.
Since this works 'most' of the time there should only be a handful of questions.
1. do you receive all of the 'expected' data (are both fields set).
2. what is the actual query that you are sending to your db? (as ardav speculated, perhaps user input is breaking the query itself, so you may need to sanitize the commentTxt before sending to db) maybe escape all ' marks, as a matter of fact why don't you try addslashes:

function slashes($str) {
     if (!empty($str)) {
	 return addslashes($str);
     } else {
	 return $str;
     }
}
// or change your line to 
  $commentTxt = mysql_real_escape_string(addslashes($_POST['commentTxt']));

again, it is hard to speculate without any more knowledge of what is going wrong.

Thank you, but the I couldn't find anything that distincts the case when the error occurs. The same input data and session values are sent when the error occurs, and when it doesn't. The data I sent when the error ocurred was the same when the error didn't occur. I only tried to close the browser and re-open it and send the same data and it did work perfectly.
I don't think I need to use addslashes() because mysql_real_escape_string() is better for MySQL queries (as stated in PHP.NET):
It's highly recommended to use DBMS specific escape function (e.g. mysqli_real_escape_string() for MySQL or pg_escape_string() for PostgreSQL), but if the DBMS you're using doesn't have an escape function and the DBMS uses \ to escape special chars, you can use this function. This would only be to get the data into the database, the extra \ will not be inserted. Having the PHP directive magic_quotes_sybase set to on will mean ' is instead escaped with another '. (www.php.net/manual/en/function.addslashes.php)

ok, addslashes, not for you... I don't know where to go from here, I would have to replicate the issue. can you give a test site and let me run the code,
how do you know when it works and when it doesn't, just by looking at db or can you tell can you wrap your query call and give the returned value... it should be 1 if successful, and 0 if not.
something like this:

$retid = mysql_query("insert into ctable(Sender,Comment) values('$Username','$commentTxt')") or die(mysql_error());
if ($retid) {
     echo 'Your comment was successfully sent.';
} else {
     echo 'There was a problem submitting your post please try again';
}

I think it's enough to write:

or die(mysql_error());

And I think, according to his code, that he should know by either seeing the message: "Your comment was successfully sent", or by seeing the MySQL error, and if the data was not set, he would see a message (also according to his code) that tells him that he should send valid data.

he keeps claiming there is no mysql_error() message returned.
hey, are you now getting an error message? what is it?
so it may be enough to write that and I would tend to agree, but the post is 'magically' disappearing when sent in firefox and he also claims to have data in both input fields... so what is happening?
what do you think is happening?
I'm just trying to get him to tell me what error message he is getting to no avail.
so far this question has not been answered...
what 'IS' happening upon failure?

Just a thought, and we haven't seen a lot of the code, but, is it possibly you are not closing your database connections and opening too many.
I'm guessing the the forum post can be a simple form from multiple pages posting to this short php script to record the data can you confirm that you are closing your db connection after you send your query.
question 2 what does your form look like, and is this an ajax post? if so would like to see the form code itself and how you are handling the post to this form processing script.

Member Avatar for diafol

what if you log the query in a text file by appending it along with a timestamp and other data, e.g. browser etc?
It will show you if the query is valid.

The page is only being reloaded on failure. The same page that appears when I only want to view the thread is being viewed on failure.
Actually, I'm not closing the MySQL connections, but does the connection automatically close when you close the browser? Also, there aren't many members there in my forum. Not more than 5 users view the forum at one time. So, does it matter in this case whether or not I'm closing the connections?
Besides, as far as I know, the connections that are not in use are automatically closed using the Zend Engine that comes with PHP core. My PHP version is above 5, and I think Zend Engine is already in the PHP core, right?
Also, there are other MySQL queries that are being executed successfully such as retrieving the thread and the comments from the database.
Thanks

could you post your form code?
is this an ajax call or a post to self?
a very relevant question considering we have yet to get anywhere on this issue.
just trying to help.

I've written the code without using AJAX. I'm not using any technique or programming language other than PHP, HTML, and maybe a little bit of JS, but without AJAX.
I really appreciate your effort for helping me. Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.