字段未保存在记录上,但会收到电子邮件

huangapple go评论67阅读模式
英文:

Fields are not saved on the record but receive email

问题

// php 代码

<?php
session_start();

require_once(__DIR__ . '/sendEmails/vendor/autoload.php');

// 检查表单是否已提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // 获取表单数据
    $companyName = isset($_POST['company_name']) ? $_POST['company_name'] : '';
    $contactName = isset($_POST['contact_name']) ? $_POST['contact_name'] : '';
    $email = isset($_POST['email']) ? $_POST['email'] : '';
    $phone_number = isset($_POST['phone_number']) ? $_POST['phone_number'] : '';
    $address = isset($_POST['address']) ? $_POST['address'] : '';
    $city = isset($_POST['city']) ? $_POST['city'] : '';
    $state = isset($_POST['state']) ? $_POST['state'] : '';
    $zip_code = isset($_POST['zip']) ? $_POST['zip'] : '';

    // 检查电子邮件是否已存在于数据库中(根据您自己的逻辑替换)
    $emailExists = checkIfEmailExists($email); // 根据数据库设置实现此函数

    if ($emailExists) {
        // 如果电子邮件已存在,则重定向到 login.php
        header("Location: login.php");
        exit;
    }

    // 发送注册电子邮件给商户
    $mail = new PHPMailer\PHPMailer\PHPMailer();
    $mail->isSMTP();
    $mail->Host = 'smtp.outlook.com';
    $mail->Port = 587;
    $mail->SMTPAuth = true;
    $mail->Username = 'delegant@outlook.com';
    $mail->Password = '****';
    $mail->setFrom('gcizmandelegant@outlook.com', 'e-Shopper');
    $mail->addAddress($email, $contactName);
    $mail->Subject = 'Merchant Registration Successful';
    $mail->Body = '尊敬的' . $contactName . ',<br><br>您的商户注册已成功。<br><br>谢谢!';
    $mail->isHTML(true);

    if (!$mail->send()) {
        echo '无法发送电子邮件。';
        echo '邮件发送器错误: ' . $mail->ErrorInfo;
    } else {
        // 将商户数据插入数据库
        function insertMerchantData($companyName, $contactName, $email, $phone_number, $address, $city, $state, $zip_code) {
            // 数据库连接设置(使用您自己的数据库凭据替换)
            $dbHost = 'localhost';
            $dbName = 'ecommerce_store';
            $dbUsername = 'root';
            $dbPassword = '';

            try {
                // 创建一个新的 PDO 实例
                $pdo = new PDO("mysql:host=$dbHost;dbname=$dbName", $dbUsername, $dbPassword);
                $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

                // 准备并执行数据库查询
                $query = "INSERT INTO merchants (company_name, contact_name, email, phone_number, address, city, state, zip_code) VALUES (:companyName, :contactName, :email, :phone_number, :address, :city, :state, :zip_code)";
                $stmt = $pdo->prepare($query);
                $stmt->execute([
                    ':companyName' => $companyName,
                    ':contactName' => $contactName,
                    ':email' => $email,
                    ':phone_number' => $phone_number,
                    ':address' => $address,
                    ':city' => $city,
                    ':state' => $state,
                    ':zip_code' => $zip_code
                ]);

                return true; // 如果成功插入数据,则返回 true
            } catch(PDOException $e) {
                echo "数据库错误: " . $e->getMessage();
                return false; // 如果插入数据时出现错误,则返回 false
            }
        }
    }
}

