Jump to content

PHP MYSQL long text issues


carlosx2

Recommended Posts

I have a problem it seems with sending over long pieces of text to mysql.

 

i have a form with a story that is being send to a registration file in php. All is fine even when i i put the var by print_r i see the long piece of text wich is about 564 thats not that many. I have a table in mysql setup with a text row to hold this story but for some reason its only stores my text when i submit around 189

 

I don't get what i am doing wrong. Its is just not accepting more. did i forget a certain setting or something? 

Link to comment
Share on other sites

this is the code i am using to post to the sql database

 

 

<title>Done</title><?PHP

    $user_name = "";
    $password = "";
    $database = "";
    $server = "";


$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);

$testname = $_POST[title];

$query = "SELECT * FROM dvd WHERE title='$_POST[title]'";
$result = mysql_query($query);
$num = mysql_num_rows($result);

if ($num == 0) {

    $SQL = "INSERT INTO dvd (title, date, time, models , niche , models2 , models3 , models4 , models5 , models6 , models7 , models8 , story ) VALUES ('$title' , '$date' , '$time' , '$models' , '$niche' , '$models2' , '$models3' , '$models4' , '$models5' , '$models6' , '$models7' , '$models8' , '$story')";

$result = mysql_query($SQL);

mysql_close($db_handle);

print "DVD has been added to the database. <p> <a href='dvdcreator.php'>Add another DVD</a>";
print_r ($SQL);
}
else {
print "DVD has already been entert into the database. hit the back button</a>";
mysql_close($db_handle);
}

?>

 

it still not working. checked with my wp blog that also uses the same mysql and there its working. so i think it might be the code or something. Then again when i so not that many char its all working perfect.

Link to comment
Share on other sites

data type i am using is text, in mysql that is.

 

change it to a text column in teh db, that should do the trick, it should be ok for a small database

 

Do you even read threads before you post?

 

not all of it no,

 

original poster ,

you may have a slashing problem wich is killing it, you need to add slashes oi recon (maybe), isue is with single quotes, hyphens

Link to comment
Share on other sites

data type i am using is text, in mysql that is.

 

change it to a text column in teh db, that should do the trick, it should be ok for a small database

 

Do you even read threads before you post?

 

not all of it no,

 

original poster ,

you may have a slashing problem wich is killing it, you need to add slashes oi recon (maybe), isue is with single quotes, hyphens

 

i am not sure what you mean by you may have a slashing problem

Link to comment
Share on other sites

