簡體   English   中英

無法使用准備好的語句插入日期

[英]Cannot INSERT date with prepared statement

我正在嘗試對給定月份/年份的每一天進行多行INSERT。

如果循環僅運行一次,則一切正常。 如果運行兩次或更多次,則會引發以下錯誤:

SQLSTATE [22018]:轉換規范的字符值無效:206 [Microsoft] [ODBC SQL Server驅動程序] [SQL Server]操作符類型沖突:文本與日期不兼容(SQLExecute [206]位於ext \\ pdo_odbc \\ odbc_stmt.c:260 )

如果我不准備日期,它也可以使用。 我茫然...我做錯什么了嗎? 這是我的代碼:

$query = "INSERT INTO scp.schedule
    (record_create_date, associate_id, scheduled_date, shift_id, is_weekend)
    VALUES ";

for ($day_count = 1; $day_count <= $days_month; $day_count++)
{
    $day_count = sprintf("%02d", $day_count);

    $full_date = $year . "-" . $month . "-" . $day_count;

    // Check if date is weekend
    $weekend = (date("N", strtotime($full_date)) < 6) ? 0 : 1;

    $query .= "(GETDATE(), ?, ?, ?, ?), ";
    $params[] = $associate["associate_id"];
    $params[] = $full_date;
    $params[] = $shifts[0]["shift_id"];
    $params[] = $weekend;
}

// Remove the last comma and space
$query = substr($query, 0, -2);

$stmt = $db->prepare($query);
$stmt->execute($params);

我正在使用SQL Server 12+版本和PHP 7+版本。 數據庫中的列類型為DATE。

編輯:

這有效

INSERT INTO scp.schedule 
    (record_create_date, associate_id, 
     scheduled_date, shift_id, is_weekend) 
VALUES (GETDATE(), ?, ?, ?, ?)

這不

INSERT INTO scp.schedule 
    (record_create_date, associate_id, 
     scheduled_date, shift_id, is_weekend) 
VALUES (GETDATE(), ?, ?, ?, ?), 
       (GETDATE(), ?, ?, ?, ?)

編輯2:

一些測試以及它們引發的錯誤。 我也嘗試使用'?'

$query .= "(GETDATE(), ?, DATEFROMPARTS(?, ?, ?), ?, ?), ";
// Also tried intval() and casting to int
$params[] = $year;
$params[] = $month;
$params[] = $day_count;

SQLSTATE [22018]:轉換規范的字符值無效:206 [Microsoft] [ODBC SQL Server驅動程序] [SQL Server]操作數沖突:文本與int不兼容(SQLExecute [206]位於ext \\ pdo_odbc \\ odbc_stmt.c:260 )

$query .= "(GETDATE(), ?, CONVERT(date, ?), ?, ?), ";

SQLSTATE [22018]:強制轉換規范的字符值無效:529 [Microsoft] [ODBC SQL Server驅動程序] [SQL Server]不允許從數據類型文本到日期的明確轉換。 (位於ext \\ pdo_odbc \\ odbc_stmt.c:260的SQLExecute [529])

您可以嘗試避免使用DATEFROMPARTS()CONVERT() T-SQL函數將字符串作為SQL Server中日期字段的值傳遞。

<?
$query = "INSERT INTO scp.schedule
    (record_create_date, associate_id, scheduled_date, shift_id, is_weekend)
    VALUES ";

for ($day_count = 1; $day_count <= $days_month; $day_count++) {
    $day       = $day_count;
    $day_count = sprintf("%02d", $day_count);
    $full_date = $year . "-" . $month . "-" . $day_count;

    // Check if date is weekend
    $weekend = (date("N", strtotime($full_date)) < 6) ? 0 : 1;

    # Using DATETIMEFROMPARTS
    $query .= "(GETDATE(), ?, DATEFROMPARTS(?, ?, ?), ?, ?), ";
    $params[] = $associate["associate_id"];
    $params[] = $year;
    $params[] = $month;
    $params[] = $day;
    $params[] = $shifts[0]["shift_id"];
    $params[] = $weekend;

    # Using CONVERT
    /*
    $query .= "(GETDATE(), ?, CONVERT(date, ?), ?, ?), ";
    $params[] = $associate["associate_id"];
    $params[] = $full_date;
    $params[] = $shifts[0]["shift_id"];
    $params[] = $weekend;
    */
}

// Remove the last comma and space
$query = substr($query, 0, -2);
$stmt = $db->prepare($query);
$stmt->execute($params);

?>

如果不必在一條語句中插入整天,則可以考慮准備一次語句並在每次迭代中執行它。 然后,如果發生錯誤,您將知道導致問題的值。

嘗試使用trim代替substr

$query = trim(trim($query), ',');

內部trim將刪除space ,外部trim將刪除,

暫無
暫無

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

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