Line breaks are not being inserted into mysql. Please Help!

eggo9432

New Member
Messages
151
Reaction score
1
Points
0
I am using ajax to insert a form using GET. When that form is submitted, it goes into a mysql database. I know that the error is occurring when this data is being submitted into mysql and not when I am retrieving it. My problem is that all line breaks, and when you press the "enter" key are not being submitted into the database. All of the text just goes in as a straight line without breaks or anything of the sort. I would appreciate any help as to figuring out how to get these breaks to actually be inserted into mysql because this is a big problem for my site. Any help is very much appreciated.

Here is the code for the ajax that I am using

$echovar400=
"
<script language='javascript' type='text/javascript'>

function ajaxFunction(){
var ajaxRequest;

try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject('Msxml2.XMLHTTP');
} catch (e) {
try{
ajaxRequest = new ActiveXObject('Microsoft.XMLHTTP');
} catch (e){
// Something went wrong
alert('Your browser broke!');
return false;
}
}
}
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('pagecomments');
ajaxDisplay.innerHTML = ajaxRequest.responseText;

}
}
var age = document.getElementById('age').value;
var wpm = document.getElementById('wpm').value;
var queryString = '?age=' + age + '&wpm=' + wpm;
ajaxRequest.open('GET', 'ajaxprofilechat.php' + queryString, true);
ajaxRequest.send(null);

}

</script>

<form name='myForm' method='GET' >
<textarea rows='4' name='message' class='comment' maxlength='250' id='age' wrap='hard'> </textarea><br><h40>
<input type='hidden' id='wpm' value='$profilename'/>
<input type='button' onclick='ajaxFunction()' value='Comment' />
</form>
";
}
?>
I realize that is not the start of the php, but the rest is unimportant.

here is the code for ajaxprofilechat

$age = strip_tags($_GET['age']);
$wpm = $_GET['wpm'];
// Escape User Input to help prevent SQL Injection
$wpm = mysql_real_escape_string($wpm);
$chatname6 = ($_SESSION['username']);
$message6 = $_GET['site_message'];
$month6 = date("F");
$dayofmonth6 = date("d");
$year6 = date("Y");
$date10 = "$month6 $dayofmonth6 $year6";

$hours6 = date("g");
$min6 = date("i");
$sec6 = date("s");
$amorpm6 = date("A");
$time6 = "$hours6:$min6 $amorpm6";

if (strlen($age)>4)
{
mysql_connect("","","") or die($error);
mysql_select_db("") or die($error);
mysql_query("INSERT INTO guestbook VALUES ('','$wpm','$chatname6','$age','$date10','$time6')");
echo "&nbsp;<h80><b>Comment Posted</b></h80<p><p>";
}
else
{
echo "&nbsp;<h80><b>Your comment must be greater than four characters</b></h80><p>";
}
?>
Any help would be great. Thanks!

if you need to see my site to look at the error, here is a link to my profile page http://www.pearlsquirrel.com/profile.php?u=eggo

I know for a fact that the error is occurring when the data is being put into mysql. I have literally spent like 3 hours a day for the past week trying to fix this problem. I really need help. Thanks.
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
How do you know they aren't being inserted?

How do you output what is stored?
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Please use
PHP:
, [html] or [code] tags (as appropriate) to separate and format code.

Are the missing newlines you're referring to supposed to be in the 'wpm' parameter? Did you check the data in the database, or are you judging it based on how it's displayed in the browser? What are the 'age' and 'wpm' parameters/values supposed to hold? "wpm" reads as "words per minute", and "age" would seem to refer to a timespan. If they're supposed to be something else, they need more appropriate names.

