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
6 Comments on “Apostrophe, Input Form, PHP, MySQL: The Easy Solution!”
Pingback: Show Code In WordPress: The Syntax Highlighter Plugin | Richard Cummings
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?
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);
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
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
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