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;
}
}
?>`
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;
}
}
?>```