If you have certain characters in the text you are inserting (such as single quote) and the data isn't being escaped that would explain the issue. Your code doesn't really show where $story (I assume that's the field in question) is coming from. That sort of leads me to believe you might be using register_globals, which is bad, but thats another story. Before the insert statement try adding...

 

$story = mysql_real_escape_string($story);

Link to comment
Share on other sites

If you have certain characters in the text you are inserting (such as single quote) and the data isn't being escaped that would explain the issue. Your code doesn't really show where $story (I assume that's the field in question) is coming from. That sort of leads me to believe you might be using register_globals, which is bad, but thats another story. Before the insert statement try adding...

 

$story = mysql_real_escape_string($story);

 

or addslashes(), i think single quotes conflict with the query itself, other than that mysql will add slashes to everything else and remove them when brogth out, you need to addslashes($string) and then remove them when you bring it out removeslashes or preg_replace()

 

"insert into table values('blah "blah" bla's');"

 

it can add and remove slashes from "blah" but blah' will end the value

Link to comment
Share on other sites

well this is all coming from a form to submit all this data. i fill in the form hit submit and you are done.  its is about $story  yes. so i use the post action that bring it to this site to process it the database.

 

where do i enter that one line. ? before i run the sent to code?

 

Link to comment
Share on other sites

if its object oriented you need to find where its inserting the code eg something like tablename->insert();

 

basicaly to whatever your form posts to. action=filepostedto.php or maybe php self

 

if its directly in teh query

 

$sql = "insert into table values(".$story.")"

 

 

just go to the page it posts to and do a search for insert or new or create or add

Link to comment
Share on other sites

Like I said, before the Insert statement. Yes I figured it's all coming from a posted form, but at no point in the code you have shown so far do you show a value being assigned to $story. Which means either a). you just didn't post it, or b). your server has register_globals enabled, which is not really recommended.

Link to comment
Share on other sites

ok it worked by just adding $story = mysql_real_escape_string($story);

 

but i have to find out how and why it worked this way. so next time i know how to use it and why. so i need to teach myself this little piece of code.

 

thanks for the help. this place is really great.

Link to comment
Share on other sites

ok it worked by just adding $story = mysql_real_escape_string($story);

 

but i have to find out how and why it worked this way. so next time i know how to use it and why. so i need to teach myself this little piece of code.

 

thanks for the help. this place is really great.

 

short texts almost never have single quotes,

 

long texts usualy paragraphs with hyphens cancel out the query beaus the query itself has a single quote to delimit values,

 

a single quote in your string will read like

 

values('blah's')

 

it will end at 'blah'

 

mysql handles slashing for everything else,

 

you will need to remove the slashes for this one on teh way out otherwise every time you edit it it will get an extra slash and you have loads of slahses

Link to comment
Share on other sites

Your code is like this...

 

$SQL = "INSERT INTO dvd (title, date, time, models , niche , models2 , models3 , models4 , models5 , models6 , models7 , models8 , story ) VALUES ('$title' , '$date' , '$time' , '$models' , '$niche' , '$models2' , '$models3' , '$models4' , '$models5' , '$models6' , '$models7' , '$models8' , '$story')";

 

Now imagine that $story holds something like...

 

$story = "I'm great";

 

If you subsitute that into your query, the end of your query now reads...

 

'$models8' , 'I'm great')"

 

As you can see the single quote will escape the string thus only I will be inserted. You really need to run all your strings through mysql_real_escape_string() for security purposes anyway, otherwise somebody could use SQL Injection on your site.

 

Faux Edit: You do not have to call stripslashes to get rid of the slashes, because they aren't really in your database.

 

Link to comment
Share on other sites

Faux Edit: You do not have to call stripslashes to get rid of the slashes, because they aren't really in your database.

 

 

thats what you would think, but from experience i know they will go in, because mysql isnt adding these slashes so it wont remove them, they become part of your string, the first slash wont show, but when you edit it, the second slash will, it will keep on slashing it every time you edit, first slash will be removed but in teh db, to wich another slash will be added on edit, you need to add slashes on edit too.

 

swhen you insert into the text box in your interface strip the slashes, the first slash is invisible, only when there are two it will become visible

Link to comment
Share on other sites

ok i get it. the only weird thing is that in the text i tried to enter there weren't any of these ' or anything else. just  ....  thats all. but i understand i should do it for each string

 

annoying isnt it, if you were mvc'd you could oiveride the insert method of teh base class of model

Link to comment
Share on other sites

Faux Edit: You do not have to call stripslashes to get rid of the slashes, because they aren't really in your database.

 

 

thats what you would think, but from experience i know they will go in, because mysql isnt adding these slashes so it wont remove them, they become part of your string, the first slash wont show, but when you edit it, the second slash will, it will keep on slashing it every time you edit, first slash will be removed but in teh db, to wich another slash will be added on edit, you need to add slashes on edit too.

 

swhen you insert into the text box in your interface strip the slashes, the first slash is invisible, only when there are two it will become visible

 

Yes, that is what I would think, because it is correct. The only time the slashes should appear in the inserted data is if you have badly written code/server setting combination. If you use mysql_real_escape_string, the only time you should get the slashes in your data, and hence have to remove them is if the data is being double escaped. This could be caused by magic quotes being enabled on the server, but since the OP was getting a problem with unescaped data, this doesn't appear to be the case for them.

Link to comment
Share on other sites

Faux Edit: You do not have to call stripslashes to get rid of the slashes, because they aren't really in your database.

 

 

thats what you would think, but from experience i know they will go in, because mysql isnt adding these slashes so it wont remove them, they become part of your string, the first slash wont show, but when you edit it, the second slash will, it will keep on slashing it every time you edit, first slash will be removed but in teh db, to wich another slash will be added on edit, you need to add slashes on edit too.

 

swhen you insert into the text box in your interface strip the slashes, the first slash is invisible, only when there are two it will become visible

 

Yes, that is what I would think, because it is correct. The only time the slashes should appear in the inserted data is if you have badly written code/server setting combination. If you use mysql_real_escape_string, the only time you should get the slashes in your data, and hence have to remove them is if the data is being double escaped. This could be caused by magic quotes being enabled on the server, but since the OP was getting a problem with unescaped data, this doesn't appear to be the case for them.

 

ok il keep that in mind thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.