+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 12

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

  1. #1
    eggo9432's Avatar
    eggo9432 is offline x10 Sophmore eggo9432 is an unknown quantity at this point
    Join Date
    Jan 2011
    Posts
    133

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

    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 by eggo9432; 10-05-2011 at 04:37 PM.

  2. #2
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

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

    How do you know they aren't being inserted?

    How do you output what is stored?
    Nothing is always absolutely so.

  3. #3
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

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

    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 mysql extension is outdated and on its way to deprecation. You should switch to PDO and use prepared statements. For one thing, it will close the injection vulnerability in your script via $age as prepared statement parameters aren't vulnerable to injection.

    Don't use die when outputting HTML. You'll get invalid HTML.

    Outputting database error messages to non-admin users discloses too much information. Instead, log the MySQL error message. For some errors (such as those related to missing or invalid values), output your own error message 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 Code:
    $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.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  4. #4
    eggo9432's Avatar
    eggo9432 is offline x10 Sophmore eggo9432 is an unknown quantity at this point
    Join Date
    Jan 2011
    Posts
    133

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

    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 Code:
    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 by eggo9432; 10-06-2011 at 04:55 PM.

  5. #5
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

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

    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?
    Nothing is always absolutely so.

  6. #6
    eggo9432's Avatar
    eggo9432 is offline x10 Sophmore eggo9432 is an unknown quantity at this point
    Join Date
    Jan 2011
    Posts
    133

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

    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?

  7. #7
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

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

    Again, do you have a URL that shows the output?
    Nothing is always absolutely so.

  8. #8
    eggo9432's Avatar
    eggo9432 is offline x10 Sophmore eggo9432 is an unknown quantity at this point
    Join Date
    Jan 2011
    Posts
    133

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

    This is the output
    PHP Code:
    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>";
    }
    }
    }
    ?> 

  9. #9
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

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

    Apparently you do not want my help. Good luck.
    Nothing is always absolutely so.

  10. #10
    eggo9432's Avatar
    eggo9432 is offline x10 Sophmore eggo9432 is an unknown quantity at this point
    Join Date
    Jan 2011
    Posts
    133

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

    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...

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Line break while writing txt file using php & form data
    By vaibhavy in forum Programming Help
    Replies: 3
    Last Post: 03-31-2010, 11:28 AM
  2. AJAX: battling to parse the xml data. Please Help!
    By cGamez010 in forum Programming Help
    Replies: 14
    Last Post: 01-03-2010, 12:10 AM
  3. Reusable Insert Script - Processes Data From Many Forms
    By u023172 in forum Programming Help
    Replies: 2
    Last Post: 05-23-2009, 03:54 AM
  4. Replies: 7
    Last Post: 11-20-2008, 06:54 AM
  5. Line breaks in Form Data
    By Adam01 in forum Programming Help
    Replies: 8
    Last Post: 01-12-2008, 03:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers