0

我有 3 个下拉框,效果很好。我可以从 3 个不同的表中选择值并通过各种表 ID 提交/保存到汇总表。但是,我还希望能够在相同的下拉字段中编辑这些值。我有一个编辑函数,它将汇总表 ID 发送到 $_GET 函数以识别行。这些值当前显示在 3 个文本框中,但我希望它们填充到相同的下拉字段中。我无法使选项语法正确。我想我需要在某个地方有一个“选定”选项。我只希望更新 proj_hours 列,但仍然希望所有 3 个下拉列表都填充值。

我希望这是有道理的?我想摆脱文本框。

最新代码:

<?php

    // Connect to server and select database.
    $link = mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");

    //grabs ID from the initial edit submit 
//grabs ID from the initial edit submit 
    if(isset($_GET['id']))
echo "<pre>Value of \$_GET:</br>";print_r($_GET);echo"</pre>";  
    {
        $summary_id = $_GET['summary_id'];
        $res = mysql_query("SELECT *
                    FROM summary S
                    JOIN projects P ON S.proj_id = P.proj_id
                    JOIN departments D ON D.dept_id = S.dept_id
                    JOIN hours H ON H.hours_id = S.hours_id
                    WHERE S.summary_id = '$_GET[id]'
                    ORDER BY D.dept_id
                    LIMIT 0 , 30");

        $rows = mysql_fetch_array($res);

//echo "<pre>Value of \$rows:</br>";print_r($rows);echo"</pre>";  //was using to check what's going on 
        }

//submits the edited values, does a join to find out what's what, and inserts the correct digits into the summary DB        
    if (isset($_POST['btnsubmit']))  
echo "<pre>Value of \$_POST:</br>";print_r($_POST);echo"</pre>";
    {
//echo "<pre>Value of \$_POST:</br>";print_r($_POST);echo"</pre>";
        $summary_id     = $_POST['summary_id'];     
        $nclarity_id    = $_POST['nclarity_id'];
        $nproj_hours    = $_POST['nproj_hours'];
        $ndept_name     = $_POST['ndept_name'];

        $sql1           = "UPDATE summary S
                            SET S.hours_id = (
                            SELECT H.hours_id  
                            FROM Hours H 
                            WHERE H.proj_hours = '$nproj_hours' )
                            WHERE S.summary_id = '$summary_id'" 
                            or die ("couldn't update".mysql_error());

//multiple queries to get the field edited - i know this can be done more efficiently and safely, but I'm not proficient enough                         
        $query1 = mysql_query($sql1);
        if ($query1)
{
echo "success_1!";
}
else
            {
        die('error inserting new record_1'.mysql_error());
    } // end of the nested if statement

}

 if (isset($_POST['btnnew']))  

    {
    //echo "<pre>Value of \$_POST:</br>";print_r($_POST);echo"</pre>";

        $nclarity_id    = $_POST['nclarity_id'];
        $nproj_hours    = $_POST['nproj_hours'];
        $ndept_name     = $_POST['ndept_name'];
        $proj_id        = $_POST['nclarity_id'];
        $hours_id       = $_POST['nproj_hours'];
        $dept_id        = $_POST['ndept_name'];

        $sql        = "INSERT INTO `summary` VALUES (null,'$proj_id','$hours_id','$dept_id',NOW(),null)" 
                            or die ("couldn't update".mysql_error());
        $query = mysql_query($sql);

        if ($query)
{
echo "success_2!";
}
else
            {
        die('error inserting new record_2'.mysql_error());
    } // end of the nested if statement
}

    $res = mysql_query("SELECT *
                    FROM summary S
                    JOIN projects P ON S.proj_id = P.proj_id
                    JOIN departments D ON D.dept_id = S.dept_id
                    JOIN hours H ON H.hours_id = S.hours_id
                    WHERE P.proj_status = 'open'
                    ORDER BY D.dept_id
                                        LIMIT 0 , 30"); 

?>

<table width="500" border="0" cellspacing="1" cellpadding="0">
   <tr>
   <td>
   <table width="100%" border="0" cellspacing="1" cellpadding="0">
   <tr>
   <td>&nbsp;</td>
   <td colspan="3"><strong>Update data in mysql</strong> </td>
   </tr>
   <tr>
   <td align="center">&nbsp;</td>
   <td align="center">&nbsp;</td>
   <td align="center">&nbsp;</td>
   </tr>
   <tr>
   <td align="center">&nbsp;</td>
   <td align="center"><strong>Clarity ID</strong></td>
   <td align="center"><strong>Hours</strong></td>
   <td align="center"><strong>Department</strong></td>
   </tr>
   </table>
   </td>
   </form>
   </tr>
   </table>

<table width="500" border="0" cellspacing="1" cellpadding="0">

<form method="post" action="index.php">
<tr>
<td>&nbsp;</td>

<select name="nclarity_id">
<option name="">
<?php
$sql = "SELECT proj_id, clarity_id FROM projects WHERE proj_status='open'";

$rs = mysql_query($sql);

$id = -9999; // a value that cannot exist in your database
if(isset($_GET['proj_id'])) { // check if there is an id in the $_GET variable
    $id = $_GET['proj_id']; // if so, set the $id variable
}
while($row = mysql_fetch_array($rs))
{
    $selectedText = "";
    if($row['proj_id'] == $id) { // if this row id is the same as the post id
        $selectedText = ' selected="selected"'; // set some text to be included in the markup
    }
    echo "<option value=\"".$row['proj_id']."\"".$selectedText.">".$row['clarity_id']."</option>\n  ";
}
?>
</select>

<select name="nproj_hours">
<option name="">
<?php
$sql = "SELECT hours_id, proj_hours FROM hours";
$rs = mysql_query($sql);
$id = -9999; // a value that cannot exist in your database
if(isset($_GET['hours_id'])) { // check if there is an id in the $_GET variable
    $id = $_GET['hours_id']; // if so, set the $id variable
}
while($row = mysql_fetch_array($rs))
{
    $selectedText = "";
    if($row['hours_id'] == $id) { // if this row id is the same as the post id
        $selectedText = ' selected="selected"'; // set some text to be included in the markup
    }
    echo "<option value=\"".$row['hours_id']."\"".$selectedText.">".$row['proj_hours']."</option>\n  ";

}
?>
</select>
<select name="ndept_name">
<option name="">
<?php
$sql = "SELECT dept_id, dept_name FROM departments";
$rs = mysql_query($sql);
$id = -9999; // a value that cannot exist in your database
if(isset($_GET['dept_id'])) { // check if there is an id in the $_GET variable
    $id = $_GET['dept_id']; // if so, set the $id variable
}
while($row = mysql_fetch_array($rs))
{
    $selectedText = "";
    if($row['hours_id'] == $id) { // if this row id is the same as the post id
        $selectedText = ' selected="selected"'; // set some text to be included in the markup
    }
    echo "<option value=\"".$row['dept_id']."\"".$selectedText.">".$row['dept_name']."</option>\n  ";

}
?>
</select>
<td align="center">
<input type="submit" name="btnnew" value="Enter New Record">
<input type="submit" name="btnsubmit" value="Save Edited Record">
</td>
</tr>
</form>
</table>

<table width="700" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="700" border="1" cellspacing="0" cellpadding="3">
<tr>
<td colspan="5"><strong>Project Information </strong> </td>
</tr>

<tr>
<tr>
<td align="center"><strong>Clarity ID</strong></td>
<td align="center"><strong>Estimated Hours</strong></td>
<td align="center"><strong>Department</strong></td>
<td align="center"><strong>Date Submitted</strong></td>
<td align="center"><strong></strong></td>
</tr>

<?php
while($row=mysql_fetch_array($res))
{

?>

<tr>
<td align="center"><?php echo $row['clarity_id']; ?></td>
<td align="center"><?php echo $row['proj_hours']; ?></td>
<td align="center"><?php echo $row['dept_name']; ?></td>
<td align="center"><?php echo $row['date_entered']; ?></td>
<td align="center"><a href="index.php?id=<?php echo $row['summary_id']; ?>">Edit</a></td>
</tr>

<?php
}

?>

</table>
</td>
</tr>
</table>


<?php
mysql_close();
?>
4

1 回答 1

0

您可以构建相同的选项列表,但检查 id 是否与您在 $_POST 或 $_GET 数组中找到的 id 相同。

编辑:刚刚意识到您指定了 $_GET - 在下面的示例中将 $_POST 替换为 $_GET 。

就像是:

$id = -9999; // a value that cannot exist in your database
if(isset($_POST['proj_id'])) { // check if there is an id in the $_POST variable
    $id = $_POST['proj_id']; // if so, set the $id variable
}
while($row = mysql_fetch_array($rs))
{
    $selectedText = "";
    if($row['proj_id'] == $id) { // if this row id is the same as the post id
        $selectedText = ' selected="selected"'; // set some text to be included in the markup
    }
    echo "<option value=\"".$row['proj_id']."\"".$selectedText.">".$row['clarity_id']."</option>\n  ";

}

希望这可以帮助。

编辑回答评论:

嗨,迈克,我再次检查了您的代码,$_POST$_GET变量有点混淆。$_POST提交时,您可以从表单中找到变量。$_GET变量来自 URL 。当您单击“编辑”链接时,您将重定向到 index.php,并且您正在传递一个名为“id”的变量。此时,您的$_GET数组将包含一个键/值对,称为“id”及其值。然而,我注意到在您的代码中,您正试图从$_GET数组中获取一个名为“summary_id”的变量,该变量可能不存在。此外,在构建每个选项列表之前,您正在寻找$_GET['proj_id'],$_GET['hours_id']$_GET['dept_id'],这又不存在了。总之,在“编辑”页面上,您正在搜索未在 URL 中传递的变量。这可能是您的下拉列表没有被填充的原因。一种解决方案是更改您的“编辑”链接,以便您也传递proj_id,dept_idhours_id变量。另外,请记住 $_POST 数组仅在提交表单时填充,并且仅可用于表单元素的“action”属性中指定的页面。一旦您离开该页面(即使您导航到同一页面,就像您使用“编辑”链接所做的那样),$_POST 数组也不再填充。

于 2012-10-10T12:21:13.910 回答