Buffmin
01-17-2012, 05:11 PM
I have mysql code to enter a businessname into a table in a database, but, if I enter a name such as "Joe's" (with a hyphen), it goes into the database as "Joe\'s" (always adds a backslash before the hyphen). I would appreciate anyone's help. Thank you, Buffmin.
My code
<?php
/*
NEW.PHP
Allows user to create a new entry in the database
*/
function renderForm($BusinessName, $error)
{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>New Member</title>
<link rel="stylesheet" type="text/css" href="member.css">
</head>
<body>
<?php
// if there are any errors, display them
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<h1 class="style1">Add a Member to Business Directory</h1>
<form action="" method="post">
<table>
<tr><td class="blue">Business Name</td><td><input type="text" size="40" name="BusinessName"value="<?php echo $BusinessName; ?>" /></td></tr>
</table><p></p>
<p class="blue">* Required Field</p>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
<?php
}
// connect to the database
require ('dbstuff.php');
$db = connectDB();
// check if the form has been submitted. If it has, start to process the form and save it to the database
if (isset($_POST['submit']))
{
// get form data, making sure it is valid
$Businessname = mysql_real_escape_string(htmlspecialchars($_POST['BusinessName']));
// check to make sure both fields are entered
if ($Businessname == '')
{
// generate error message
$error = 'ERROR: Please fill in all required fields.....!';
// if either field is blank, display the form again
renderForm($BusinessName, $error);
}
else
{
// save the data to the database
mysql_query("INSERT mybiz SET BusinessName='$Businessname'")
or die(mysql_error());
mysql_query("alter table mybiz order by BusinessName")
or die(mysql_error());
// once saved, redirect back to the view page
header("Location: view.php");
}
}
else
// if the form hasn't been submitted, display the form
{
renderForm('','','','','','','','','','','','','','');
}
?>
My code
<?php
/*
NEW.PHP
Allows user to create a new entry in the database
*/
function renderForm($BusinessName, $error)
{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>New Member</title>
<link rel="stylesheet" type="text/css" href="member.css">
</head>
<body>
<?php
// if there are any errors, display them
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<h1 class="style1">Add a Member to Business Directory</h1>
<form action="" method="post">
<table>
<tr><td class="blue">Business Name</td><td><input type="text" size="40" name="BusinessName"value="<?php echo $BusinessName; ?>" /></td></tr>
</table><p></p>
<p class="blue">* Required Field</p>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
<?php
}
// connect to the database
require ('dbstuff.php');
$db = connectDB();
// check if the form has been submitted. If it has, start to process the form and save it to the database
if (isset($_POST['submit']))
{
// get form data, making sure it is valid
$Businessname = mysql_real_escape_string(htmlspecialchars($_POST['BusinessName']));
// check to make sure both fields are entered
if ($Businessname == '')
{
// generate error message
$error = 'ERROR: Please fill in all required fields.....!';
// if either field is blank, display the form again
renderForm($BusinessName, $error);
}
else
{
// save the data to the database
mysql_query("INSERT mybiz SET BusinessName='$Businessname'")
or die(mysql_error());
mysql_query("alter table mybiz order by BusinessName")
or die(mysql_error());
// once saved, redirect back to the view page
header("Location: view.php");
}
}
else
// if the form hasn't been submitted, display the form
{
renderForm('','','','','','','','','','','','','','');
}
?>