gpt4 book ai didi

php - 使用下拉菜单编辑 mysql 数据

转载 作者:行者123 更新时间:2023-11-30 00:12:08 26 4
gpt4 key购买 nike

我有一个数据输入页面,其中存在一些下拉菜单。下拉列表中选定的项目存储在我的 mysql 数据库中没有任何问题。

我制作了第二页来编辑个人记录。我可以毫无问题地在文本框中显示下拉列表中的数据。但是,我希望能够使用相同的下拉选项编辑结果。

我可以在 edit.php 页面上添加包含所有正确选项的下拉菜单,但数据库中存储的值不会出现。相反,我默认得到第一个选择,而不是存储的值。

<?php
$position_sql = "SELECT id, position FROM ref_positions ORDER BY position ASC";
$position_result = mysql_query($position_sql);
echo "<select name='position'>";
while ($row = mysql_fetch_array($position_result)) {
echo "<option value='" . $row['id'] . "'>" . $row['position'] . "</option>";
}
echo "</select>";
?>

我使用 POST 和 GET 来获取正确的记录 ID。

我的文本框工作正常,如下:

Department:<input type="text" name="department" size="20" value="<?php echo "$row[department]"; ?>">

我假设我必须构建某种 if 语句来显示存储的值?

不确定这是否有帮助,但这就是我获取要编辑的记录的 ID 的方法:

        <?php
$id= ($_GET["id"]);


$sql = "SELECT * FROM people
WHERE id='$id' LIMIT 1";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
?>

整个代码:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Form Edit Data</title>
</head>

<body>

<?php
$BASE_PATH = 'C:\xampp\htdocs\OGS';
include_once($BASE_PATH . "\includes\layouts\header.php");
?>

<div id="main">
<div id="subnavigation">
<?php include_once($BASE_PATH . "\mods\main_menu\index.html");?>
</div>

<div id="page"
<br><br>
<table border=1>
<tr>
<td align=center>Update Employee Information</td>
</tr>
<tr>
<td>
<table>
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('ogs');
?>

<?php
$id= ($_GET["id"]);


$sql = "SELECT * FROM people
WHERE id='$id' LIMIT 1";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
?>

<form method="post" action="edit_data.php">
<input type="hidden" name="id" value="<?php echo "$row[id]"; ?>">

<fieldset>
<legend><b>Name</b></legend>
First Name:<input type="text" name="first_name" size="20" value="<?php echo "$row[first_name]"; ?>">
Last Name:<input type="text" name="last_name" size="40" value="<?php echo "$row[last_name]"; ?>">
</fieldset>
<br><br>
<fieldset>
<legend><b>Contact Information</b></legend>
Town:<input type="text" name="town" size="20" value="<?php echo "$row[town]"; ?>">
Address:<input type="text" name="address" size="40" value="<?php echo "$row[address]"; ?>">
Province:<input type="text" name="province" size="20" value="<?php echo "$row[province]"; ?>">
Postal Code:<input type="text" name="postal_code" size="40" value="<?php echo "$row[postal_code]"; ?>">
<br><br>
Home Phone:<input type="text" name="home_phone" size="20" value="<?php echo "$row[home_phone]"; ?>">
Cell Phone:<input type="text" name="cell_phone" size="40" value="<?php echo "$row[cell_phone]"; ?>">
</fieldset>
<br><br>
<fieldset>
<legend><b>Emergency Contact</b></legend>
Emergency Contact Name:<input type="text" name="first_name" size="20" value="<?php echo "$row[first_name]"; ?>">
Emergency Contact Number:<input type="text" name="last_name" size="40" value="<?php echo "$row[last_name]"; ?>">
</fieldset>
<br><br>
<fieldset>
<legend><b>Work Information</b></legend>
Role:<input type="text" name="role" size="20" value="<?php echo "$row[role]"; ?>">
Employer:<input type="text" name="company_works_for" size="40" value="<?php echo "$row[company_works_for]"; ?>">
<br><br>
Department:<input type="text" name="department" size="20" value="<?php echo "$row[department]"; ?>">
Position:
<?php
$position_sql = "SELECT id, position FROM ref_positions ORDER BY position ASC";
$position_result = mysql_query($position_sql);
echo "<select name='position'>";

// You should use PHP to get the existing value here, I have made it up here as 14
$existing_id = '$row[id]';

while ($row = mysql_fetch_array($position_result))
{
// Check if the existing id is the same as the current id we are displaying
// If it is, set the selected attribute
if($existing_id == $row['id'])
echo "<option selected='selected' value='" . $row['id'] . "'>" . $row['position'] . "</option>";
else
echo "<option value='" . $row['id'] . "'>" . $row['position'] . "</option>";
}
echo "</select>";
?>

<br><br>
Is Supervisor?:
<input type="radio" name="is_supervisor" value="<?php echo "$row[is_supervisor]"; ?>"> Yes
<input type="radio" name="is_supervisor" value="<?php echo "$row[is_supervisor]"; ?>"> No
<br><br>
Is Active?:
<input type="radio" name="active_employee" value="<?php echo "$row[active_employee]"; ?>"> Yes
<input type="radio" name="active_employee" value="<?php echo "$row[active_employee]"; ?>"> No
<br><br>
Start Date:<input type="text" name="start_date" size="40" value="<?php echo "$row[start_date]"; ?>">
</fieldset>


<input type="submit"
name="submit value" value="Update">

</form>




</div>
</div>
</body>
</html>

尼古拉斯·弗隆 (Nicholas Furlong) 的记录显示他是一名健康与安全协调员。 http://prntscr.com/3o34g2

但是当我单击“编辑”并转到我的编辑页面时,他被列为控制者。 (这是本列中的第一个选项。) http://prntscr.com/3o36qu

最佳答案

不太清楚你在问什么。但我会努力的

<?php
$position_sql = "SELECT id, position FROM ref_positions ORDER BY position ASC";
$position_result = mysql_query($position_sql);
echo "<select name='position'>";

// You should use PHP to get the existing value here, I have made it up here as 14
$existing_id = 14;

while ($row = mysql_fetch_array($position_result))
{
// Check if the existing id is the same as the current id we are displaying
// If it is, set the selected attribute
if($existing_id == $row['id'])
echo "<option selected='selected' value='" . $row['id'] . "'>" . $row['position'] . "</option>";
else
echo "<option value='" . $row['id'] . "'>" . $row['position'] . "</option>";
}
echo "</select>";
?>

关于php - 使用下拉菜单编辑 mysql 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23960846/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com