我对编码一窍不通。我建立了一个名为 contacts 的 MYSQL 数据库,一个名为 contacttable 的表,其中包含字段 id、名字、姓氏、电子邮件地址、邮政编码和电话号码。每个都是文本或 varchars 除了 id,它是一个 auto_increment,pk 字段。连接不会给出任何错误,并且不会通过 mysqli_connect_error() 方法传递任何错误。他们的查询没有通过,也没有执行任何查询。我想不通为什么。
<html>
<head>
<title>Registration</title>
</head>
<body>
<h1>Register with Us!</h1>
<h2>Registration Complete!</h2>
<div class="feedback-container" <?= isset($_REQUEST["first-name"])? "style=\"display:block\"": "style=\"display:none\""; ?>>
<?php
$firstname = $lastname = $emailaddress = $postalcode = $phonenumber = NULL;
if (isset($_REQUEST["first-name"])){
$firstname = $_REQUEST["first-name"];
$lastname = $_REQUEST["last-name"];
$emailaddress = $_REQUEST["email-address"];
$postalcode = $_REQUEST["postal-code"];
$phonenumber = $_REQUEST["phone-number"];
$dbconn = new mysqli();
$dbconn->connect("localhost","root","","contacts");
if(mysqli_connect_error()){
echo "Connection Failed";
}else{
echo "Connection Established";
}
$query = "INSERT INTO 'contactstable' ('firstname', 'lastname', 'emailaddress','postalcode','phonenumber') VALUES ('$firstname', '$lastname', '$emailaddress', '$postalcode', '$phonenumber')";
if ($dbconn->query($query) == TRUE){
echo ("Thank you for registering with us. We will shortly send a confirmation email to $emailaddress.");
}else{
echo ("<p>Your contact information was not added to our database. Please try again later or contact our webadmin at webadmin@gmail.com</p>");
}
}
?>
</div>
</body>
运行时,输出如下: “连接已建立” “您的联系信息未添加到我们的数据库中。请稍后重试或通过 webadmin@gmail.com 联系我们的网络管理员” 没有错误消息。 没有数据更新。
最佳答案
我会做这个
$query = "INSERT INTO 'contactstable' ('firstname', 'lastname', 'emailaddress','postalcode','phonenumber') VALUES ('$firstname', '$lastname', '$emailaddress', '$postalcode', '$phonenumber')";
看起来像这样
$query = "INSERT INTO contactstable (firstname, lastname, emailaddress,postalcode,phonenumber) VALUES ('$firstname', '$lastname', '$emailaddress', '$postalcode', '$phonenumber')";
关于php - mysqli insert 不工作,没有错误信息,连接成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13132012/