英文:
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
`<?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("Location: login.php");
exit;
}
// Send registration email to the merchant
$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);
if (!$mail->send()) {
echo 'Email could not be sent.';
echo 'Mailer Error: ' . $mail->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 = 'localhost';
$dbName = 'ecommerce_store';
$dbUsername = 'root';
$dbPassword = '';
try {
// Create a new PDO instance
$pdo = new PDO("mysql:host=$dbHost;dbname=$dbName", $dbUsername, $dbPassword);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Prepare and execute the database query
$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; // Return true if the data is inserted successfully
} catch(PDOException $e) {
echo "Database error: " . $e->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;
}
}
?>`
答案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->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("Location: login.php");
exit;
}
// Send registration email to the merchant
$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 'Email could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
$this->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("mysql:host=$dbHost;dbname=$dbName", $dbUsername, $dbPassword);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Prepare and execute the database query
$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; // Return true if the data is inserted successfully
} catch(PDOException $e) {
echo "Database error: " . $e->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;
}
}
?>```
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论