Jump to content

[SOLVED] Inserting apostrophes into MySQL field...


Chappers

Recommended Posts

Hi,

 

Trying to insert a height input into a MySQL table's field:

 

<select class='normal' name='height' size='1'>
<option value='5\''>5'</option>
<option value='5\'1\"'>5'1"</option>
</select>

 

the height variable is then inserted into the database. Looking at the table afterwards with phpMyAdmin shows that it contains only: 4\

 

I'm escaping the apostrophes and speech marks for obvious reasons, but I imagine there's a proper way of doing this. Of course, selecting the height field from the database and echoing it to the page also just shows: 4\

 

Thanks for any help

Link to comment
Share on other sites

Thanks a lot for that information. Could I just quickly pick your brains again please?

 

Can't get this if statement to work:

 

if ($current != 'employed' || 'unemployed') {
echo "Neither";
}

 

Whereas if I were to use one like this, with an equal instead of not equal, it works fine:

 

if ($current == 'employed' || 'unemployed') {
echo "Both";
}

 

What am I doing wrong?

Link to comment
Share on other sites

Hi, I tried that also but it doesn't work for me.

 

Just discovered that the answers to my original question about the apostrophes don't work. Since the form field uses apostrophes or speech marks for the data fields, any further use of them within that field is taken as an end to the entry, ie:

 

<option value='5'1"'>5'1"</option>

 

So my 5'1" is seen just as 5

Link to comment
Share on other sites

Worked it out at last by myself!

 

Here's how I've done it:

 

<select class='normal' name='height' size='1'>
<option value='4f'>4'</option>
<option value='4f1i'>4'1"</option>
<option value='4f2i'>4'2"</option>
</select>

$arr = array("f", "i");
$arr2 = array("\'", "\"");
$height = $_POST['height'];
$height = str_replace($arr,$arr2,$height);
$height2 = str_replace("\'","'",$height);

 

The $height variable is inserted as it is into the MySQL database. $height2 is for echoing onto the page to show it's worked alright and to show what's just been sent to the database. Because $height displays as, for example: 5\'3" (apostrophe still showing the escape symbol because the echo only cares about quotes being escaped, whereas MySQL needs apostrophes and quotes escaped.), I then have $height2 having \' replaced with just an apostrophe.

 

Result.

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.