-
Sending form to DB
Hi all,
I have now created the following code, which is designed to send web form data to not only a specifed email address but also to a MySQL DB. The problem is I'm getting the an error message:
Code:
Invalid query: Table 'dbname.mail' doesn't exist
The web form is successfully passed to email; however as per the above it will not pass the details to DB. I have included my code below and would be grateful if someone could assist with where the error may lie:
PHP Code:
<?php
function is_valid_email($email) {
$result = TRUE;
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) {
$result = FALSE;
}
return $result;
}
function add_to_db($data){
$link = mysql_connect("localhost", "username", "password") or die("Could not connect: " . mysql_error());
mysql_select_db('dbname', $link) or die ('Can\'t use this DB : ' . mysql_error());
$values = '';
$flds = '';
foreach ($data as $f => $val){
$values .= (empty($values) ? "'".$val."'" : ",'".$val."'");
$flds .= (empty($flds) ? $f : ','.$f);
}
$sql = "INSERT INTO mail ({$flds}) VALUES({$values})";
$result = mysql_query($sql) or die("Invalid query: " . mysql_error());
mysql_close($link);
}
empty($_POST['email']) ? $email='' : $email = $_POST['email'];
empty($_POST['name']) ? $name='' : $name = $_POST['name'];
empty($_POST['surname']) ? $surname='' : $surname = $_POST['surname'];
empty($_POST['add_info']) ? $add_info='' : $add_info = $_POST['add_info'];
if(!empty($_POST['submitted']) && $_POST['submitted'] == '1'){
$error = '';
if(empty($email))$error .= "Email address<br>";
if(empty($name))$error .= "First Name<br>";
if(empty($surname))$error .= "Last Name<br>";
if(empty($add_info))$error .= "Question/Feedback<br>";
if(!is_valid_email($email))$error .= "Invalid E-mail<br>";
if(empty($error)){
$verif_box = $_REQUEST["verif_box"];
if(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){
require_once 'includes/class.ext.mail.php';
$options = array(
'To' => '[email protected]', /* Sets the To email address for the message */
'ToName' => '', /* Sets the From name of the message */
'From' => '', /* Sets the From email address for the message */
'FromName' => '', /* Sets the From name of the message */
'Subject' => 'Contact form', /* Sets the Subject of the message */
'Mailer' => 'mail', /* Method to send mail: ("mail", "sendmail", or "smtp") */
'Host'=> '', /* Sets the SMTP hosts. All hosts must be separated by a semicolon.
You can also specify a different port for each host by using this
format: [hostname:port] (e.g. "smtp1.example.com:25;smtp2.example.com").
Hosts will be tried in order. */
'Port' => 25, /* Sets the default SMTP server port */
'SMTPAuth' => false, /* Sets SMTP authentication. Utilizes the Username and Password variables. */
'Username' =>'', /* Sets SMTP username. */
'Password' => '', /* Sets SMTP password. */
);
$fields = array(
'Nome (your name)' => 'name',
'E.mail (E-mail address)' => 'email',
'Messaggio (Message)' => 'message'
);
$mail = new ExtMail($options);
if(empty($options['From'])) $mail->AddFromAddress($email,$name);
$mail->AddAddress($options['To'],$options['ToName']);
$Body = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD></HEAD>
<BODY>';
$Body .= 'Email address: '.$email.'<br />';
$Body .= 'First Name: '.$name.'<br />';
$Body .= 'Last Name: '.$surname.'<br />';
$Body .= 'Message: '.$add_info.'<br />';
$AltBody = 'Email address: '.$email."/n";
$AltBody .= 'First Name: '.$name."/n";
$AltBody .= 'Last Name: '.$surname."/n";
$AltBody .= 'Message: '.$add_info."/n";
$Body .= '</BODY></HTML>';
$mail->Body = $Body;
$mail->AltBody = $AltBody;
if($mail->Send()){
add_to_db(array(
'name'=>$_POST['name'],
'surname'=>$_POST['surname'],
'email'=>$_POST['email'],
'add_info'=>$_POST['add_info']
));
$success = "<b>Thank you {$_POST['name']}</b>,<br>
We have received your feedback, and if requested, we will get back to you promptly.";
unset($_POST);
empty($_POST['email']) ? $email='' : $email = $_POST['email'];
empty($_POST['name']) ? $name='' : $name = $_POST['name'];
empty($_POST['surname']) ? $surname='' : $surname = $_POST['surname'];
empty($_POST['add_info']) ? $add_info='' : $add_info = $_POST['add_info'];
}
setcookie('tntcon','');
} else {
$wrong_code = true;
}
}
}
?>
-
Re: Sending form to DB
the error means that your table doesn't exist.
make sure the table "mail" exists in your database "dbname." there's nothing else to it.