I need some help with my MySQL Queries in PHP
Before I say anymore I use mysql_* function, I know they are depreciated, however only from PHP v5.5 and I have PHP 5.3 installed on my server and other mysql_* functions are working its just this one.
I'm trying to insert values from a form into a table on form submit, the code is in the correct place as the PHP sends the email and echo's the 'mail sent'.
This is my query:
mysql_query("INSERT INTO customers (
name,email,telephone
) VALUES (
".$_POST['name'].",
".$_POST['email'].",
".$_POST['telephone'].",
)");
This is the document it's in:
<?php
// Database connect
$db_host = 'localhost';
$db_user = 'redjaxco';
$db_pass = 'CORRECT PASSWORD';
$db_database = 'redjaxco_website';
$link = mysql_connect($db_host,$db_user,$db_pass) or die('Unable to establish a DB connection');
mysql_select_db($db_database,$link);
mysql_query("SET names UTF8");
$owner_email = "[email protected]";
$headers = 'From:' . $_POST["email"];
$subject = 'Online Form - '. $_POST["topic"]. " : " . $_POST["name"];
$messageBody = "";
if($_POST['topic']!='nope'){
$messageBody .= '<p>Subject: ' . $_POST["topic"] . '</p>' . "\n";
$messageBody .= '<br>' . "\n";
}
if($_POST['name']!='nope'){
$messageBody .= '<p>Visitor: ' . $_POST["name"] . '</p>' . "\n";
$messageBody .= '<br>' . "\n";
}
if($_POST['email']!='nope'){
$messageBody .= '<p>Email Address: ' . $_POST['email'] . '</p>' . "\n";
$messageBody .= '<br>' . "\n";
}else{
$headers = '';
}
if($_POST['phone']!='nope'){
$messageBody .= '<p>Phone Number: ' . $_POST['phone'] . '</p>' . "\n";
$messageBody .= '<br>' . "\n";
}
if($_POST['message']!='nope'){
$messageBody .= '<p>Message: ' . $_POST['message'] . '</p>' . "\n";
}
if($_POST["stripHTML"] == 'true'){
$messageBody = strip_tags($messageBody);
}
try{
if(!mail($owner_email, $subject, $messageBody, $headers))
{
throw new Exception('mail failed');
}
else
{
mysql_query("INSERT INTO customers (
name,email,telephone
) VALUES (
".$_POST['name'].",
".$_POST['email'].",
".$_POST['telephone'].",
)");
echo 'mail sent';
}
}catch(Exception $e){
echo $e->getMessage() ."\n";
}
?>
ext/mysql(it might not have been deprecated in v5.3, but its use was certainly discouraged) doesn't mean you should avoid escaping your variables. Your code is wide open to SQL injection and XSS attacks.