My php form field will not accept an apostrophe?

My registration form is working very well, however, my form won't accept an apostrophe in the first or last name although I think I have inserted this in the right spot in my php code. The form will submit after I take out the apostrophe.

	// Check for a first name:
	if (preg_match ('/^[A-Z \'.-]{2,20}$/i', $_POST['first_name'])) {
		$fn = mysqli_real_escape_string ($connect, $_POST['first_name']);
	} else {
		$reg_errors['first_name'] = 'Please enter your first name!';
	}

	// Check for a last name:
	if (preg_match ('/^[A-Z \'.-]{2,40}$/i', $_POST['last_name'])) {
		$ln = mysqli_real_escape_string ($connect, $_POST['last_name']);
	} else {
		$reg_errors['last_name'] = 'Please enter your last name!';
	}

Open in new window

Wanda MarstonCreative DirectorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
http://www.laprbass.com/RAY_temp_wchrn.php Outputs:
OK

So the problem does not appear to be the regular expression.  Can you show us an isolated test case, please? Then we can suggest some debugging techniques.
<?php // RAY_temp_wchrn.php
error_reporting(E_ALL);

// TEST DATA
$x = "P. J. O'Rourke";

// COPIED FROM THE POST AT EE
	// Check for a first name:
	if (preg_match ('/^[A-Z \'.-]{2,20}$/i', $x)) { echo "OK"; }

Open in new window

0
Wanda MarstonCreative DirectorAuthor Commented:
I have attempted to add a screen shot of the form as I have filled it out. Otherwise I do not know what else I can show.

My form tells me to enter my last name if I put any name in with an apostrophe. It will accept a period or a hypehen. The database seems to match up and the forms functions include seems to be fine.

Thanks,

WC
Screen-shot-LastName.png
0
Wanda MarstonCreative DirectorAuthor Commented:
Sorry, didn't see what you were doing.

I entered your code at the top of my page and got and "OK".

WC
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Wanda MarstonCreative DirectorAuthor Commented:
However, I still get a "Please enter your last name." in my form.

WC
0
Wanda MarstonCreative DirectorAuthor Commented:
I left everything the same and then when I took out the apostophe in O'Rourke the form took the entries and entered it into the database.

WC
0
Dave BaldwinFixer of ProblemsCommented:
I took your code and made a page out of it and it seems to work fine.  Database table is very simplified for this purpose.
<?php 
$dbhost = "10.202.46.41";    // Your database server 192.168.0.10
$dbuser = "eecontact";      // Your db username
$dbpass = "eazy4U";      // Your db password
$dbname = "nads";      // Your database name
$dbtable = "firstlast";  // the table we're using

$connect = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
//$dbc = new mysqli('10.202.46.41', 'eecontact', 'eazy4U', 'nads');

/*
 * Use this instead of $connect_error if you need to ensure
 * compatibility with PHP versions prior to 5.2.9 and 5.3.0.
 */
if (mysqli_connect_error()) {
    die('Connect Error (' . mysqli_connect_errno() . ') '
            . mysqli_connect_error());
}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>PHPfrmchk</title>
</head>
<body>
<h1>PHPfrmchk</h1>
<?php 
if(isset($_POST['submit'])) {
	// Check for a first name:
	if (preg_match ('/^[A-Z \'.-]{2,20}$/i', $_POST['first_name'])) {
		$fn = mysqli_real_escape_string ($connect, $_POST['first_name']);
		echo "First Name = ".$fn." ";
	} else {
		$reg_errors['first_name'] = 'Please enter your first name!';
	}

	// Check for a last name:
	if (preg_match ('/^[A-Z \'.-]{2,40}$/i', $_POST['last_name'])) {
		$ln = mysqli_real_escape_string ($connect, $_POST['last_name']);
		echo "&nbsp;|| Last Name = ".$ln;
	} else {
		$reg_errors['last_name'] = 'Please enter your last name!';
	}

if(($fn != "") && ($ln != "")) {
$query = "INSERT INTO firstlast (first_name, last_name) VALUES ('$fn', '$ln')";
//echo $query."<br>";

$result = mysqli_query($connect, $query)
	  	or die('Error inserting in '.$dbname." ".mysqli_error($connect));
	}
}
?>
<form action="#" method="post">
First Name: <input type="text" name="first_name" value="" /><br>
Last Name: <input type="text" name="last_name" value="" /><br>
<input type="submit" name="submit" value="Submit" />
</form>
<hr>
<?php 
$sql = "SELECT * FROM `firstlast` LIMIT 10";
//echo $sql;
$result = mysqli_query($connect, $sql)
	  	or die('Error selecting from '.$dbname." ".mysqli_error($connect));

while($row = mysqli_fetch_object($result)) {
	echo "<b>First Name</b> = ".$row->first_name." || <b>Last Name</b> = ".$row->last_name."<br>";
	}
 ?>
</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
when I took out the apostophe(sic) in O'Rourke the form took the entries and entered it into the database.

So the real issue appears to be that you need to escape the strings before using them in a query?  Or that the query fails silently? If you want to post the rest of the action script, we may be able to spot something.
0
Wanda MarstonCreative DirectorAuthor Commented:
Sorry I didn't get back to you. A related issue came up with regard to foreign accents such as french accents in names.

Thank you for your help.

Wchirnside
0
Ray PaseurCommented:
Thanks for the points.  Do you have a related question that needs an answer?  The issue about European characters has a pretty well understood design pattern.
0
Wanda MarstonCreative DirectorAuthor Commented:
Yes, I do. SInce I am in Canada and my site - if I ever get the English done - will be done in French and then possibly other languages, the question of other accents etc. came up and do I deal with these in the same way. I suppose I would have to pose this question separately.

Wchirnside
0
Ray PaseurCommented:
It would be good to post a new question about that.  It's really a different issue.  This one is the escape that allows you to use special characters in queries.  The other issue is character encoding.  Accented characters are not special characters and do not need any kind of escape.
0
Wanda MarstonCreative DirectorAuthor Commented:
Okay, Thanks. I will get the other issue tidied up and then if I need to post regarding character encoding, I will do so.

Appreciate all the help that Experts Exchange can provide.

WC
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.