Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

remove an apostrophe from users input

Status
Not open for further replies.

dugen

Programmer
Joined
Jun 16, 2003
Messages
59
Location
US
Hi,

I have created a form on a website that has a php handle. The handle inputs the data from the form fields into a mysql database.

Whenever a user enters an apostrophe into the form it causes the query to fail.

How can i make it so the code automatically deletes the apostrophe?

Thanks
 
For this simple task I'd use str_replace :
Code:
<?php

$user_input = "This is char ' is about to be gone!";

echo "Now you see it : $user_input<br />\n";

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

echo "Now you don't : $user_input<br />\n";

?>
 
Looks like you pressed submit before me, thedaver ;-)
 
HI,

Below is the code i tried... This does not seem to be working. The query fails when an ' is in the name text box (txtname)


$name = 'txtname';
$customer ='txtcustomer';
$comments = 'txtcomments';

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


//link to the db

$link = mysql_connect($host, $user, $password) or die (mysql_error());
Mysql_select_db("motion") or die(mysql_error());


//Insert text box values from form into the database
$query = mysql_query("INSERT INTO feedback_accounting (`name`, `customer_name`, `comments`) VALUES ('$_POST[$name]', '$_POST[$customer]', '$_POST[$comments]')");

//execute query
mysql_query(($query) or die('Error, query failed'));
 
Unless you have register_globals turned off, $name and $_POST['name'] are different variables.

I second sleipnir's suggestion. It's easier, and there are other evils besides apostrophe's.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top