【问题标题】:$wpdb not working, even though validation comes back positive$wpdb 不工作,即使验证返回正面
【发布时间】:2013-10-17 10:41:09
【问题描述】:

我想知道是否有人可以帮助我。我正在尝试将一些数据添加到 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;

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

谢谢

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

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

    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) {
         ^
    

    【讨论】:

    • Nope 仍然没有向数据库中添加数据
    • 更改了 $result 错误类型,并出现“将练习上传到数据库时出现问题”
    • 刚刚调试,我得到: WordPress 数据库错误:[您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在“位置”、“价格”、“类型”、“收入”、“否”附近使用正确的语法。手术次数”、“任期”、“PDF”)第 1 行的 VAL'] INSERT INTO 实践(“位置”、“价格”、“类型”、“收入”、“手术次数”、“任期”、“ PDF') 值 ('Newport','56,000','nhs','876,000','1','freehold','oneguard.co.uk/wp-content/uploads/2013/10/…)
    【解决方案2】:

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

    MySQL 期望列标识符使用反引号括起来:`.

    【讨论】:

      猜你喜欢
      • 2015-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多