tags:

views:

385

answers:

3

I'm using ajax to gather the ckeditor data to be submitted. The problem is only the content before the first apostrophe is being submitted to the database. What could I be doing wrong?

Edit:

$date = strtotime($formData['date']);
$article=mysql_real_escape_string($formData['article'],$DBconnect);

$DBconnect=mysql_connect($dbVals['host'],$dbVals['user'],$dbVals['pass']);

mysql_select_db($dbVals['db'], $DBconnect);
$SQLstring="INSERT INTO PressRelease (ip, tym, title, date, article) VALUES('${_SERVER['REMOTE_ADDR']}', ".time().",'${formData['title']}', '$date', '$article')";

I'm fairly new at this so if there is anything else you need to see in order to help let me know.

+2  A: 

It sounds like you aren't escaping the text data before you insert it into the database. Use this function on the data before you pass it into your SQL query:

http://www.php.net/manual/en/function.mysql-real-escape-string.php

Edit: sorry, that's assuming you are using MySQL.

Mark B
+1 I bet two beers this is it.
Pekka
yes i am using mysql, i had tried using that function previously, i thought that was the issue as well, but by using it none of the data is submitted. what i have in the form is 2 text boxes and then a ckeditor text area, all other data is submitted to the database fine.
sassy_geekette
Are you saying that with mysql_real_escape_string no data at all is entered in the database? Please be as specific as you can, that helps us helping you.
Niels Bom
Yes, more details please: can you post your PHP insert code?
Mark B
yes, using mysql_real_escape_string no data is inserted from the ckeditor text area, all other fields in the form are submitted though, code has been posted above.
sassy_geekette
In the line where you call `mysql_real_escape_string`, you are passing in the database connection (`$DBconnect`) before it has been declared or opened. Try moving that line below the line which opens the connection.
Mark B
See my updated answer for why exactly this results in an empty field in the db for 'article'.(Btw, do people get notifications if an answer to their question is updated?)
Niels Bom
I don't think they do, Niels — but they should!
Mark B
+1  A: 

A different, more complicated, and arguably superior method to the one suggested by Mark, is using Parameterized Statements.

To borrow an example from Wikipedia:

<?php
$db = new mysqli("localhost", "user", "pass", "database");
$stmt = $db -> prepare("SELECT priv FROM testUsers WHERE username=? AND password=?");
$stmt -> bind_param("ss", $user, $pass);
$stmt -> execute();
?>

It leaves the escaping up to the MySQL driver, severely reducing the chance of SQL Injection and things like accidental double-escaping.

Note that this is not possible using the old MySQL functions. You need the Improved MySQLI functions/object, or something like PDO.

Atli
Mark B's solution is a lot simpler, and I'm not even sure the problem is at the query level.
Niels Bom
I did say this was more complicated, did I not?. - If the problem is at the query level (which, given the problem description, is at least among the most likely reasons), this is definitely the most "secure" method. (In more than one sense.)
Atli
i am a beginner, so i am looking for something more at my level, thanks for the suggestion though
sassy_geekette
+1  A: 

If I understand correctly the following is the case:

  • You've got a textarea that's "taken over" by CKeditor
  • You're reading the content of that textarea with Javascript
  • You're sending the gathered content to the server with AJAX

If you alert() the content that Javascript gets from the textarea, you can see whether step 2 succeeds. If not, please post your Javascript.

If step 2 is correct, then maybe there's a problem server side, dump your db query to look at that.

Update: Make sure you when you're developing that you turn on all errors and notices. And if you're doing stuff which you can't "see" easily, like AJAX, make sure to keep an eye on your server's error log.

In your code example line 2 you use $DBconnect, and then in line 4 you define what that is. As you can see in the PHP.net entry for mysql_real_escape_string if the function cannot find a connection to the database the function generates an error and returns FALSE. The FALSE is put into your database and that's what goes into your database.

My advice to you is: try harder at debugging. Test all your assumptions, test the value of variables at every step, check if they have the value you expect them to have. Use var_dump(), print_r(), echo and die(). Or if you want something more advanced use a debugger (I don't).

Niels Bom
alert succeeds, all other data in the form submits fine, before adding in the ckeditor the form worked perfectly
sassy_geekette
I don't understand, the content in the other parts of the form element *do* end up in the database and the stuff in the ckeditor textarea doesn't?
Niels Bom
yes, that's right, i know it doesn't make much sense, but as I mentioned I am a new programmer and i could be missing something so simple...
sassy_geekette