我是新來php
。運行兩個完全不同的一個腳本內部SQLI查詢
我有這個頁面:
<?php
function renderForm($id, $StaffFullName, $StaffJobPosition, $error)
{
?>
<!doctype html>
<html>
<head><title></title></head>
<body>
<?php
// if there are any errors, display them
if ($error != '')
{
echo '<div>'.$error.'</div>';
}
?>
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<div>
<p>ID: <?php echo $id; ?></p>
Name: * <input type="text" name="StaffFullName" value="<?php echo $StaffFullName; ?>"/><br/>
Job Position: * <select name="JobPosition">
<?php
$query = "SELECT * FROM LUT_JOBPOS";
$result = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($result)){
if ($StaffJobPosition == $row['JobposID'])
{
echo "<option value='{$row['JobposID']}' selected='selected'>{$row['JobposTitle']}</option>";
}
else {
echo "<option value='{$row['JobposID']}'>{$row['JobposTitle']}</option>";
}
}
$result->close();
?>
</select><br/>
<input type="submit" name="submit" value="Update">
<input type="button" onClick="parent.location='view.php'" value="Back">
</div>
</form>
</body>
</html>
<?php
}
// connect to the database
require_once('../../authenticate.php');
// check if the form has been submitted. If it has, process the form and save it to the database
if (isset($_POST['submit']))
{
// do some funky stuff
}
else
// if the form hasn't been submitted, get the data from the db and display the form
{
// get the 'id' value from the URL (if it exists), making sure that it is valid (checking that it is numeric/larger than 0)
if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
{
// query db
$id = $_GET['id'];
$query = "SELECT * FROM STAFF WHERE StaffID=$id";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_assoc($result);
$result->close();
// check that the 'id' matches up with a row in the database
if($row)
{
// get data
$StaffFullName = $row['StaffFullName'];
$StaffJobPosition = $row['StaffJobPosition'];
// show form
renderForm($id, $StaffFullName, $StaffJobPosition, '');
}
else
// if no match, display result
{
echo "No results!";
}
}
else
// if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error
{
echo 'Error!';
}
}
?>
所以,這裏所發生的是:
當您打開像edit.php?id=1
的頁面,它從STAFF
表獲取相關記錄的數據,並將其顯示在頁面上供用戶編輯它們。 這部分代碼正常工作。 我還希望用戶能夠從下拉框中選擇「作業位置」可能的值。下拉框應從數據庫中的另一個表中獲取其數據,LUT_JOBPOS
。 這是不起作用的代碼的一部分。
我之前使用此頁面上mysql_query
命令,它完美地工作。不過,我被告知打開mysqli_query
。 由於我做了轉換,我找不到如何在同一個腳本上運行這兩個查詢。 我搞砸了一點require_once
命令,並根據我稱之爲我可以運行一個查詢或另一個,但從來都沒有。
看着我的虛擬主機,我可以看到,可能是有關我的問題的唯一的事情的記錄是:
「mod_fcgid:標準錯誤:PHP公告:未定義的變量:連接在/ var/WWW上線24" /vhosts/myhostdomain.com/httpdocs/prod15/admin/staff/edit.php
連接可變來自authenticate.php
並將其保持的連接參數到數據庫中。我確定它已經設置,否則第一個查詢(獲取用戶數據)將不起作用。
我讀的地方,你不能在同一個腳本運行兩個sqli
查詢。 那麼我應該如何使用LUT
表(查找表)?
PS:我知道,用於顯示數據,我可以用一個UNION
,這就是我做什麼。 但是當我編輯數據時,我希望用戶只能從LUT
表中存在的可能值中選擇(下拉選擇框)
任何幫助?
你想解決幾個問題,你可以分解它們,以便我們可以更好地理解。 –
這只是一個問題:第二個'sqli_query'(從LUT_JOBPOS表中讀取數據以便將它們添加到選擇框中的那個)不會執行。我只是儘可能多地提供信息。 – PhantomDj