doddsey_65 Posted July 30, 2011 Share Posted July 30, 2011 Everytime i submit text it is sent to the database with new lines being replaced with u00a. Anyone know what causes this? Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted July 30, 2011 Share Posted July 30, 2011 Please post all relevant code. Quote Link to comment Share on other sites More sharing options...
doddsey_65 Posted July 30, 2011 Author Share Posted July 30, 2011 here is the code which inserts the content into the database: $sql = "INSERT INTO ".TBL_PREFIX."posts (p_tid, p_fid, p_poster, p_name, p_content, p_time_posted) VALUES (:topic_id, :forum_id, :poster, :post_name, :post_content, :time_posted)"; // prepare the sql statement $sth = $db->prepare($sql); // execute the sql statement $sth->execute(array( ':topic_id' => $data->post[3]->topic_id, ':forum_id' => $data->post[2]->forum_id, ':poster' => $user['u_username'], ':post_name' => $data->post[0]->subject, ':post_content' => $data->post[1]->content, ':time_posted' => time() ) )or die($db->printError($sql)); Quote Link to comment Share on other sites More sharing options...
voip03 Posted July 30, 2011 Share Posted July 30, 2011 syntax error Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted July 30, 2011 Share Posted July 30, 2011 Review this, http://dev.mysql.com/doc/refman/5.0/en/insert.html For the correct syntax on inserting into Mysql. Quote Link to comment Share on other sites More sharing options...
doddsey_65 Posted July 30, 2011 Author Share Posted July 30, 2011 theres nothing wrong with the actual sql otherwise it would have given an error. I am using PDO which is a database abstraction layer Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted July 30, 2011 Share Posted July 30, 2011 Ah your right. I didn't scroll down to review your code more carefully. A little later today I will review your code and see if I can provide feedback if no-one else has. Quote Link to comment Share on other sites More sharing options...
doddsey_65 Posted July 30, 2011 Author Share Posted July 30, 2011 i think its probably to do with JSON rather than the php, when i view the JSON response in firebug the u00a is already there Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted July 30, 2011 Share Posted July 30, 2011 Can you show us a test URL, of the live area you are working with, so I can play around with the results. Also where is the JSON coming from, can you copy/paste the code pertaining to the JSON generation. Quote Link to comment Share on other sites More sharing options...
doddsey_65 Posted July 30, 2011 Author Share Posted July 30, 2011 you wont be able to test it without registering, sorry. I think it could be json_encode() which is converting new lines to 'u00a'. here is the code to insert to the database: $db = db_pdo::$instance; $data = json_decode(stripslashes($data)); $user = sessionsClass::$data; if(empty($data->post[0]->subject)) { $data->post[0]->subject = 'No Subject'; } if(empty($data->post[1]->content)) { $data->status[0]->error = 'You must supply a message body'; } // sql for post insertion $sql = "INSERT INTO ".TBL_PREFIX."posts (p_tid, p_fid, p_poster, p_name, p_content, p_time_posted) VALUES (:topic_id, :forum_id, :poster, :post_name, :post_content, :time_posted)"; // prepare the sql statement $sth = $db->prepare($sql); // execute the sql statement $sth->execute(array( ':topic_id' => $data->post[3]->topic_id, ':forum_id' => $data->post[2]->forum_id, ':poster' => $user['u_username'], ':post_name' => $data->post[0]->subject, ':post_content' => $data->post[1]->content, ':time_posted' => time() ) )or die($db->printError($sql)); when the form is submitted its contents are processed using: $j('form[name="new_post_form"]').submit(function(e){ e.preventDefault(); var data = { 'post': [ {subject : asf.getInput('subject')}, {content : asf.getInput('post_content')}, {forum_id : '{FORUM_ID}'}, {topic_id : '{TOPIC_ID}'} ] } asf.handleForm(data, './new_post.php?method=add_{METHOD}'); }); this.handleForm = function(data, file, redirect) { data.status = [{ error : '', success : '' }]; dataString = JSON.stringify(data); $j.post(file, { data : dataString }, function(result) { var obj = JSON.parse(result); if(obj.status[0].error) { handleProcess('error',obj.status[0].error); } else { handleProcess('success',obj.status[0].success); if(redirect) { document.location = redirect; } } }); } and then sent to the above php script Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted July 30, 2011 Share Posted July 30, 2011 Edit: I started writing this about 5 replies ago. new lines New lines gotten from where? A keyboard typing into a form? From a copy/paste from a Word document? Also, how do you know the data is - u00a? How are you looking at or displaying that value? Without all your code (the form and the from processing code from the start of the file through to that query) that duplicates the symptom, no one here can directly help you with what could be causing the problem. I could easily name 12 different things that could be altering the data or causing it to be displayed like that, from javascrpt in your form to the database table definition to a browser debugging plug-in, but no one here is going to spend time guessing what your relevant code is, to either eliminate or confirm your code as the source of the problem. Quote Link to comment Share on other sites More sharing options...
doddsey_65 Posted July 30, 2011 Author Share Posted July 30, 2011 ive had a read through google and it seems like the u000a is coming from JSON.stringify function which replaces \n with u000a. As a simple fix i have replaced all u000a's with <br /> when it is outputted to the browser. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted July 30, 2011 Share Posted July 30, 2011 Why are you using stripslashes on the data before you use json_decode? You would only want to do that if the data got escaped prior to that point in the code. Quote Link to comment Share on other sites More sharing options...
doddsey_65 Posted July 30, 2011 Author Share Posted July 30, 2011 the data is escaped before that point, im not too sure how though lol Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.