【发布时间】:2017-07-12 19:23:24
【问题描述】:
我通常更熟悉在 SQL 或 Oracle 数据库中创建 INSERT 语句。
但是,我使用 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");
?>
【问题讨论】:
-
看起来你错过了美元
$off custzip。 -
... 还有,mysql_error的使用无效,标识符的引号错误,数据库名和表名需要单独引用,SQL中的字符串字面量需要用单引号括起来,不测试不成功执行且不检索错误消息,SQL 文本中包含潜在的不安全值,