$ wpdb无效,即使验证恢复正常

时间:2013-10-17 10:41:10

标签: php wordpress

我想知道是否有人可以帮助我。我正在尝试将一些数据添加到wordpress数据库中的表中。我正在使用$ wpdb类,每次运行代码时,我得到的验证都会恢复正常。但是当我检查数据库中的表时,它是空的。

我的代码:

    // Define global $wpdb
    global $wpdb;

    // Retrieve information from the form
    $location = $_POST['txtLocation'];
    $price = $_POST['txtPrice'];
    $type = $_POST['sltType'];
    $revenue = $_POST['txtRevenue'];
    $surgeries = $_POST['txtSurgeries'];
    $tenure = $_POST['sltTenure'];
    $upload = $_POST['txtUpload'];

    // SQL statement to insert information from the form into the database
    $sql = $wpdb->prepare("INSERT INTO practices ('Location', 'Price', 'Type', 'Revenue', 'No. of Surgeries', 'Tenure', 'PDF') VALUES (%s,%s,%s,%s,%s,%s,%s", $location, $price, $type, $revenue, $surgeries, $tenure, $upload);

    $result = $wpdb->query($sql);

    if (!result) {
        echo '<p class="sql-error">There was a problem uploading the practice to the database</p>';
    } else {
        echo '<p class="sql-success">The practice was uploaded to the database</p>';
    }

    echo $wpdb->last_query;

有人知道为什么我的代码会这样做吗?

由于

2 个答案:

答案 0 :(得分:0)

在查询中忘记结束)。 这里

VALUES (%s,%s,%s,%s,%s,%s,%s 
                            ^

尝试:

 $sql = $wpdb->prepare("INSERT INTO practices ('Location', 'Price', 'Type', 'Revenue', 'No. of Surgeries', 'Tenure', 'PDF') VALUES (%s,%s,%s,%s,%s,%s,%s)", $location, $price, $type, $revenue, $surgeries, $tenure, $upload);

也是

if (!$result) {
     ^

答案 1 :(得分:0)

除了按照建议关闭查询中的VALUES语句之外,您可能会发现使用'引用列名也会给您带来问题。

MySQL期望使用反引号来封闭列标识符:`。

相关问题