// 检查电子邮件是否已存在于数据库中的函数
function checkIfEmailExists($email) {
    // 数据库连接设置(使用您自己的数据库凭据替换)
    $dbHost = 'localhost';
    $dbName = 'ecommerce_store';
    $dbUsername = 'root';
    $dbPassword = '';

    // 创建一个新的 PDO 实例
    try {
        $pdo = new PDO("mysql:host=$dbHost;dbname=$dbName", $dbUsername, $dbPassword);
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch(PDOException $e) {
        die("数据库连接失败: " . $e->getMessage());
    }

    // 准备并执行数据库查询以检查电子邮件是否存在
    $query = "SELECT * FROM merchants WHERE email = :email";
    $stmt = $pdo->prepare($query);
    $stmt->execute([':email' => $email]);

    // 检查查询是否返回任何行
    if ($stmt->rowCount() > 0) {
        // 电子邮件已存在
        return true;
    } else {
        // 电子邮件不存在
        return false;
    }
}
?>
英文:

I have server side code, i want to save these fields to the table 'merchants'. The problem now the only thing i could do is to submit the form and get email, while this part is my main issue and cant seem to fix it. I did couple of debugs steps, to check from the table when running a sql query nothing is being inserted. Yet debugg on the browser no error found. I need some help with server side code so can both get an email and save the new data to the table when new merchants register to the form.

// php code

`&lt;?php
session_start();
require_once(__DIR__ . &#39;/sendEmails/vendor/autoload.php&#39;);
// Check if the form has been submitted
// Check if the form has been submitted
if ($_SERVER[&#39;REQUEST_METHOD&#39;] === &#39;POST&#39;) {
// Retrieve form data
$companyName = isset($_POST[&#39;company_name&#39;]) ? $_POST[&#39;company_name&#39;] : &#39;&#39;;
$contactName = isset($_POST[&#39;contact_name&#39;]) ? $_POST[&#39;contact_name&#39;] : &#39;&#39;;
$email = isset($_POST[&#39;email&#39;]) ? $_POST[&#39;email&#39;] : &#39;&#39;;
$phone_number = isset($_POST[&#39;phone_number&#39;]) ? $_POST[&#39;phone_number&#39;] : &#39;&#39;;
$address = isset($_POST[&#39;address&#39;]) ? $_POST[&#39;address&#39;] : &#39;&#39;;
$city = isset($_POST[&#39;city&#39;]) ? $_POST[&#39;city&#39;] : &#39;&#39;;
$state = isset($_POST[&#39;state&#39;]) ? $_POST[&#39;state&#39;] : &#39;&#39;;
$zip_code = isset($_POST[&#39;zip&#39;]) ? $_POST[&#39;zip&#39;] : &#39;&#39;;
// Check if the email already exists in your database (replace with your own logic)
$emailExists = checkIfEmailExists($email); // Implement this function according to your database setup
if ($emailExists) {
// Redirect to login.php if email already exists
header(&quot;Location: login.php&quot;);
exit;
}
// Send registration email to the merchant
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail-&gt;isSMTP();
$mail-&gt;Host = &#39;smtp.outlook.com&#39;;
$mail-&gt;Port = 587;
$mail-&gt;SMTPAuth = true;
$mail-&gt;Username = &#39;delegant@outlook.com&#39;;
$mail-&gt;Password = &#39;****&#39;;
$mail-&gt;setFrom(&#39;gcizmandelegant@outlook.com&#39;, &#39;e-Shopper&#39;);
$mail-&gt;addAddress($email, $contactName);
$mail-&gt;Subject = &#39;Merchant Registration Successful&#39;;
$mail-&gt;Body = &#39;Dear &#39; . $contactName . &#39;,&lt;br&gt;&lt;br&gt;Your merchant registration was successful.&lt;br&gt;&lt;br&gt;Thank you!&#39;;
$mail-&gt;isHTML(true);
if (!$mail-&gt;send()) {
echo &#39;Email could not be sent.&#39;;
echo &#39;Mailer Error: &#39; . $mail-&gt;ErrorInfo;
} else {
// Insert merchant data into the database
function insertMerchantData($companyName, $contactName, $email, $phone_number, $address, $city, $state, $zip_code) {
// Database connection settings (replace with your own database credentials)
$dbHost = &#39;localhost&#39;;
$dbName = &#39;ecommerce_store&#39;;
$dbUsername = &#39;root&#39;;
$dbPassword = &#39;&#39;;
try {
// Create a new PDO instance
$pdo = new PDO(&quot;mysql:host=$dbHost;dbname=$dbName&quot;, $dbUsername, $dbPassword);
$pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Prepare and execute the database query
$query = &quot;INSERT INTO merchants (company_name, contact_name, email, phone_number, address, city, state, zip_code) VALUES (:companyName, :contactName, :email, :phone_number, :address, :city, :state, :zip_code)&quot;;
$stmt = $pdo-&gt;prepare($query);
$stmt-&gt;execute([
&#39;:companyName&#39; =&gt; $companyName,
&#39;:contactName&#39; =&gt; $contactName,
&#39;:email&#39; =&gt; $email,
&#39;:phone_number&#39; =&gt; $phone_number,
&#39;:address&#39; =&gt; $address,
&#39;:city&#39; =&gt; $city,
&#39;:state&#39; =&gt; $state,
&#39;:zip_code&#39; =&gt; $zip_code
]);
return true; // Return true if the data is inserted successfully
} catch(PDOException $e) {
echo &quot;Database error: &quot; . $e-&gt;getMessage();
return false; // Return false if there is an error inserting the data
}
}
}
}
// Function to check if the email already exists in the database
// Function to check if the email already exists in the database
function checkIfEmailExists($email) {
// Database connection settings (replace with your own database credentials)
$dbHost = &#39;localhost&#39;;
$dbName = &#39;ecommerce_store&#39;;
$dbUsername = &#39;root&#39;;
$dbPassword = &#39;&#39;;
// Create a new PDO instance
try {
$pdo = new PDO(&quot;mysql:host=$dbHost;dbname=$dbName&quot;, $dbUsername, $dbPassword);
$pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
die(&quot;Database connection failed: &quot; . $e-&gt;getMessage());
}
// Prepare and execute a database query to check if the email exists
$query = &quot;SELECT * FROM merchants WHERE email = :email&quot;;
$stmt = $pdo-&gt;prepare($query);
$stmt-&gt;execute(array(&#39;:email&#39; =&gt; $email));
// Check if the query returned any rows
if ($stmt-&gt;rowCount() &gt; 0) {
// Email already exists
return true;
} else {
// Email does not exist
return false;
}
}
?&gt;`

答案1

得分: 0

如果您想在新商户注册表单时同时发送电子邮件并将新数据保存到表格中,请查看以下代码。

我已将$mail->send()函数存储到一个变量中,然后检查它是否返回true或false。如果返回true,则调用数据存储函数。

<?php
session_start();

require_once(__DIR__ . '/sendEmails/vendor/autoload.php');

// 检查表单是否已提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // 检索表单数据
    $companyName = isset($_POST['company_name']) ? $_POST['company_name'] : '';
    $contactName = isset($_POST['contact_name']) ? $_POST['contact_name'] : '';
    $email = isset($_POST['email']) ? $_POST['email'] : '';
    $phone_number = isset($_POST['phone_number']) ? $_POST['phone_number'] : '';
    $address = isset($_POST['address']) ? $_POST['address'] : '';
    $city = isset($_POST['city']) ? $_POST['city'] : '';
    $state = isset($_POST['state']) ? $_POST['state'] : '';
    $zip_code = isset($_POST['zip']) ? $_POST['zip'] : '';

    // 检查电子邮件是否已在数据库中存在(根据您的数据库设置进行替换)
    $emailExists = checkIfEmailExists($email); // 根据您的数据库设置实现此函数

    if ($emailExists) {
        // 如果电子邮件已存在,则重定向到login.php
        header("Location: login.php");
        exit;
    }

    // 发送注册电子邮件给商户
    $mail = new PHPMailer\PHPMailer\PHPMailer();
    $mail->isSMTP();
    $mail->Host = 'smtp.outlook.com';
    $mail->Port = 587;
    $mail->SMTPAuth = true;
    $mail->Username = 'delegant@outlook.com';
    $mail->Password = '****';
    $mail->setFrom('gcizmandelegant@outlook.com', 'e-Shopper');
    $mail->addAddress($email, $contactName);
    $mail->Subject = 'Merchant Registration Successful';
    $mail->Body = 'Dear ' . $contactName . ',<br><br>Your merchant registration was successful.<br><br>Thank you!';
    $mail->isHTML(true);
    $emailSent = $mail->send();

    if (!$emailSent) {
        echo '无法发送电子邮件。';
        echo '邮件发送器错误: ' . $mail->ErrorInfo;
    } else {
        $this->insertMerchantData($companyName, $contactName, $email, $phone_number, $address, $city, $state, $zip_code)
    }
}

