【发布时间】:2020-10-02 18:59:20
【问题描述】:
我正在尝试将数据元素或从提交表单获取到 MySql 数据库。我已经以本地方式连接到数据库。但是现在我想从网页的 webhook 中获取数据,我无法检索数据。这是我的代码:
<!doctype html>
<html>
<body>
<?php
//Data Base connection info:
$servername = "localhost";
$username = "username";
$password = "password";
$dbName="dbName";
////////////////////////////Retrieved information from form ////////////////////////////////////////////
$name=$email=$telephone=$message="";
///////Data Obtained in the form:////////////
if($_SERVER["REQUEST_METHOD"] == "POST") {
$name=$_POST["No Label name"];
$email=$_POST["No Label email"];
$telephone=$_POST["No Label phone"];
$message=$_POST["No Label message"];;
//////////////////////////////////////////// DATA BASE CONNECTION ///////////////////////////////////////
// Create connection with DB.
$conn = mysqli_connect($servername, $username, $password, $dbName);
//////////////////////////// Check connection ///////////////////////////////////
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql="INSERT INTO TableName (Name, Email, Phone, Menssage ,Contacted) VALUES (?, ?, ?, ?,0)";
$stmt=$conn->prepare($sql);
$stmt->bind_param("ssss", $name, $email, $telephone, $message);
$stmt->execute();
mysqli_close($conn);
}
?>
</body>
</html>
出现“无标签名称”是因为我不想更改网页的外观。请求箱 https://requestbin.com/ 向我展示了正在发送的数组的标签。
我也尝试过这个解决方案,但没有奏效:
<!doctype html>
<html>
<body>
<?php
//Data Base connection info:
$servername = "localhost";// has to be changed to the actual host
$username = "username";
$password = "password";
$dbName="dbName";//we need to add the data base name
////////////////////////////Retrieved information from form ////////////////////////////////////////////
$name=$email=$telephone=$message="";
///////Data Obtained in the form:////////////
$Rawdata = file_get_contents("php://input");
$data = json_decode($Rawdata, true);
$name=$data["No Label name"];
$email=$data["No Label email"];
$telephone=$data["No Label phone"];
$message=$data["No Label message"];
//////////////////////////////////////////// DATA BASE CONNECTION ///////////////////////////////////////
// Create connection with DB.
$conn = mysqli_connect($servername, $username, $password, $dbName);
//////////////////////////// Check connection ///////////////////////////////////
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql="INSERT INTO TableName (Name, Email, Phone, Menssage ,Contacted) VALUES (?, ?, ?, ?,0)";
$stmt=$conn->prepare($sql);
$stmt->bind_param("ssss", $name, $email, $telephone, $message);
$stmt->execute();
mysqli_close($conn);
?>
</body>
</html>
如果他们对代码或变量有任何疑问,请询问。 我还有一个问题,如果 elementors webhooks 以 json 格式或作为普通 POST 方法发送数据。 问候
【问题讨论】:
标签: php html wordpress webhooks elementor