我通常更熟悉在SQL或Oracle数据库中创建INSERT语句。如何将记录插入我的自定义WordPress表格中?
但是,我使用phpMyAdmin在WordPress中创建了一个名为CustomerDB和自定义表的自定义数据库。
我正在尝试使用以下代码将记录插入到自定义表中。
我没有收到任何错误,但没有记录插入。
请注意,在这一点上,我不关心sql注入攻击。
我会处理一旦我的INSERT代码工作。
有谁知道为什么下面的代码是不插入记录到WordPress的表中?
<?php
//make database accessible
global $wpdb;
// Get data
$customername = $_POST["customername"];
$custaddress = $_POST["custaddress"];
$custcity = $_POST["custcity"];
$custstate = $_POST["custstate"];
$custzip = $_POST["custzip"];
// Database connection
$conn = mysqli_connect("localhost","myuser","mypass","CustomerDB");
if(!$conn) {
die('Problem in database connection: ' . mysql_error());
}
// Data insertion into database
$query = "INSERT INTO ‘CustomerDB.wp_customTable’ (‘customername’, ‘custaddress’, ‘custcity’, ‘custstate’,‘custzip’) VALUES ($customername, $custaddress, $custcity, $custstate,custzip)";
mysqli_query($conn, $query);
// Redirection to the success page
header("Location: thankyou.php");
?>
看起来你错过了美元'''关闭custzip。 – thodic
...另外,mysql_error的错误用法,标识符,数据库名称和表名称的错误引号需要单独引用,SQL中的字符串文字需要用单引号括起来,不要测试不成功的执行和不检索错误消息,包含在SQL文本中的潜在的不安全值, – spencer7593