#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    9
    Rep Power
    0

    Mail success but not sending AND fail to insert data from contact form to database


    Hi there, i am having a problem with the contact form of my website.

    I get success message but emails does not deliver to my inbox, and it also fails to import the data from the contact form to database.
    Here is the code:

    Html file (trimmed):

    PHP Code:
        <form name="freecontactform" method="post" action="freecontactformprocess.php" onsubmit="return validate.check(this)"


    freecontactformprocess.php:


    PHP Code:
    <?php

    //configuration
    include config.php;
      
      


    if(isset(
    $_POST['Email_Address'])) {
        
        include 
    'contactformsettings.php';
        
    //include 'insert.php'
        
        
    function died($error) {
            echo 
    "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 />";
            die();
        }
        
        if(!isset(
    $_POST['First_Name']) ||
            !isset(
    $_POST['Last_Name']) ||
            !isset(
    $_POST['Email_Address']) ||
            !isset(
    $_POST['Telephone_Number']) ||
            !isset(
    $_POST['Your_Message']) || 
            !isset(
    $_POST['AntiSpam'])        
            ) {
            
    died('Sorry, there appears to be a problem with your form submission.');        
        }
        
        
    $full_name $_POST['First_Name']; // required
        
    $full_name $_POST['Last_Name']; // required
        
    $email_from $_POST['Email_Address']; // required
        
    $telephone $_POST['Telephone_Number']; // not required
        
    $comments $_POST['Your_Message']; // required
        
    $antispam $_POST['AntiSpam']; // 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)==0) {
          
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
      }
      if(
    strlen($full_name) < 2) {
          
    $error_message .= 'Your Name 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(
    $antispam <> $antispam_answer) {
        
    $error_message .= 'The Anti-Spam answer you entered is not correct.<br />';
      }
      
      if(
    strlen($error_message) > 0) {
          
    died($error_message);
      }
        
    $email_message "Form details below.\r\n";
        
        function 
    clean_string($string) {
          
    $bad = array("content-type","bcc:","to:","cc:");
          return 
    str_replace($bad,"",$string);
        }
        
        
    $email_message .= "First Name: ".clean_string($full_name)."\r\n";
        
    $email_message .= "Last Name: ".clean_string($full_name)."\r\n";
        
    $email_message .= "Email: ".clean_string($email_from)."\r\n";
        
    $email_message .= "Telephone: ".clean_string($telephone)."\r\n";
        
    $email_message .= "Message: ".clean_string($comments)."\r\n";
        
    $headers 'From: '.$email_from."\r\n".
    'Reply-To: '.$email_from."\r\n" .
    'X-Mailer: PHP/' phpversion();
    mail($email_to$email_subject$email_message$headers);
    header("Location: $thankyou");




    //import data to database

    include insert.php;





    ?>
    <script>location.replace('<?php echo $thankyou;?>')</script>
    <?php
    }
    die();
    ?>


    insert.php code:

    PHP Code:
    $sql="INSERT INTO contacts (ID, Firstname, Lastname, Phone, Email)
    VALUES

    ('
    $_POST[ID]',
    '
    $_POST[Firstname]',
    '
    $_POST[Lastname]',
    '
    $_POST[Phone]',
    '
    $_POST[Email]')";

    if (!
    mysqli_query($con,$sql))
      {
      die(
    'Error: ' mysqli_error());
      }
    echo 
    "1 record added";

    mysqli_close($con);

    $results mysqli_query($link"select * from proj2.contacts;");


    $row mysqli_fetch_array($results);
    print 
    $row[0]; 

    config.php code:


    PHP Code:
    $con=mysqli_connect("127.0.0.1:3306","root");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo 
    "Failed to connect to MySQL: " mysqli_connect_error();
      }

    // Create database


    mysql_query("DROP DATABASE IF EXISTS proj2");
    $sql="CREATE DATABASE proj2";
    if (
    mysqli_query($con,$sql))
      {
      echo 
    "Database proj2 created successfully";
      }
    else
      {
      echo 
    "Error creating database: " mysqli_error();
      }
      
      
      
      
    // Create table
      
    $con=mysqli_connect("127.0.0.1:3306","root""""proj2");
      
      
    // Check connection
    if (mysqli_connect_errno())
      {
      echo 
    "Failed to connect to MySQL: " mysqli_connect_error();
      }

      
      
    mysql_query("DROP TABLE IF EXISTS contacts");

    $sql="CREATE TABLE contacts
        (
        ID INT(5) NOT NULL AUTO_INCREMENT,
        PRIMARY KEY(ID),
        Firstname CHAR(30),
        Lastname CHAR(30), 
        Phone VARCHAR(45), 
        Email VARCHAR(45))"
    ;
        
        


    // Execute query
    if (mysqli_query($con,$sql))
      {
      echo 
    "Table persons created successfully";
      }
    else
      {
      echo 
    "Error creating table: " mysqli_error($con);
      } 
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,600
    Rep Power
    595
    Why are you recreating the database and tables every time? With all that going on, its no wonder you are having trouble debugging this. You need to rethink your logic. The database and tables should only be created once and outside of PHP.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    9
    Rep Power
    0
    Originally Posted by gw1500se
    Why are you recreating the database and tables every time? With all that going on, its no wonder you are having trouble debugging this. You need to rethink your logic. The database and tables should only be created once and outside of PHP.
    So i should run config.php once and delete include config.php from proccess.php or create the db and the tables directly through mysql ?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,600
    Rep Power
    595
    Create the database and tables through mysql. You probably should create a SQL command file to save it in the event you do need to recreate everything some time. Throw away all that stuff in your PHP script. Just use PHP to manage the records.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.

IMN logo majestic logo threadwatch logo seochat tools logo