// 将商户数据插入数据库
public function insertMerchantData($companyName, $contactName, $email, $phone_number, $address, $city, $state, $zip_code) {
    // 数据库连接设置(用您自己的数据库凭据替换)
    $dbHost = 'localhost';
    $dbName = 'ecommerce_store';
    $dbUsername = 'root';
    $dbPassword = '';

    try {
        // 创建一个新的PDO实例
        $pdo = new PDO("mysql:host=$dbHost;dbname=$dbName", $dbUsername, $dbPassword);
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        // 准备并执行数据库查询
        $query = "INSERT INTO merchants (company_name, contact_name, email, phone_number, address, city, state, zip_code) VALUES (:companyName, :contactName, :email, :phone_number, :address, :city, :state, :zip_code)";
        $stmt = $pdo->prepare($query);
        $stmt->execute([
            ':companyName' => $companyName,
            ':contactName' => $contactName,
            ':email' => $email,
            ':phone_number' => $phone_number,
            ':address' => $address,
            ':city' => $city,
            ':state' => $state,
            ':zip_code' => $zip_code
        ]);

        return true; // 如果数据成功插入,则返回true
    } catch(PDOException $e) {
        echo "数据库错误: " . $e->getMessage();
        return false; // 如果插入数据时出现错误,则返回false
    }
}
?>

