Apostrophe, Input Form, PHP, MySQL: The Easy Solution!



To make money online, you must be on top. The SEO System gets you there! --Richard

In this article, I will tell you how to deal with the apostrophe not working in form data that writes to MySql using PHP.

I do a lot of work involving input forms on PHP pages that write the input data to MySQL. Every time I do this, I have to account for the user who puts in an apostrophe. In most cases, it is intended.

This morning I was writing a form that allows users to enter movies. What was one of the first movies that I tried to enter as a test? It’s Complicated. An appropriate title, no?

When you have not accounted for this apostrophe in your PHP file, you may receive a message that looks something like this:

Anyway, I run into this quite a bit and each time I find myself fishing through my old projects or the Internet for a solution. So, I write this solution as much for myself as for you :)

And, thankfully, despite all the ramblings you may find on the Internet, the solution is quite easy.

Let’s have a look. Assuming your variable is called $keyword, as mine is in the form I am working with right now, simply enter this after your $keyword variable:

$keyword = str_replace("'","''",$keyword);

Dealing with apostrophes when you are working with form data in a PHP file that writes to a database can be a headache. However, in the end, you just need that one line and you will be good to go.

Hope this helps. Cheers and Happy New Year, Richard

Want a fresh, unique perspective on web technologies once a week? Subscribe to Richard Cummings now.

Other SEO, Blogging, and Technical Artilces You May Enjoy

, , , , , ,

6 Responses to “Apostrophe, Input Form, PHP, MySQL: The Easy Solution!”

  1. Arindam Chakraborty Says:

    Thanks. I have a different problem. A php file shows me a 500 error everytime I run it in my browser. Reason? There is an apostrophe in the title tag! This is just a simple static php file, NO mysql is involved. How do i fix this?

  2. matt dial Says:

    Thanks…this was the first result I saw on the Google search and it worked. Just in case it helps someone, I had a place for entering author quotes. One had an apostrophe (only one apostrophe – there was another quote that had an apostrophe but also contained quotes within the quote field) and it wouldn’t successfully submit to the database.

    The first line was the existing code. The second line is what I added immediately following the first line that fixed the problem.

    $quote=$_POST['quote'];
    $quote = str_replace(“‘”,”””,$quote);

  3. Lisa Waters Says:

    I am having the same problem. I am a newbie using Dreamweaver and when the form submits the data into a MYSQL database, it errors out. Here is the code:

    <?php
    if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
    {
    if (PHP_VERSION

    Any help would be appreciated. Thanks

    Lisa

  4. Pierre Lewin Says:

    After an hour messing around without getting the string with an apostrophe into the form value… I found a very trivial way that works ! :)

    while ($newArray = mysqli_fetch_array($res1, MYSQLI_ASSOC)) {
    $_title = stripslashes($newArray['title']);
    $_title = ‘”‘.$_title.’”‘;

    echo “”;

    Pierre

  5. Pierre Lewin Says:

    After an hour messing around without getting the string with an apostrophe into the form value… I found a very trivial way that works !

    … while ($newArray = mysqli_fetch_array($res1, MYSQLI_ASSOC)) {
    $_title = stripslashes($newArray['title']);
    $_title = ‘”‘.$_title.’”‘;

    and then :

    (notice the absence of apostrophe around the value)

    Pierre


Trackbacks/Pingbacks

  1. [...] called SyntaxHighlighter. See it in action on the bottom of a page that I just created called Apostrophe, Input Form, PHP, MySQL: The Easy Solution! It makes adding code to WordPress a [...]

Leave a Reply