我正在使用HTTP查詢字符串來傳遞一個id。我將它分配給的變量完全適用於緊隨其後的查詢。然而,它並不在我在同一個文件中定義的任何函數中工作,儘管我將變量聲明爲全局函數。聲明爲全局變量的PHP變量在函數內不起作用
$circleID = $_GET['id'];
$circleID
很適合這個查詢:
// Retrieve circle data
$circleDataResult = mysqli_query($connection," SELECT name, description
FROM circle
WHERE circleID = '$circleID' ");
$circleData = mysqli_fetch_array($circleDataResult);
$circleName = $circleData['name'];
$circleDesc = $circleData['description'];
它沒有下面的函數內的工作,雖然。 $circleID
似乎在這種情況下是空的:
if(isset($_POST['action']) && !empty($_POST['action'])) {
$action = $_POST['action'];
switch($action) {
case 'removeUser' : removeUser(); break;
case 'makeAdmin' : makeAdmin(); break;
case 'revokeAdmin' : revokeAdmin(); break;
case 'makeOwner' : makeOwner(); break;
}
}
function removeUser(){
global $connection;
global $circleID;
$thisUserID = $_POST['id'];
$removeUserFromCircle = " DELETE
FROM circle_participants
WHERE circleID = '$circleID' AND userID = '$thisUserID' ";
if (mysqli_query($connection, $removeUserFromCircle)) {
echo "You removed " . getName($thisUserID) . " from this circle";
} else {
echo "Error deleting record: " . mysqli_error($connection);
}
}
道歉,如果這是一個很小的問題。我對PHP很陌生,花了相當多的時間來解決這個問題,但我被卡住了。
你確定你選擇了正確的桌子?這是什麼形式? –
你應該準備和綁定並使用'$ _GET ['id']'(這是一個超級全局):http://php.net/manual/en/mysqli-stmt.bind-param.php – AbraCadaver
是的,如果我硬編碼circleID值,查詢起作用。 – waldbaum