// 检查电子邮件是否已在数据库中存在
function checkIfEmailExists($email) {
// 数据库连接设置(用您自己的数据库凭据替换)
$dbHost = 'localhost';
$dbName = 'ecommerce_store';
$dbUsername = 'root';
$dbPassword = '';

// 创建一个新的PDO实例
try {
$pdo = new PDO("mysql:host=$dbHost;dbname=$dbName", $dbUsername, $dbPassword);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
die("数据库连接失败: " . $e->getMessage());
}

// 准备并执行一个数据库查询以检查电子邮件是否存在
$query = "SELECT * FROM merchants WHERE email = :email";
$stmt = $pdo->prepare($query);
$stmt->execute(array(':email' => $email));

// 检查查询是否返回任何行
if ($stmt->rowCount() > 0) {
// 电子邮件已存在
return true;
} else {
// 电子邮件不存在
return false;
}
}
?>


<details>
<summary>英文:</summary>
If you want to send the email and save the new data to the table at the same time when new merchants register to the form, check the below code. 
I have stored $mail-&gt;send() function to a variable and then checked whether it returns true or false. If it true then data store function calls.

<?php
session_start();

require_once(DIR . '/sendEmails/vendor/autoload.php');

// Check if the form has been submitted
// Check if the form has been submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Retrieve form data
$companyName = isset($_POST['company_name']) ? $_POST['company_name'] : '';
$contactName = isset($_POST['contact_name']) ? $_POST['contact_name'] : '';
$email = isset($_POST['email']) ? $_POST['email'] : '';
$phone_number = isset($_POST['phone_number']) ? $_POST['phone_number'] : '';
$address = isset($_POST['address']) ? $_POST['address'] : '';
$city = isset($_POST['city']) ? $_POST['city'] : '';
$state = isset($_POST['state']) ? $_POST['state'] : '';
$zip_code = isset($_POST['zip']) ? $_POST['zip'] : '';

