karma12
asked on
insert into db and send email
Hi,
I am new to php, please bear with me.... I have created a simple form that askes customers for their details and it emails those details to someone.. what i want to do is to have these details submitted to a DB at the same time - I have no idea how to incorporate this in my code... can someone please help?
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- --------
<?php
// Receiving variables
@$title = addslashes($_POST['title'] );
@$surname = addslashes($_POST['surname ']);
@$address = addslashes($_POST['address ']);
@$town = addslashes($_POST['town']) ;
@$county = addslashes($_POST['county' ]);
@$postcode = addslashes($_POST['postcod e']);
@$telephone = addslashes($_POST['telepho ne']);
@$besttime = addslashes($_POST['besttim e']);
@$email = addslashes($_POST['email'] );
// Validation
if (strlen($title) == 0 )
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid title</font></p>");
}
if (strlen($surname) == 0 )
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid surname</font></p>");
}
if (strlen($address) == 0 )
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid address</font></p>");
}
if (strlen($town) == 0 )
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid town</font></p>");
}
if (strlen($county) == 0 )
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid county</font></p>");
}
if (strlen($postcode) == 0 )
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid postcode</font></p>");
}
if (strlen($telephone) == 0 )
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid telephone</font></p>");
}
if (strlen($besttime) == 0 )
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid besttime</font></p>");
}
if (! ereg('[A-Za-z0-9_-]+\@[A-Z a-z0-9_-]+ \.[A-Za-z0 -9_-]+', $email))
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid email</font></p>");
}
if (strlen($email) == 0 )
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid email</font></p>");
}
//Sending Email to form owner
$pfw_header = "From: $email\n"
. "Reply-To: $email\n";
$pfw_subject = "Info";
$pfw_email_to = "someone@somewhere.co.uk";
$pfw_message = "title: $title\n"
. "surname: $surname\n"
. "address: $address\n"
. "town: $town\n"
. "county: $county\n"
. "postcode: $postcode\n"
. "telephone: $telephone\n"
. "besttime: $besttime\n"
. "email: $email\n";
@mail($pfw_email_to, $pfw_subject ,$pfw_message ,$pfw_header ) ;
echo("<p align='center'><font face='Arial' size='3' color='#FF0000'>Thankyou for your enquiry, we will contact you shortly regarding your query</font></p>");
?>
I am new to php, please bear with me.... I have created a simple form that askes customers for their details and it emails those details to someone.. what i want to do is to have these details submitted to a DB at the same time - I have no idea how to incorporate this in my code... can someone please help?
--------------------------
<?php
// Receiving variables
@$title = addslashes($_POST['title']
@$surname = addslashes($_POST['surname
@$address = addslashes($_POST['address
@$town = addslashes($_POST['town'])
@$county = addslashes($_POST['county'
@$postcode = addslashes($_POST['postcod
@$telephone = addslashes($_POST['telepho
@$besttime = addslashes($_POST['besttim
@$email = addslashes($_POST['email']
// Validation
if (strlen($title) == 0 )
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid title</font></p>");
}
if (strlen($surname) == 0 )
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid surname</font></p>");
}
if (strlen($address) == 0 )
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid address</font></p>");
}
if (strlen($town) == 0 )
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid town</font></p>");
}
if (strlen($county) == 0 )
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid county</font></p>");
}
if (strlen($postcode) == 0 )
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid postcode</font></p>");
}
if (strlen($telephone) == 0 )
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid telephone</font></p>");
}
if (strlen($besttime) == 0 )
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid besttime</font></p>");
}
if (! ereg('[A-Za-z0-9_-]+\@[A-Z
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid email</font></p>");
}
if (strlen($email) == 0 )
{
die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid email</font></p>");
}
//Sending Email to form owner
$pfw_header = "From: $email\n"
. "Reply-To: $email\n";
$pfw_subject = "Info";
$pfw_email_to = "someone@somewhere.co.uk";
$pfw_message = "title: $title\n"
. "surname: $surname\n"
. "address: $address\n"
. "town: $town\n"
. "county: $county\n"
. "postcode: $postcode\n"
. "telephone: $telephone\n"
. "besttime: $besttime\n"
. "email: $email\n";
@mail($pfw_email_to, $pfw_subject ,$pfw_message ,$pfw_header ) ;
echo("<p align='center'><font face='Arial' size='3' color='#FF0000'>Thankyou for your enquiry, we will contact you shortly regarding your query</font></p>");
?>
That is COMPLETELY untested, and you would need to make sure you are using MySQL and not some other DB. You will need to know the server name and your DB credentials ($s_server, $s_username, $s_password).
In terms of error handling, the mysql_xxx functions return False if they go wrong.
Commonly ...
$r_conn = mysql_pconnect($s_server, $s_username, $s_password) or die('Sorry. It has all gone horribly wrong : ' . mysql_errno() . ':' . mysql_error());
$s_SQL = <<< END_SQL
INSERT INTO
tablename(title, surname, address, town, country, postcode, telephone, besttime, email)
VALUE('$title', '$surname', '$address', '$town', '$country', '$postcode', '$telephone', '$besttime', '$email')
END_SQL;
$r_results = mysql_query($s_SQL) or die('Sorry. It has all gone horribly wrong : ' . mysql_errno() . ':' . mysql_error());
mysql_free_results($r_resu lts);
sort of thing.
In terms of error handling, the mysql_xxx functions return False if they go wrong.
Commonly ...
$r_conn = mysql_pconnect($s_server, $s_username, $s_password) or die('Sorry. It has all gone horribly wrong : ' . mysql_errno() . ':' . mysql_error());
$s_SQL = <<< END_SQL
INSERT INTO
tablename(title, surname, address, town, country, postcode, telephone, besttime, email)
VALUE('$title', '$surname', '$address', '$town', '$country', '$postcode', '$telephone', '$besttime', '$email')
END_SQL;
$r_results = mysql_query($s_SQL) or die('Sorry. It has all gone horribly wrong : ' . mysql_errno() . ':' . mysql_error());
mysql_free_results($r_resu
sort of thing.
ASKER
where would i stick this in my code? or does that not matter?
ASKER
i know my DB credentials...... do you not mean i need my hostname not server???
Your database server name or IP.
It may be localhost if you have a your DB server in your Web server.
It may not.
That code should be put in just after the mail() call.
It may be localhost if you have a your DB server in your Web server.
It may not.
That code should be put in just after the mail() call.
ASKER
I get the followin error: although the email is sent...
Warning: mysql_pconnect(): Unknown MySQL server host '0net' (1) in insert_db.php on line 33
which is
$r_conn = mysql_pconnect($s_bbbbb.dn s-systems. net, $s_username, $s_password) or die('Sorry. It has all gone horribly wrong : ' . mysql_errno() . ':' . mysql_error());
im wondering if the hiphan in the Hostname is failin it? or is their no chance that can happen....
Warning: mysql_pconnect(): Unknown MySQL server host '0net' (1) in insert_db.php on line 33
which is
$r_conn = mysql_pconnect($s_bbbbb.dn
im wondering if the hiphan in the Hostname is failin it? or is their no chance that can happen....
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi,
I managed to get it working using the below:::
<?php
//Sending Email to form owner
$pfw_header = "From: $email\n"
. "Reply-To: $email\n";
$pfw_subject = " Info";
$pfw_email_to = "someone@somewhere.co.uk";
$pfw_message = "firstname: $firstname\n"
. "firstname: $firstname\n"
. "surname: $surname\n"
. "initial: $initial\n"
. "address1: $address1\n"
. "address2: $address2\n"
. "city: $city\n"
. "postcode: $postcode\n"
. "contactno: $contactno\n"
. "email: $email\n"
. "appliances: $appliances\n";
@mail($pfw_email_to, $pfw_subject ,$pfw_message ,$pfw_header ) ;
$con = mysql_connect("bbbbb.dns-s ystems.net ","bbbbb_t est","bbbb b");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}mysql_select_db("bbbbb_te st", $con);$sql="INSERT INTO person
(firstname,surname,initial ,address1, address2,c ity,postco de,contact no,email,a ppliances)
VALUES
('$_POST[firstname]','$_PO ST[surname ]','$_POST [initial]' ,'$_POST[a ddress1]', '$_POST[ad dress2]',' $_POST[cit y]','$_POS T[postcode ]','$_POST [contactno ]','$_POST [email]',' $_POST[app liances]') ";if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}echo "Success!";
?>
I managed to get it working using the below:::
<?php
//Sending Email to form owner
$pfw_header = "From: $email\n"
. "Reply-To: $email\n";
$pfw_subject = " Info";
$pfw_email_to = "someone@somewhere.co.uk";
$pfw_message = "firstname: $firstname\n"
. "firstname: $firstname\n"
. "surname: $surname\n"
. "initial: $initial\n"
. "address1: $address1\n"
. "address2: $address2\n"
. "city: $city\n"
. "postcode: $postcode\n"
. "contactno: $contactno\n"
. "email: $email\n"
. "appliances: $appliances\n";
@mail($pfw_email_to, $pfw_subject ,$pfw_message ,$pfw_header ) ;
$con = mysql_connect("bbbbb.dns-s
if (!$con)
{
die('Could not connect: ' . mysql_error());
}mysql_select_db("bbbbb_te
(firstname,surname,initial
VALUES
('$_POST[firstname]','$_PO
{
die('Error: ' . mysql_error());
}echo "Success!";
?>
ASKER
ill award you the points becasue you pointed me in the right direction, which i greatly appreciate........
Ta!
So, after your @mail(...);
$r_conn = mysql_pconnect($s_server, $s_username, $s_password);
$s_SQL = <<< END_SQL
INSERT INTO
tablename(title, surname, address, town, country, postcode, telephone, besttime, email)
VALUE('$title', '$surname', '$address', '$town', '$country', '$postcode', '$telephone', '$besttime', '$email')
END_SQL;
$r_results = mysql_query($s_SQL);
mysql_free_results($r_resu