簡體   English   中英

解析MySQL准備語句

[英]Parsing MySQL prepare statement

我正在嘗試創建常規的插入/更新功能,但在解析語句時遇到錯誤。

首先,我創建更新並插入字符串

$update = '$wpdb->prepare("UPDATE '. $dbTable. ' SET Distance = %d WHERE Date = %s AND UserID = %d", '. $distance. ', '. $today. ', '. $userID. ')';
$insert = '$wpdb->prepare("INSERT INTO '. $dbTable. ' (Distance, Date, UserID) VALUES (%d,%s,%d)", '. $distance. ', '. $today. ', '. $userID. ')';

當我回聲時,它看起來還不錯:

$wpdb->prepare("INSERT INTO hfwp_Balance_Arm_Reach (Distance, Date, UserID) VALUES (%d,%s,%d)", 50, 2018-01-13, 29)

但是,當我將其傳遞給函數時,會出現語法錯誤。

function hfwp_SaveFormData($update, $insert, $userID, $today, $dbTable)
{
  global $wpdb;
  $wpdb->show_errors();

  $sql = $wpdb->prepare("SELECT COUNT(*) UserID FROM $dbTable WHERE Date = %s AND UserID = %d", $today, $userID );
  $count = $wpdb->get_var($sql);

  if ($count > 0)
  {
    if (strpos($update, '$wpdb->prepare(' ) === 0) {

        $wpdb->query($update);

        if ($wpdb->last_error != "") {
            $error = $wpdb->last_error;
            hfwp_Log_Errors($update, $userID, $today, $error);
        }
    }

  }else{
    if (strpos($insert, '$wpdb->prepare(' ) === 0) {

        $wpdb->query($insert);

        if ($wpdb->last_error != "") {
            $error = $wpdb->last_error;
            hfwp_Log_Errors($insert, $userID, $today, $error);
        }
    }
}

}

錯誤:

<div id="error"><p class="wpdberror"><strong>WordPress databasefejl:</strong> [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 

&#039;$wpdb-&gt;prepare(&quot;INSERT INTO hfwp_Balance_Arm_Reach (Distance, Date, UserID) VALU&#039; at line 1]<br /><code>$wpdb-&gt;prepare(&quot;INSERT INTO hfwp_Balance_Arm_Reach (Distance, Date, UserID) VALUES (%d,%s,%d)&quot;, 50, 2018-01-13, 29)</code></p></div>

<div id="error"><p class="wpdberror"><strong>WordPress databasefejl:</strong> [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near &#039;$wpdb-&gt;prepare(&quot;INSERT INTO hfwp_Balance_Arm_Reach (Distance, Date, UserID) VALU&#039; at line 1]<br /><code></code></p></div>

無法解析包含prepare“ $ wpdb-> prepare”的SQL語句,還是我的語句中缺少某些內容?

在更新中刪除''並插入

$update = $wpdb->prepare("UPDATE '. $dbTable. ' SET Distance = %d WHERE Date = %s AND UserID = %d, '. $distance. ', '. $today. ', '. $userID. '");
$insert =$wpdb->prepare("INSERT INTO '. $dbTable. ' (Distance, Date, UserID) VALUES (%d,%s,%d)", '. $distance. ', '. $today. ', '. $userID.'");

並像這樣$update->execute()

您收到語法錯誤,因為$update包含未在此處的代碼中查詢的字符串

$wpdb->query($update);

$ update包含'$wpdb->prepare("UPDATE '. $dbTable. ' SET Distance = %d WHERE Date = %s AND UserID = %d", '. $distance. ', '. $today. ', '. $userID. ')'; \\

echen您打印$update ,它將打印您的prepare語句的更新值。

例如$R='priot()'; 你覺得這會執行,也不會$R包含值即priot()不是THA返回值priot()

在這里學習

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM