无法将数据字段插入数据库

时间:2017-12-04 17:38:49

标签: php mysql forms

我有 just forms 脚本,可以从表单生成数据字段并将其插入数据库。

然而,当我尝试发送数据时,我收到了数据发送的消息,但实际上我没有在数据库中看到它。

我的数据库中有一个名为cantaki的表,它包含以下列:

contact_email  
contact_filename  
contact_id  
contact_name  
message  
mysql_table  
subject  
token  

我尝试将contact_email更改为email以及其他人更改,但数据不会插入。

以下是actions.php文件:

<?php
    header("Content-Type: text/html; charset=utf-8");

    if (!$_POST) exit;

    require dirname(__FILE__)."/validation.php";
    require dirname(__FILE__)."/csrf.php";

/************************************************/
/* Your data */
/************************************************/
    /* Your email goes here */
    $your_email = "mymail@gmail.com";

    /* Your name or your company name goes here */
    $your_name = "david";

    /* Message subject */
    $your_subject = "New message";

    /* Define your data to access MySQL database */
    define("CON_USER", "myDBuser");                 // your username
    define("CON_SERVER", "localhost");                  // your host
    define("CON_PASSWORD", "Mypass");               // your password
    define("CON_DATABASE", "myDB");             // your database

    /* Your database table goes here */
    $mysql_table = "cantaki";

/************************************************/
/* Settings */
/************************************************/
    /* Select validation for fields */
    /* If you want to validate field - true, if you don't - false */
    $validate_name      = true;
    $validate_email     = true;


    /* Select the action */
    /* If you want to do the action - true, if you don't - false */
    $send_letter            = true;
    $upload_file            = true;
    $duplicate_to_database  = true;

/************************************************/
/* Variables */
/************************************************/
    /* Error variables */
    $error_text     = array();
    $error_message  = '';

    /* Last row ID */
    /* In case, if data will not be duplicated to a database */
    $row_id = "No data in a database";

    /* File name */
    /* In case, if a file will not be uploaded to a server */
    $file_name = "No file for upload";

    /* Allowed file types */
    $valid_types = array("image/jpg", "image/jpeg", "image/png", "application/msword",
                            "application/vnd.openxmlformats-officedocument.wordprocessingml.document");

    /* POST data */
    $name    = (isset($_POST["name"]))          ? strip_tags(trim($_POST["name"]))          : false;
    $email   = (isset($_POST["email"]))         ? strip_tags(trim($_POST["email"]))         : false;
    $token   = (isset($_POST["token_contact"])) ? strip_tags(trim($_POST["token_contact"])) : false;

    $name    = htmlspecialchars($name, ENT_QUOTES, 'UTF-8');
    $email   = htmlspecialchars($email, ENT_QUOTES, 'UTF-8');
    $token   = htmlspecialchars($token, ENT_QUOTES, 'UTF-8');

    $name    = substr($name, 0, 50);
    $email   = substr($email, 0, 40);

/************************************************/
/* CSRF protection */
/************************************************/
    $new_token = new CSRF('contact');
    if (!$new_token->check_token($token)) {
        echo '<div class="error-message unit"><i class="fa fa-close"></i>Incorrect token. Please reload this webpage</div>';
        exit;
    }

/************************************************/
/* Validation */
/************************************************/
    /* Name */
    if ($validate_name){
        $result = validateName($name, 1);
        if ($result !== "valid") {
            $error_text[] = $result;
        }
    }

    /* Email */
    if ($validate_email){
        $result = validateEmail($email);
        if ($result !== "valid") {
            $error_text[] = $result;
        }
    }


    /* If validation error occurs */
    if ($error_text) {
        foreach ($error_text as $val) {
            $error_message .= '<li>' . $val . '</li>';
        }
        echo '<div class="error-message unit"><i class="fa fa-close"></i>Oops! The following errors occurred:<ul>' . $error_message . '</ul></div>';
        exit;
    }

/************************************************/
/* Upload file to the server */
/************************************************/
    /* Upload file */
    if ($upload_file) {
        $file_name = uploadFile();
    }

/************************************************/
/* Duplicate info to a database */
/************************************************/
    if ($duplicate_to_database) {

        /* Select type of connection to a database */
        /* If you want to use connection - true, if you don't - false */
        /* For proper work you have to select only one type of connection */

        /* Mysqli connection to DB */
        $mysqli_connect = true;
        if ($mysqli_connect) {
            require dirname(__FILE__)."/mysql.php";
            $row_id = queryMysqli($mysql_table, $name, $email);
        }
        /* PDO connection to DB */
        $pdo_connect = false;
        if ($pdo_connect) {
            require dirname(__FILE__)."/pdo.php";
            $row_id = queryPdo($mysql_table, $name, $email);
        }
    }

