Need help adding mysql query to store details in database

Need help adding mysql query in my sendmail script to store “name”,“email”,“tel”,“message” in database.

This is my sendmail script.

if(isset($_POST['email'])) {
    $email_to = "your email";
    $email_subject = "New contact detail from website.";
    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
    // validation expected data exists
    if(!isset($_POST['name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['tel']) ||
        !isset($_POST['message'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    $first_name = $_POST['name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['tel']; // not required
    $comments = $_POST['message']; // required
    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  if(strlen($error_message) > 0) {
    $email_message = "Form details below.\n\n";
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
<!-- include your own success html here -->
Thank you for contacting us.



If you have a look down the forum a little, there are plenty of bits of PHP code that show how to connect to the database and run a query against it. If you find on that still uses the old-style mysql functions, don’t use it, use mysqli or PDO. Come back if you have any specific problems with your code. Have you created the database and the table yet?

1 Like

@droopsnoot Actually i want to know where should i place these code, so that details can be stored in database?

$con= mysqli_connect("localhost","root"," ","details") or die("Connection to database failed!!!");
$name=mysqli_real_escape_string($con, $_POST['name']);
$email=mysqli_real_escape_string($con, $_POST['email']);
$telephone=mysqli_real_escape_string($con, $_POST['tel']);
$message=mysqli_real_escape_string($con, $_POST['message']);
//insert into database
mysqli_query($con,"INSERT INTO details(`name`,`email`,`telephone`,`message`)VALUES('$name','$email','$telephone','$message')");

No i’ve not.

Thank you.

I would put code similar to that just before I send the email. That way, the data is still stored in the database even if the email fails to send.


ok I’ll try. :slight_smile:

@droopsnoot I placed it before “if(isset($_POST[‘email’]))” and it gives no errors but nothing is stored in the database also.

@droopsnoot Issue fixed by using “backtick” in mysql query.

Thanks i got it, i’ve also added some validation. :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.