// Check if the email already exists in your database (replace with your own logic)
$emailExists = checkIfEmailExists($email); // Implement this function according to your database setup
if ($emailExists) {
// Redirect to login.php if email already exists
header(&quot;Location: login.php&quot;);
exit;
}
// Send registration email to the merchant
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail-&gt;isSMTP();
$mail-&gt;Host = &#39;smtp.outlook.com&#39;;
$mail-&gt;Port = 587;
$mail-&gt;SMTPAuth = true;
$mail-&gt;Username = &#39;delegant@outlook.com&#39;;
$mail-&gt;Password = &#39;****&#39;;
$mail-&gt;setFrom(&#39;gcizmandelegant@outlook.com&#39;, &#39;e-Shopper&#39;);
$mail-&gt;addAddress($email, $contactName);
$mail-&gt;Subject = &#39;Merchant Registration Successful&#39;;
$mail-&gt;Body = &#39;Dear &#39; . $contactName . &#39;,&lt;br&gt;&lt;br&gt;Your merchant registration was successful.&lt;br&gt;&lt;br&gt;Thank you!&#39;;
$mail-&gt;isHTML(true);
$emailSent = $mail-&gt;send();
if (!$emailSent) {
echo &#39;Email could not be sent.&#39;;
echo &#39;Mailer Error: &#39; . $mail-&gt;ErrorInfo;
} else {
$this-&gt;insertMerchantData($companyName, $contactName, $email, $phone_number, $address, $city, $state, $zip_code)

}

// Insert merchant data into the database
public function insertMerchantData($companyName, $contactName, $email, $phone_number, $address, $city, $state, $zip_code) {
// Database connection settings (replace with your own database credentials)
$dbHost = 'localhost';
$dbName = 'ecommerce_store';
$dbUsername = 'root';
$dbPassword = '';

try {
// Create a new PDO instance
$pdo = new PDO(&quot;mysql:host=$dbHost;dbname=$dbName&quot;, $dbUsername, $dbPassword);
$pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Prepare and execute the database query
$query = &quot;INSERT INTO merchants (company_name, contact_name, email, phone_number, address, city, state, zip_code) VALUES (:companyName, :contactName, :email, :phone_number, :address, :city, :state, :zip_code)&quot;;
$stmt = $pdo-&gt;prepare($query);
$stmt-&gt;execute([
&#39;:companyName&#39; =&gt; $companyName,
&#39;:contactName&#39; =&gt; $contactName,
&#39;:email&#39; =&gt; $email,
&#39;:phone_number&#39; =&gt; $phone_number,
&#39;:address&#39; =&gt; $address,
&#39;:city&#39; =&gt; $city,
&#39;:state&#39; =&gt; $state,
&#39;:zip_code&#39; =&gt; $zip_code
]);
return true; // Return true if the data is inserted successfully
} catch(PDOException $e) {
echo &quot;Database error: &quot; . $e-&gt;getMessage();
return false; // Return false if there is an error inserting the data
}

}

}

}

// Function to check if the email already exists in the database
// Function to check if the email already exists in the database
function checkIfEmailExists($email) {
// Database connection settings (replace with your own database credentials)
$dbHost = 'localhost';
$dbName = 'ecommerce_store';
$dbUsername = 'root';
$dbPassword = '';

// Create a new PDO instance
try {
$pdo = new PDO("mysql:host=$dbHost;dbname=$dbName", $dbUsername, $dbPassword);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
die("Database connection failed: " . $e->getMessage());
}

// Prepare and execute a database query to check if the email exists
$query = "SELECT * FROM merchants WHERE email = :email";
$stmt = $pdo->prepare($query);
$stmt->execute(array(':email' => $email));

// Check if the query returned any rows
if ($stmt->rowCount() > 0) {
// Email already exists
return true;
} else {
// Email does not exist
return false;
}
}

?>```

huangapple
  • 本文由 发表于 2023年5月25日 13:16:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76329131.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定