簡體   English   中英

語法錯誤或訪問沖突:1064您在alter table中的SQL語法中有錯誤

[英]Syntax error or access violation: 1064 You have an error in your SQL syntax in alter table

我試圖通過laravel stament函數改變auto_increment並且它給出了錯誤。

顯示完整的錯誤消息是SQLSTATE [42000]:語法錯誤或訪問沖突:1064您的SQL語法中有錯誤; 檢查與您的MariaDB服務器版本對應的手冊,以便在'?'附近使用正確的語法 在第1行(SQL:alter table bills AUTO_INCREMENT = 9)

if($quantity[$i]>$qtny)
{
    Bills::where('id','=',$invoice_no)->delete();
    DB::enableQueryLog();
        DB::statement('alter table `bills` AUTO_INCREMENT=?',array('invoice_no'=>$invoice_no));
    dd(DB::getQueryLog());
    //return "<script>alert('Quantity is not available for product ".$item_name[$i]."');location.href='/create';</script>";
}

各地都不允許使用可變占位符

在聲明中,? 可以將字符用作參數標記,以指示稍后在執行時將數據值綁定到查詢的位置。

alter table在這里需要一個常量,例如你不能使用alter table bills AUTO_INCREMENT = (select max(id) from bills)這樣的東西alter table bills AUTO_INCREMENT = (select max(id) from bills) 通常,在任何地方都可以執行此操作,您可以使用占位符(有一些例外情況,例如limit ,但在文檔中會提到這些)。

所以在這種情況下你必須直接使用這個值:

DB::statement('alter table `bills` AUTO_INCREMENT = '.$invoice_no)

此處不允許使用可變占位符。 所以。 我這樣做了,

DB::statement('alter table `bills` AUTO_INCREMENT = '.$invoice_no);

您可以用兩種不同的方式編寫laravel查詢,以便對變量進行標記

1)第一種方法

if($quantity[$i]>$qtny)
{
    Bills::where('id','=',$invoice_no)->delete();
    DB::enableQueryLog();
        DB::statement('alter table `bills` AUTO_INCREMENT=:invoice_no',array('invoice_no'=>$invoice_no));
    dd(DB::getQueryLog());
    //return "<script>alert('Quantity is not available for product ".$item_name[$i]."');location.href='/create';</script>";
}

2)第二種方法

if($quantity[$i]>$qtny)
{
    Bills::where('id','=',$invoice_no)->delete();
    DB::enableQueryLog();
        DB::statement('alter table `bills` AUTO_INCREMENT=?',[$invoice_no]);
    dd(DB::getQueryLog());
    //return "<script>alert('Quantity is not available for product ".$item_name[$i]."');location.href='/create';</script>";
}

您也可以在下面編寫查詢,但您的查詢將為sql注入打開。

DB::statement('alter table bills AUTO_INCREMENT = '.$invoice_no);

暫無
暫無

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

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