1
這裏是我的相關代碼:如何在SQL查詢中使用會話變量?
<?php
require("common.php");
$userID = $_SESSION['user']['id'];
echo $userID;
if(!empty($_POST))
{
if(empty($_POST['bio']))
{
die("Please enter your Bio.");
}
if(empty($_POST['location']))
{
die("Please enter your location");
}
$query = 'UPDATE users
SET usertype = 1
WHERE id="$userID"';
$query_params = array(
':bio' => $_POST['bio'],
':location' => $_POST['location'],
);
try
{
$stmt = $db->prepare($query);
$result = $stmt->execute();
}
catch(PDOException $ex)
{
die("Failed to run query: " . $ex->getMessage());
}
// header("Location: myprofile.php");
}
?>
我想外地用戶類型更新爲1上提交一個表單,其中ID匹配當前會話的是,作爲變量$用戶ID設置的。代碼運行時,它不提供錯誤,但在查詢內部使用PHP變量似乎不起作用,該字段不會更新。
我試過不同的引號,並且我可以在整個多個if語句中訪問變量,但是不在查詢中。另外,我正在使用PDO。任何幫助表示讚賞!
謝謝你,這是唯一的問題! –