The [URL="http://x10hosting.com/forums/programming-help/162529-php-begin-deprecation-ext-mysql-start-moving-your-development-pdo-now.html"]mysql extension[/URL] is outdated and on its way to deprecation. You should switch to [URL=http://php.net/PDO]PDO[/URL] and use [URL=http://www.php.net/PDO.prepared-statements]prepared statements[/URL]. For one thing, it will close the injection vulnerability in your script via [c]$age[/c] as prepared statement parameters aren't vulnerable to injection.

[URL="http://www.phpfreaks.com/blog/or-die-must-die"]Don't use [c]die[/c][/URL] when outputting HTML. You'll get invalid HTML.

Outputting database error messages to non-admin users [URL=http://msdn.microsoft.com/en-us/library/ms995351.aspx#securityerrormessages_topic2]discloses too much information[/URL]. Instead, log the MySQL error message. For some errors (such as those related to missing or invalid values), output your own [url=http://developer.apple.com/library/mac/documentation/UserExperience/Conceptual/AppleHIGuidelines/Windows/Windows.html#//apple_ref/doc/uid/20000961-TP10]error message[/url] to the user and what action the user can take to address it. For the rest, inform the user that there was an internal error.

You don't need to make multiple calls to date, then combine the results. Do it all in one call. You'll be less likely to forget the seconds if you do this.
[php]$date10 = date('F d Y');
$time = date('g:i:s A');

Alternatively, you can do this in SQL using function like DATE(), TIME() and NOW(). While you're at it, you could combine the columns in the table as a TIMESTAMP column with the DEFAULT CURRENT_TIMESTAMP property, letting MySQL set it automatically when a new row is created.

You should explicitly specify the columns in the INSERT statement.

There is no <h80> HTML element.

The numbers at the end of the variable names aren't descriptive, which every variable name should be. If you have variables with the same prefix and different numeric suffixes, you should likely be using an array.

<br/> isn't semantic; use something more appropriate, such as a paragraph element. Similarly, <b> should be replaced with semantic elements and CSS.
 

eggo9432

New Member
Messages
151
Reaction score
1
Points
0
Thank you for all of the help! $age refers to the id of the textbox that I am using GET to retrieve the data from. I never switched the variable names b/c I was suing it for another script and just modified that script to fit this one. I am looking at the database and that is why I think that is where the error is. Even if there are breaks in the textbox, the data that is inserted into the database is just one long strand that only keeps the spaces and not the breaks, that is why I think that the error is occurring while I am inserting the data into the database.

Here is the code that retrieves the data:
PHP:
mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname) or die(mysql_error());
$id2 = $_GET['q'];
$id2 = mysql_real_escape_string($id2);
$getdata8 = mysql_query("SELECT * FROM guestbook WHERE profilename='$id2' ORDER BY id DESC");
$getdata10 = mysql_query("SELECT * FROM guestbook WHERE profilename='$id2' ORDER BY id DESC");
while ($row8 = mysql_fetch_assoc($getdata8))
{
 $row10 = mysql_fetch_assoc($getdata10);
 $id40 = $row10['id'];
 $name8 = $row8['name'];
 $message8 = $row8['message'];
 $date8 = $row8['date'];
 $time8 = $row8['time'];
 echo"
 ".nl2br($message8)."<br>
<h8>&nbsp;<a href=/profile.php?u=$name8>$name8</a> at $time8 on $date8 $echovar800 $echovar888</h8>
";

that is not all of it, only part of the php.

Thanks guys, if you have anything else to say that would be greatly appreciated. I am going to go try to implement some changes and see if i can fix this problem!

---------- Post added at 09:53 PM ---------- Previous post was at 08:37 PM ----------

I think that my best bet might be to try converting this ajax into POST instead of GET. However, I have tried and failed. Would any of you possibly know how i could do this?
I would have to convert this into POST
Code:
<script language='javascript' type='text/javascript'>

function ajaxFunction(){
	var ajaxRequest;  
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject('Msxml2.XMLHTTP');
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject('Microsoft.XMLHTTP');
			} catch (e){
				// Something went wrong
				alert('Your browser broke!');
				return false;
			}
		}
	}
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById('pagecomments');
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
			
		}
	}
	var message = document.getElementById('message').value;
	var wpm = document.getElementById('wpm').value;
	var queryString = '?message=' + message + '&wpm=' + wpm;
	request.open("POST", ajaxprofilechat.php, true);	
	http.send(null); 

}

</script>
As you can see I have attempted to use the POST method, but I must have an error somewhere because this section of script is not working. Did I overlook something when converting it from GET to POST? Any help would be appreciated.
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Do you have a page with the results we could look at?

You do understand that newline characters (as entered in a textarea in a form) do not make line breaks when displayed on a web page?
 

eggo9432

New Member
Messages
151
Reaction score
1
Points
0
Ok guys I fixed the major problem by converting my script to using the POST method. Now, if you press enter, that line break is being inserted into the database and that is working. However, when you reach the end of the textbox and it goes down to a new line, that break is not being inserted. Does anyone know how I might possibly fix that?
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Again, do you have a URL that shows the output?
 

eggo9432

New Member
Messages
151
Reaction score
1
Points
0
This is the output
PHP:
mysql_connect($dbhost, $dbuser, $dbpass);

mysql_select_db($dbname) or die(mysql_error());

$id2 = $_GET['q'];

$id2 = mysql_real_escape_string($id2);
$getdata8 = mysql_query("SELECT * FROM guestbook WHERE profilename='$id2' ORDER BY id DESC");
$getdata10 = mysql_query("SELECT * FROM guestbook WHERE profilename='$id2' ORDER BY id DESC");
while ($row8 = mysql_fetch_assoc($getdata8))
{
 $row10 = mysql_fetch_assoc($getdata10);
 $id40 = $row10['id'];
 $name8 = $row8['name'];
 $message8 = $row8['message'];
 $date8 = $row8['date'];
 $time8 = $row8['time'];
 echo"
 ".nl2br($message8)."<br>
<h8>&nbsp;<a href=/profile.php?u=$name8>$name8</a> at $time8 on $date8 $echovar800 $echovar888</h8>
";
 if ($username==$id2){
echo "<h8><input type='hidden' id='wpm2' value='$id40'><a href='javascript:;' onclick='LinkOnClick2($id40);'>(delete?)</a></h8><p></td>
	";
}
else
{
if ($name8==$username)
{
echo "<h8><input type='hidden' id='wpm2' value='$id40'><a href='javascript:;' onclick='LinkOnClick2($id40);'>(delete?)</a></h8><p></td>
	";
}
else
{
echo "<p>";
}
}
}
?>
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Apparently you do not want my help. Good luck.
 

eggo9432

New Member
Messages
151
Reaction score
1
Points
0
What do you mean I do not want you help. There is so specific url that shows output, that is just an ajax file. I'm sorry if I misunderstood the question. If you mean a specific page url to my website then try this http://www.pearlsquirrel.com/profile.php?u=eggo Sorry for the misunderstanding...
 

Dead-i

x10Hosting Support Ninja
Community Support
Messages
6,084
Reaction score
368
Points
83
Are you talking about MySQL? If so, separate queries with ; for example:

Code:
SELECT * FROM 'mytable';
SELECT * FROM 'myothertable';
 
Last edited:
Top