/************************************************/
/* Sending email */
/************************************************/
    if ($send_letter) {

        /* Send email using sendmail function */
        /* If you want to use sendmail - true, if you don't - false */
        /* If you will use sendmail function - do not forget to set '$smtp' variable to 'false' */
        $sendmail = true;
        if ($sendmail) {
            require dirname(__FILE__)."/phpmailer/PHPMailerAutoload.php";
            require dirname(__FILE__)."/message.php";
            $mail = new PHPMailer;
            $mail->isSendmail();
            $mail->IsHTML(true);
            $mail->From = $email;
            $mail->CharSet = "UTF-8";
            $mail->FromName = "J-forms";
            $mail->Encoding = "base64";
            $mail->ContentType = "text/html";
            $mail->addAddress($your_email, $your_name);
            $mail->Subject = $your_subject;
            $mail->Body = $letter;
            $mail->AltBody = "Use an HTML compatible email client";
        }

        /* Send email using smtp function */
        /* If you want to use smtp - true, if you don't - false */
        /* If you will use smtp function - do not forget to set '$sendmail' variable to 'false' */
        $smtp = false;
        if ($smtp) {
            require dirname(__FILE__)."/phpmailer/PHPMailerAutoload.php";
            require dirname(__FILE__)."/message.php";
            $mail = new PHPMailer;
            $mail->isSMTP();                                            // Set mailer to use SMTP
            $mail->Host = "smtp1.example.com;smtp2.example.com";        // Specify main and backup server
            $mail->SMTPAuth = true;                                     // Enable SMTP authentication
            $mail->Username = "your-username";                          // SMTP username
            $mail->Password = "your-password";                          // SMTP password
            $mail->SMTPSecure = "tls";                                  // Enable encryption, 'ssl' also accepted
            $mail->Port = 465;                                          // SMTP Port number e.g. smtp.gmail.com uses port 465
            $mail->IsHTML(true);
            $mail->From = $email;
            $mail->CharSet = "UTF-8";
            $mail->FromName = "J-forms";
            $mail->Encoding = "base64";
            $mail->Timeout = 200;
            $mail->SMTPDebug = 0;
            $mail->ContentType = "text/html";
            $mail->addAddress($your_email, $your_name);
            $mail->Subject = $your_subject;
            $mail->Body = $letter;
            $mail->AltBody = "Use an HTML compatible email client";
        }

        /* Multiple email recepients */
        /* If you want to add multiple email recepients - true, if you don't - false */
        /* Enter email and name of the recipients */
        $recipients = false;
        if ($recipients) {
            $recipients = array("email@domain.com" => "name of recipient",
                                "email@domain.com" => "name of recipient",
                                "email@domain.com" => "name of recipient"
                                );
            foreach ($recipients as $email => $name) {
                $mail->AddBCC($email, $name);
            }
        }

        /* if error occurs while email sending */
        if(!$mail->send()) {
            echo '<div class="error-message unit"><i class="fa fa-close"></i>Mailer Error: ' . $mail->ErrorInfo . '</div>';
            exit;
        }
    }

/************************************************/
/* Success message */
/************************************************/
    echo '<div class="success-message unit"><i class="fa fa-check"></i>Your message has been sent</div>';
?>

这是 mysql.php 文件:

<?php

/* Duplicate information to DB */
function queryMysqli($mysql_table, $contact_name, $contact_email) {

    /* Variables */
    $error_exists = false;
    $error_mysql = "";

    /* Connection to DB */
    /* Constants, that defined in action.php, are used */
    $link = mysqli_connect(CON_SERVER, CON_USER, CON_PASSWORD, CON_DATABASE);
    if (mysqli_connect_error()) {
        $error_mysql = ("Error connecting to database (" . mysqli_connect_errno() . ") ". mysqli_connect_error());
        return $error_mysql;
    }
    mysqli_set_charset($link, 'utf8');

    /* Query to DB */
    /* Add data to DB */
    $result = mysqli_query($link, "INSERT INTO ".$mysql_table."(`".$mysql_table."_id`,
                                                                    `".$mysql_table."_name`,
                                                                    `".$mysql_table."_email`,

                                                        VALUES (NULL,   '".mysqli_real_escape_string($link, $name)."',
                                                                        '".mysqli_real_escape_string($link, $email)."')");

    /* Get a last row ID to send in message */
    $row_id = mysqli_insert_id($link);

    /* If error occurs */
    if (!$result){
        $error_exists = true;
        $error_mysql = "Error database query: ".mysqli_error($link);
    }

    /* Return result */
    mysqli_close($link);
    return $error_exists ? $error_mysql : $row_id;
}
?>

你能告诉我我做错了吗?

0 个答案:

没有答案