Jump to content

[SOLVED] Apostrophe trouble....


galvin

Recommended Posts

I'm sure this is easy but can someone help me here.

 

I have MySQL code to insert last names into a database.  I'll strip it down for examples sake...

 

$sql = "INSERT into names (last) values ('$player1last')";

 

My problem is that if the Last Name has an apostrophe (like O'Connell), then I get the following error...

 

Database query failed: 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 'Connell')' at line 1

 

So I realize I have to do something to escape (or maybe temporarily strip?) the apostrohe in last names that have them.  I just can't figure out how to do it.  I'm pretty sure my new code will be...

 

$player1last = {{{ some code to do something to $player1last to handle any apostrophes that might be in it }}}

$sql = "INSERT into names (last) values ('$player1last')";

 

But obviously I need help with the part in {{{...}}}.

 

Can anyone help me here?

Link to comment
Share on other sites

Thanks everyone.  I actually want to KEEP the apostrophe in the name, so that would just be...

 

$player1last = str_replace("'","\'",player1last);

 

That works (I just tried it  :)). But if you're now saying that the following is better to use...

 

$sql="insert into name(last) values('".mysql_real_escape_string($_POST['player1last'])."');

 

...how would I use that newest code AND KEEP the apostrophe?

Link to comment
Share on other sites

$player1last = mysql_real_escape_string($player1last);

$sql = "INSERT into names (last) values ('$player1last')";

 

You definitely want to be using MSRE here.  I know it was already suggested, but I just separated it from the SQL string so you could see it more clearly.

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.