[英]PHP/SQL Insert data from form and insert data from another table using prepared statement
我正在嘗試在正在創建的網站(想想ebay)上創建銷售,用戶輸入所有輸入詳細信息以創建可銷售的商品,單擊按鈕,然后將所有信息插入db表“ sellingitems”使用准備好的語句,現在的問題是我試圖從表(用戶)中獲取用戶位置(州和郊區)的詳細信息,並使用准備好的語句將其插入,並使用SaleState將其全部放入“ sellingitems”中, Sellingitems表中的SaleSuburb,我得到的錯誤是
“致命錯誤:語句失敗!您的SQL語法有誤;請查看與您的MySQL服務器版本相對應的手冊,以獲取在'INSERT INTO sellingitems(ItemID,UserID,CatID,ItemName,ItemDesc,ItemAmount, '在“
我希望這是清楚且可以理解的,我已經研究了許多小時,並且無法通過join或其他方法在INSERT上找到任何東西來幫助我,請問我是否需要澄清一下,並在此先感謝您的幫助!
$stmt = mysqli_prepare($conn, "INSERT INTO sellingitems (ItemID, UserID, CatID, ItemName, ItemDesc, ItemAmount, TimeFrame, ItemCond, Postage, Returns, SaleState, SaleSuburb)
SELECT State, Suburb FROM user WHERE UserID = '$UID'
VALUES ('',?,'',?,?,?, CURRENT_TIMESTAMP,?,?,?,State,Suburb)");
if ($stmt === false) {
trigger_error('Statement failed! ' . htmlspecialchars(mysqli_error($conn)), E_USER_ERROR);
}
$bind = mysqli_stmt_bind_param($stmt, 'issdsssss', $UID, $ItemName, $ItemDesc, $Amount, $ItemCond, $Postage, $Returns);
if ($bind === false) {
trigger_error('Bind param failed!', E_USER_ERROR);
}
$exec = mysqli_stmt_execute($stmt);
if ($exec === false) {
trigger_error('Statement execute failed! ' . htmlspecialchars(mysqli_stmt_error($stmt)), E_USER_ERROR);
}
if ($stmt == false) {
$response = "Sorry something went wrong. : ";
print_r($response);
print_r($stmt->mysqli_error);
print_r($stmt->error);
return;
} else {
$response = "Sale Created.";
print_r($response);
}
這不是INSERT SELECT的正確語法。 請參閱文檔 。
嘗試以下方法:
$stmt = mysqli_prepare($conn, "INSERT INTO sellingitems (ItemID, UserID, CatID, ItemName, ItemDesc, ItemAmount, TimeFrame, ItemCond, Postage, Returns, SaleState, SaleSuburb)
SELECT '',?,'',?,?,?, NOW(),?,?,?,State,Suburb FROM user WHERE UserID = '$UID'");
最好還是將兩個語句分開,如下所示:
$result = mysqli_query($conn, "SELECT State, Suburb FROM user WHERE UserID = '$UID'");
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$stmt = mysqli_prepare(
$conn,
"INSERT INTO sellingitems (
ItemID,
UserID,
CatID,
ItemName,
ItemDesc,
ItemAmount,
TimeFrame,
ItemCond,
Postage,
Returns,
SaleState,
SaleSuburb)
VALUES
('', ?, '', ?, ?, ?, NOW(), ?, ?, ?, '{$row['State']}', '{$row['Suburb']}')"
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.