我想在计算完域后将该值存储在数据库中。据我所知,我已正确完成了所有操作,但数据未发送到表中。
注意:我的mysql服务器在AWS中,并且我已经打开了mysql端口
我的代码在下面,
<?php
$con = mysqli_connect("localhost","user_name","passwd","db_name");
$sql = "INSERT INTO contact_form (name, mobile, email, message) VALUES ('".$_POST['name']."', '".$_POST['phone']."', '".$_POST['email']."', '".$_POST['message']."')";
if (mysqli_query($con, $sql)) {
echo "New record created successfully";
$to = 'contact@xxx.com';
$subject = 'Contact Form';
$from = 'contact@xxx.com';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Create email headers
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
// Compose a simple HTML email message
$message = '<html><body>';
$message .= '<h1 style="color:#f40;">Hi!</h1>';
$message .= '<p>Name :'. $_POST['name'] .' </p>';
$message .= '<p>Contact Number :'.$_POST['phone'] .'</p>';$message .= '<p>Email :'.$_POST['email'] .'</p>';
$message .= '<p>Message :'.$_POST['message'] .'</p>';
$message .= '</body></html>';
// Sending email
mail($to, $subject, $message, $headers);
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
mysqli_close($con);
?>
请帮帮我。我不太擅长mysql。在此先感谢!!
答案 0 :(得分:2)
A愧,首先,您需要检查是否已设置$ _POST且它不为空。然后,您需要将此数据分配给自定义变量。
希望对您有帮助。
$con = mysqli_connect("localhost","user_name","passwd","db_name");
if (isset($_POST['name']) && isset($_POST['phone']) && isset($_POST['email']) && isset($_POST['message'])){
$username = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];
$sql = "INSERT INTO contact_form (name, mobile, email, message) VALUES ('$username', '$phone', '$email', '$message')";
$result = $con->query($sql);
}