我有以下php代码将数据插入mysql并同时发送电子邮件。电子邮件正在发送,但数据未插入数据库。
<?php
$connection = mysqli_connect("localhost", "username", "password", "database_name");
$cName = $_POST['cName'];
$contact = $_POST['contact'];
$address = $_POST['address'];
$query = mysqli_query($connection,"INSERT INTO registration(CompanyName,Contact,Address) VALUES('$cName','$contact','$address')");
$message = "Registration Details";
$message .= $cName;
$message .= $contact;
$message .= $address;
mail("example@domain.com", "Registration Email", $message);
mysql_close($connection);
?>
问题是这个代码在我的本地机器上使用localhost正常工作。当我把它放在网上时出现问题。电子邮件正在发送但数据未插入。请帮帮我..
答案 0 :(得分:-1)
只需修改代码即可调试错误。这是您正确的完整代码。
<?php
$connection = mysqli_connect("localhost", "username", "password", "database_name");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform a query, check for error
if (!mysqli_query($connection,"INSERT INTO registration(CompanyName,Contact,Address) VALUES('".$cName."','".$contact."','".$address."')"))
{
echo("Error description: " . mysqli_error($connection));
}
$message = "Registration Details";
$message .= $cName;
$message .= $contact;
$message .= $address;
mail("example@domain.com", "Registration Email", $message);
mysqli_close($connection);
?>