connect_error) {
$error_message = "Connection failed: " . $conn->connect_error;
}
// Auto-create the table if it doesn't exist
$create_table_sql = "
CREATE TABLE IF NOT EXISTS licenses (
id INT AUTO_INCREMENT PRIMARY KEY,
user_email VARCHAR(100) NOT NULL,
product_name VARCHAR(100) NOT NULL,
license_key VARCHAR(50) NOT NULL UNIQUE,
activation_status BOOLEAN DEFAULT 0,
expiration_date DATE NOT NULL
)";
if ($conn->query($create_table_sql) !== TRUE) {
$error_message = "Error creating table: " . $conn->error;
}
// Prepare and bind the SQL query to insert the license data
if (!isset($error_message)) {
$stmt = $conn->prepare("INSERT INTO licenses (user_email, product_name, license_key, expiration_date) VALUES (?, ?, ?, ?)");
if ($stmt === false) {
$error_message = "SQL Prepare Failed: " . $conn->error;
} else {
$stmt->bind_param("ssss", $user_email, $product_name, $license_key, $expiration_date);
// Execute the query to insert the license record into the database
if ($stmt->execute()) {
$success_message = "License key generated and stored successfully!
";
$success_message .= "Your license key: $license_key
";
$success_message .= "Expiration Date: $expiration_date";
// Optionally, send the license key to the user via email
$subject = "Your License Key for $product_name";
$message = "Your license key is: $license_key\nExpiration Date: $expiration_date";
if (mail($user_email, $subject, $message)) {
$success_message .= "
Email sent successfully!";
} else {
$success_message .= "
Failed to send email.";
}
} else {
$error_message = "Error executing SQL: " . $stmt->error;
}
// Close the statement
$stmt->close();
}
}
// Close the connection
$conn->close();
}
}
?>