Jump to content

INSERT string with spaces


xProteuSx

Recommended Posts

Say I have the following ...

 

$string = 'This is a silly problem!';

 

$insert = "INSERT INTO table (column1) VALUES ($string)";

$insertresult = mysql_query($insert) or die ('Error in Insert query: ' . mysql_error());

 

... I get the following problem:

 

Error in Insert query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'is a silly problem!' at line 1

 

It throws an error after the first space that it encounters.  How do I get around this?

Link to comment
https://forums.phpfreaks.com/topic/248957-insert-string-with-spaces/
Share on other sites

Okay, that solves it.  Thanks.  However, I'm up against a new problem (to which the solution is probably just as simple as the original problem).

 

I've got a paragraph stored in a session variable:

 

$_SESSION['paragraph'] = "We are constantly trying to improve PHP Freaks and these forums, so feel free to go to the PHPFreaks Comments/Suggestions board and point out anything you'd like to see different!";

 

$string = mysql_real_escape_string($_SESSION['paragraph']);

 

$insert = "INSERT INTO table (column1) VALUES ($string)";

$insertresult = mysql_query($insert) or die ('Error in Insert query: ' . mysql_error());

 

Now, the insert works.  However, the 'column' value for this new row is:

 

mysql_real_escape_string(We are constantly trying to improve PHP Freaks and these forums, so feel free to go to the PHPFreaks Comments/Suggestions board and point out anything you'd like to see different!)

 

Whereas it should be:

 

We are constantly trying to improve PHP Freaks and these forums, so feel free to go to the PHPFreaks Comments/Suggestions board and point out anything you\'d like to see different!

 

What am I doing wrong now???

 

 

That doesn't solve the problem of the following being stored in the table:

 

mysql_real_escape_string(We are constantly trying to improve PHP Freaks and these forums, so feel free to go to the PHPFreaks Comments/Suggestions board and point out anything you'd like to see different!)

 

What's going on here?  How do I get around this?  Thanks ...

Archived

This topic is now archived and is closed to further replies.

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