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

 

 

Link to comment
Share on other sites

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

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.