Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    How do I send form data to my email and my MYSQL database?

    I have one form that is sending basic text information and a photo attachment to my MYSQL database and to my email address. I know how to store the text and the image to the database and I know how to send the text and picture to my email. The problem is I cannot seem to get the two scripts to execute all on the same page. Once I hit submit on my form the page reloads itself where the two PHP scripts are located. Once the page is located the first script that is on the page gets executed first but it wont go to the next php script as it stops at the first script and only executes the first script that is on the page if its the database script or the email script. In my case I have the email script first and the database script second. I need to have both scripts executed. How do I edit my code to have both scripts executed and not just the first one. The code seems to stop executing once it executes the email script, In need it to continue on to the next script which is the script that makes it so the user can create an account and it sends all the form information to the database as well. Here is the PHP code.


    This is the email script:
    ____________________________________________

    <?php if ($_SERVER['REQUEST_METHOD']=="POST"){

    // we'll begin by assigning the To address and message subject
    $to="ec10.nate@gmail.com";

    $subject="New Member";

    // get the sender's name and email address
    // we'll just plug them a variable to be used later
    $from = stripslashes($_POST['Firstname'])."<".stripslashes($_POST['contactemail']).">";

    if(empty($_POST['name']) ||
    empty($_POST['email']) ||
    empty($_POST['haircolor']) ||
    empty($_POST['message']))
    {
    $errors .= "\n Error: all fields are required";
    }


    // POST the values


    $Video_Yearly_Package = $_POST['Video_Yearly_Package'];
    $Card_Brand = $_POST['Card_Brand'];
    $Card_Number = $_POST['Card_Number'];
    $Security_Code = $_POST['Security_Code'];
    $Name_On_Card = $_POST['Name_On_Card'];
    $Expiration_Date_Month = $_POST['Expiration_Date_Month'];
    $Expiration_Date_Year = $_POST['Expiration_Date_Year'];
    $username = $_POST['username'];
    $password1 = $_POST['password1'];
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $Your_Title_In_Company = $_POST['Your_Title_In_Company'];
    $Company_Name = $_POST['Company_Name'];
    $Company_Phone_Number = $_POST['Company_Phone_Number'];
    $Country_Business_Is_Based_In = $_POST['Country_Business_Is_Based_In'];
    $State_Where_Company_Is_Located = $_POST['State_Where_Company_Is_Located'];
    $City_Where_Company_Is_Located = $_POST['City_Where_Company_Is_Located'];
    $Company_Address = $_POST['Company_Address'];
    $Product_Video_URL = $_POST['Product_Video_URL'];
    $Type_Of_Product_The_Company_Supplies_To_OEM = $_POST['Type_Of_Product_The_Company_Supplies_To_OEM'];
    $Website_Where_People_Can_Buy_The_Product = $_POST['Website_Where_People_Can_Buy_The_Product'];
    $List_Of_All_The_Manufactures_Your_Company_Supplies_To = $_POST['List_Of_All_The_Manufactures_Your_Company_Supplies_To'];
    $About = $_POST['About'];


    if (!eregi(
    "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$",
    $email_address))
    {
    $errors .= "\n Error: Invalid email address";
    }

    // generate a random string to be used as the boundary marker
    $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";

    // store the file information to variables for easier access
    $tmp_name = $_FILES['new_picture']['tmp_name'];
    $type = $_FILES['new_picture']['type'];
    $file_name = $_FILES['new_picture']['name'];
    $size = $_FILES['new_picture']['size'];

    // here we'll hard code a text message
    // again, in reality, you'll normally get this from the form submission
    $message = "\n\n Video_Yearly_Package: $Video_Yearly_Package \n\n Card_Brand: $Card_Brand \n\n Card_Number: $Card_Number \n\n Security_Code: $Security_Code \n\n Name_On_Card: $Name_On_Card \n\n Expiration_Date_Month: $Expiration_Date_Month \n\n Expiration_Date_Year: $Expiration_Date_Year \n\n username: $username \n\n password1: $password1 \n\n fname: $fname \n\n lname: $lname \n\n Your_Title_In_Company: $Your_Title_In_Company \n\n Company_Name: $Company_Name \n\n Company_Phone_Number: $Company_Phone_Number \n\n Country_Business_Is_Based_In: $Country_Business_Is_Based_In \n\n State_Where_Company_Is_Located: $State_Where_Company_Is_Located \n\n City_Where_Company_Is_Located: $City_Where_Company_Is_Located \n\n Company_Address: $Company_Address \n\n Product_Video_URL: $Product_Video_URL \n\n Type_Of_Product_The_Company_Supplies_To_OEM: $Type_Of_Product_The_Company_Supplies_To_OEM \n\n Website_Where_People_Can_Buy_The_Product: $Website_Where_People_Can_Buy_The_Product \n\n List_Of_All_The_Manufactures_Your_Company_Supplies_To: $List_Of_All_The_Manufactures_Your_Company_Supplies_To \n\n About: $About \n\n Here is your file: $new_picture";


    // if the upload succeded, the file will exist
    if (file_exists($tmp_name)){

    // check to make sure that it is an uploaded file and not a system file
    if(is_uploaded_file($tmp_name)){

    // open the file for a binary read
    $file = fopen($tmp_name,'rb');

    // read the file content into a variable
    $data = fread($file,filesize($tmp_name));

    // close the file
    fclose($file);

    // now we encode it and split it into acceptable length lines
    $data = chunk_split(base64_encode($data));
    }

    // now we'll build the message headers
    $headers = "From: $from\r\n" .
    "MIME-Version: 1.0\r\n" .
    "Content-Type: multipart/mixed;\r\n" .
    " boundary=\"{$mime_boundary}\"";

    // next, we'll build the message body
    // note that we insert two dashes in front of the
    // MIME boundary when we use it
    $message = "This is a multi-part message in MIME format.\n\n" .
    "--{$mime_boundary}\n" .
    "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
    "Content-Transfer-Encoding: 7bit\n\n" .
    $message . "\n\n";

    // now we'll insert a boundary to indicate we're starting the attachment
    // we have to specify the content type, file name, and disposition as
    // an attachment, then add the file content and set another boundary to
    // indicate that the end of the file has been reached
    $message .= "--{$mime_boundary}\n" .
    "Content-Type: {$type};\n" .
    " name=\"{$file_name}\"\n" .
    //"Content-Disposition: attachment;\n" .
    //" filename=\"{$fileatt_name}\"\n" .
    "Content-Transfer-Encoding: base64\n\n" .
    $data . "\n\n" .
    "--{$mime_boundary}--\n";

    // now we just send the message
    if (@mail($to, $subject, $message, $headers))
    header("Location: index.php");
    else
    echo "Failed to send";
    }
    } else {
    ?>


    _________________________________

    Right after the email script the database script is next:


    <?php
    require_once('appvars.php');
    require_once('connectvars.php');

    // Connect to the database
    $dbc = mysqli_connect('rpss.db.6579954.hostedresource.com', 'rpss', 'Azusa777', 'rpss');

    if (isset($_POST['submit'])) {
    // Grab the profile data from the POST

    $Video_Yearly_Package = mysqli_real_escape_string($dbc, trim($_POST['Video_Yearly_Package']));
    $Card_Brand = mysqli_real_escape_string($dbc, trim($_POST['Card_Brand']));
    $Card_Number = mysqli_real_escape_string($dbc, trim($_POST['Card_Number']));
    $Security_Code = mysqli_real_escape_string($dbc, trim($_POST['Security_Code']));
    $Name_On_Card = mysqli_real_escape_string($dbc, trim($_POST['Name_On_Card']));
    $Expiration_Date_Month = mysqli_real_escape_string($dbc, trim($_POST['Expiration_Date_Month']));
    $Expiration_Date_Year = mysqli_real_escape_string($dbc, trim($_POST['Expiration_Date_Year']));
    $username = mysqli_real_escape_string($dbc, trim($_POST['username']));
    $password1 = mysqli_real_escape_string($dbc, trim($_POST['password1']));
    $password2 = mysqli_real_escape_string($dbc, trim($_POST['password2']));
    $fname = mysqli_real_escape_string($dbc, trim($_POST['fname']));
    $lname = mysqli_real_escape_string($dbc, trim($_POST['lname']));
    $Your_Title_In_Company = mysqli_real_escape_string($dbc, trim($_POST['Your_Title_In_Company']));
    $Company_Name = mysqli_real_escape_string($dbc, trim($_POST['Company_Name']));
    $Company_Phone_Number = mysqli_real_escape_string($dbc, trim($_POST['Company_Phone_Number']));
    $Country_Business_Is_Based_In = mysqli_real_escape_string($dbc, trim($_POST['Country_Business_Is_Based_In']));
    $State_Where_Company_Is_Located = mysqli_real_escape_string($dbc, trim($_POST['State_Where_Company_Is_Located']));
    $City_Where_Company_Is_Located = mysqli_real_escape_string($dbc, trim($_POST['City_Where_Company_Is_Located']));
    $Company_Address = mysqli_real_escape_string($dbc, trim($_POST['Company_Address']));
    $Product_Video_URL = mysqli_real_escape_string($dbc, trim($_POST['Product_Video_URL']));
    $Type_Of_Product_The_Company_Supplies_To_OEM = mysqli_real_escape_string($dbc, trim($_POST['Type_Of_Product_The_Company_Supplies_To_OEM']));
    $Website_Where_People_Can_Buy_The_Product = mysqli_real_escape_string($dbc, trim($_POST['Website_Where_People_Can_Buy_The_Product']));
    $List_Of_All_The_Manufactures_Your_Company_Supplies_To = mysqli_real_escape_string($dbc, trim($_POST['List_Of_All_The_Manufactures_Your_Company_Supplies_To']));
    $About = mysqli_real_escape_string($dbc, trim($_POST['About']));
    $old_picture = mysqli_real_escape_string($dbc, trim($_POST['old_picture']));
    $new_picture = mysqli_real_escape_string($dbc, trim($_FILES['new_picture']['name']));
    $new_picture_type = $_FILES['new_picture']['type'];
    $new_picture_size = $_FILES['new_picture']['size'];
    list($new_picture_width, $new_picture_height) = getimagesize($_FILES['new_picture']['tmp_name']);
    $error = false;

    // Validate and move the uploaded picture file, if necessary
    if (!empty($new_picture)) {
    if ((($new_picture_type == 'image/gif') || ($new_picture_type == 'image/jpeg') || ($new_picture_type == 'image/pjpeg') ||
    ($new_picture_type == 'image/png')) && ($new_picture_size > 0) && ($new_picture_size <= MM_MAXFILESIZE) &&
    ($new_picture_width <= MM_MAXIMGWIDTH) && ($new_picture_height <= MM_MAXIMGHEIGHT)) {
    if ($_FILES['file']['error'] == 0) {
    // Move the file to the target upload folder
    $target = MM_UPLOADPATH . basename($new_picture);
    if (move_uploaded_file($_FILES['new_picture']['tmp_name'], $target)) {
    // The new picture file move was successful, now make sure any old picture is deleted
    if (!empty($old_picture) && ($old_picture != $new_picture)) {
    @unlink(MM_UPLOADPATH . $old_picture);
    }
    }
    else {
    // The new picture file move failed, so delete the temporary file and set the error flag
    @unlink($_FILES['new_picture']['tmp_name']);
    $error = true;
    echo '<p class="error">Sorry, there was a problem uploading your picture.</p>';
    }
    }
    }
    else {
    // The new picture file is not valid, so delete the temporary file and set the error flag
    @unlink($_FILES['new_picture']['tmp_name']);
    $error = true;
    echo '<p class="error">Your picture must be a GIF, JPEG, or PNG image file no greater than ' . (MM_MAXFILESIZE / 1024) .
    ' KB and ' . MM_MAXIMGWIDTH . 'x' . MM_MAXIMGHEIGHT . ' pixels in size.</p>';
    }
    }
    // Update the profile data in the database


    // Only set the picture column if there is a new picture
    if (!empty($new_picture)) {
    $query = "UPDATE mismatch_user SET picture = '$new_picture' WHERE user_id = '" . $_SESSION['user_id'] . "'";
    }

    mysqli_query($dbc, $query);

    // Grab the profile data from the database
    $query = "SELECT picture FROM mismatch_user WHERE user_id = '" . $_SESSION['user_id'] . "'";
    $data = mysqli_query($dbc, $query);
    $row = mysqli_fetch_array($data);

    if ($row != NULL) {
    $old_picture = $row['picture'];
    }


    if (!empty($username) && !empty($password1) && !empty($password2) && ($password1 == $password2)) {
    // Make sure someone isn't already registered using this username
    $query = "SELECT * FROM mismatch_user WHERE username = '$username'";
    $data = mysqli_query($dbc, $query);
    if (mysqli_num_rows($data) == 0) {


    // The username is unique, so insert the data into the database
    $query = "INSERT INTO mismatch_user (Video_Yearly_Package,Card_Brand, Card_Number, Security_Code, Name_On_Card, Expiration_Date_Month, Expiration_Date_Year, username, password, fname, lname, Your_Title_In_Company, Company_Name, Company_Phone_Number, Country_Business_Is_Based_In, State_Where_Company_Is_Located, City_Where_Company_Is_Located, Company_Address, Product_Video_URL, Type_Of_Product_The_Company_Supplies_To_OEM, Website_Where_People_Can_Buy_The_Product, List_Of_All_The_Manufactures_Your_Company_Supplies_To, About, join_date) VALUES ('$Video_Yearly_Package','$Card_Brand','$Card_Number','$Security_Code','$Name_On_Card','$Expiration_ Date_Month','$Expiration_Date_Year','$username', SHA('$password1'),'$fname','$lname','$Your_Title_In_Company','$Company_Name','$Company_Phone_Number' ,'$Country_Business_Is_Based_In','$State_Where_Company_Is_Located','$City_Where_Company_Is_Located', '$Company_Address','$Product_Video_URL','$Type_Of_Product_The_Company_Supplies_To_OEM','$Website_Whe re_People_Can_Buy_The_Product','$List_Of_All_The_Manufactures_Your_Company_Supplies_To','$About', NOW())";
    mysqli_query($dbc, $query);




    // Confirm success with the user
    header("Location: Supplier_Login_Form.php");

    mysqli_close($dbc);
    exit();
    }
    else {
    // An account already exists for this username, so display an error message
    echo '<p class="error">An account already exists for this username. Please use a different address.</p>';
    $username = "";
    }
    }
    else {
    echo '<p class="error">You must enter all of the sign-up data, including the desired password twice.</p>';
    }
    }



    mysqli_close($dbc);
    ?>




    ________________________________






    Both scripts work fine but how do I go from one script to the next once I hit the submit button? I need both scripts to execute starting with the email script first


    Thanks Much!
    Nate

  • #2
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Please wrap your code in [PHP] tags!

    After sending the email your code redirects to index.php:

    PHP Code:
    // now we just send the message
    if (@mail($to$subject$message$headers))
    header("Location: index.php"); 
    so any code that follows this will never be executed. Delete this header() and, if possible, combine the two scripts to one. Or include the second script after the first one.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • Users who have thanked AndrewGSW for this post:

    theebusinessman (09-12-2012)

  • #3
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    I get an unexpected $end parse error:

    Hello,

    Thank you very much for the reply and help. I deleted this header with the location below:

    Before I deleted the header:

    PHP Code:
    // now we just send the message
              
    if (@mail($to$subject$message$headers))
                
    header("Location: index.php");
              else
                 echo 
    "Failed to send";
           }
        } else {
        
    ?> 

    After I deleted the header:

    PHP Code:
     // now we just send the message
              
    if (@mail($to$subject$message$headers))
              else
                 echo 
    "Failed to send";
           }
        } else {
        
    ?> 


    After I deleted the header I saved the page and then uploaded it onto my live server. When I loaded the page on the browser I get this error that is generated and printed onto the screen:

    Parse error: syntax error, unexpected $end in /home/content/54/6579954/html/RipeProducts/Supplier_Sign-Up-Form.php on line 2165

    I am thinking this is because of the If Else statement that is tied into the email sending part of the code but I am not sure. How could I go about fixing the code?

    Thanks for all the help!,
    Nate
    Last edited by theebusinessman; 09-12-2012 at 09:06 PM. Reason: Editing code:

  • #4
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    One approach:

    PHP Code:
     // now we just send the message
              
    if (!mail($to$subject$message$headers))
                 echo 
    "Failed to send";
           }
        } else {
        
    ?> 
    ! means "not". I removed @, which suppresses errors, as mail() will return true or false to indicate that the email was sent. [I suppose you could leave the @ in..]
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • Users who have thanked AndrewGSW for this post:

    theebusinessman (09-13-2012)

  • #5
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thank you for all the help. I used some of what you gave me and then integrated it into some other code I had. This is what I came up with and it works great! Everything is now resolved and working better than ever!

    PHP Code:
     // now we just send the message
    mail($to,$subject,$message,$headers);
    // BELOW IS WHERE I TIED THE SCRIPT INTO ONE WHICH WORKED GREAT!
            
    require_once('appvars.php');
      require_once(
    'connectvars.php');

      
    // Connect to the database 







    Then the code just continued. I tied the two scripts into one.

    Thank you for all your help!
    Nate
    Last edited by theebusinessman; 09-13-2012 at 02:10 AM.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •