我怎样才能让我的下拉菜单转到一个页面并显示选定的记录


How can I get my dropdown to goto a page and display a selected record?

第一次张贴和非常新的mysqli,抱歉,如果我的问题是不正确的。我有一个表(员工)与id,员工名称和个人资料。我希望能够编辑/添加记录,从下拉菜单中选择一个工作人员,提交后,转到一个名为add-edit-staff.php的新页面。

我已经创建了页面add-edit-staff.php,它工作得很好。如果我在浏览器的地址栏中手动输入"add-edit-staff.php",则会出现添加新记录的表单。如果我手动输入"add-edit-staff.php?"Id =1"在我的浏览器地址栏中,表单出现编辑Id为1的记录。

现在到我的问题,我有一个名为view-staff.php的页面。这个页面有一个带有下拉列表的表单,显示字段"staffmember"。我希望能够从下拉菜单中选择一条记录,并根据该记录的id转到我的add-edit-staff.php页面上的相应记录。目前,当我选择我的记录,它会直接回到mt add-edit-staff.php页面。它这样做是因为view-staff.php页面没有在URL中发送记录的id。

我已经附上了我的代码,我很感激一些关于如何解决我的问题的建议。正如我所说的,我对这方面很陌生,我一直在努力调整代码以适应我的目标,但我似乎无法理解它。

Thanks in advance.

add-edit-staff.php

<?php
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';
sec_session_start();
?>
<?php
// creates the new/edit record form
function renderForm($staffmember = '', $profile ='', $error = '', $id = '')
{
?>
<form action="" method="post">
            <div>
                <?php if ($id != '') { ?><input type="hidden" name="id" value="<?php echo $id; ?>" />
                    <?php $id; ?><?php } ?>
        <table width="675" border="0" cellspacing="10">
        <tr>
          <td valign="top"><strong>Staff Member</strong></td>
          </tr>
        <tr>
        <td valign="top"><input name="staffmember" type="text" name="staffmember" size="147" value="<?php echo $staffmember; ?>"</td>
        </tr>
        <tr>
          <td><strong>Staff Profile</strong></td>
          </tr>
        <tr>
        <td><textarea name="profile" cols="110" rows="10"><?php echo $profile; ?></textarea></td>
        </tr>
        </table>
                  <p></p>
                <input type="submit" name="submit" value="Submit" />
            </div>
            </form>
</body></html>
<?php }
    /*
       EDIT RECORD
    */
// if the 'id' variable is set in the URL, we know that we need to edit a record
if (isset($_GET['id']))
{
    // if the form's submit button is clicked, we need to process the form
    if (isset($_POST['submit']))
    {
        // make sure the 'id' in the URL is valid
        if (is_numeric($_POST['id']))
        {
            // get variables from the URL/form
            $id = $_POST['id'];
            $staffmember = htmlentities($_POST['staffmember'], ENT_QUOTES);
            $profile = htmlentities($_POST['profile'], ENT_QUOTES);
            // check that staffmember and profile are both not empty
            if ($staffmember == '' || $profile == '')
            {
                // if they are empty, show an error message and display the form
                $error = 'ERROR: Please fill in all required fields!';
                renderForm($staffmember, $profile, $error, $id);
            }
            else
            {
                // if everything is fine, update the record in the database
                if ($stmt = $mysqli->prepare("UPDATE staff SET staffmember = ?, profile = ?
                    WHERE id=?"))
                {
                    $stmt->bind_param("ssi", $staffmember, $profile, $id);
                    $stmt->execute();
                    $stmt->close();
                }
                // show an error message if the query has an error
                else
                {
                    echo "ERROR: could not prepare SQL statement.";
                }
                // redirect the user once the form is updated
                header("Location: view-staff.php");
            }
        }
        // if the 'id' variable is not valid, show an error message
        else
        {
            echo "Error!";
        }
    }
    // if the form hasn't been submitted yet, get the info from the database and show the form
    else
    {
        // make sure the 'id' value is valid
        if (is_numeric($_GET['id']) && $_GET['id'] > 0)
        {
            // get 'id' from URL
            $id = $_GET['id'];
            // get the recod from the database
            if($stmt = $mysqli->prepare("SELECT * FROM staff WHERE id=?"))
            {
                $stmt->bind_param("i", $id);
                $stmt->execute();
                $stmt->bind_result($id, $staffmember, $profile);
                $stmt->fetch();
                // show the form
                renderForm($staffmember, $profile, NULL, $id);
                $stmt->close();
            }
            // show an error if the query has an error
            else
            {
                echo "Error: could not prepare SQL statement";
            }
        }
        // if the 'id' value is not valid, redirect the user back to the view-staff2.php page
        else
        {
            header("Location: view-staff.php");
        }
    }
}

    /*
       NEW RECORD
    */
// if the 'id' variable is not set in the URL, we must be creating a new record
else
{
    // if the form's submit button is clicked, we need to process the form
    if (isset($_POST['submit']))
    {
        // get the form data
        $staffmember = htmlentities($_POST['staffmember'], ENT_QUOTES);
        $profile = htmlentities($_POST['profile'], ENT_QUOTES);
        // check that staffmember and profile are both not empty
        if ($staffmember == '' || $profile == '')
        {
            // if they are empty, show an error message and display the form
            $error = 'ERROR: Please fill in all required fields!';
            renderForm($staffmember, $profile, $error);
        }
        else
        {
            // insert the new record into the database
            if ($stmt = $mysqli->prepare("INSERT staff (staffmember, profile) VALUES (?, ?)"))
            {
                $stmt->bind_param("ss", $staffmember, $profile);
                $stmt->execute();
                $stmt->close();
            }
            // show an error if the query has an error
            else
            {
                echo "ERROR: Could not prepare SQL statement.";
            }
            // redirec the user
            header("Location: view-staff2.php");
        }
    }
    // if the form hasn't been submitted yet, show the form
    else
    {
        renderForm();
    }
}
// close the mysqli connection
$mysqli->close();
?>

view-staff.php

<form name="form1" method="post" action="add-edit-staff.php">
<select name="staffmember">
  <?php
                    // get the records from the database
                    if ($result = $mysqli->query("SELECT * FROM staff ORDER BY id"))
                    {
                            // display records if there are records to display
                            if ($result->num_rows > 0)
                            {
                                   {
                                            while ($row = $result->fetch_assoc()) {
                                            echo "<option value='"{$row['id']}'">";
                                            echo $row['staffmember'];
                                            echo "</option>";
}
?>
            <input type="submit" name="Submit" value="   Edit   ">
              </form<?php
    }
    }
                            // if there are no records in the database, display an alert message
    else
    {
    echo "No results to display!";
    }
    }
                            // show an error if there is an issue with the database query
    else
    {
    echo "Error: " . $mysqli->error;
    }
                    // close database connection
    $mysqli->close();
?>
       <a href="add-edit-staff.php"><h3 align="center">Add New Record</h3></a>

      <?php else : ?>
      <p>
            <span class="error">You are not authorized to access this page.</span> Please <a href="index.php">login</a>.
        </p>
    <?php endif; ?>

修改view-staff.php

<form name="form1" method="get" action="add-edit-staff.php">
<select name="id">
  <?php
                    // get the records from the database
                    if ($result = $mysqli->query("SELECT * FROM staff ORDER BY id"))
                    {
                            // display records if there are records to display
                            if ($result->num_rows > 0)
                            {
                                   {
                                            while ($row = $result->fetch_assoc()) {
                                            echo "<option value='"{$row['id']}'">";
                                            echo $row['staffmember'];
                                            echo "</option>";
}
?>
            <input type="submit" name="Submit" value="   Edit   ">
              </form<?php
    }
    }
                            // if there are no records in the database, display an alert message
    else
    {
    echo "No results to display!";
    }
    }
                            // show an error if there is an issue with the database query
    else
    {
    echo "Error: " . $mysqli->error;
    }
                    // close database connection
    $mysqli->close();
?>
       <a href="add-edit-staff.php"><h3 align="center">Add New Record</h3></a>

      <?php else : ?>
      <p>
            <span class="error">You are not authorized to access this page.</span> Please <a href="index.php">login</a>.
        </p>
    <?php endif; ?>

和add-edit-staff.php

<?php
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';
sec_session_start();
?>
<?php
// creates the new/edit record form
function renderForm($staffmember = '', $profile ='', $error = '', $id = '')
{
?>
<form action="" method="post">
            <div>
                <?php if ($id != '') { ?><input type="hidden" name="id" value="<?php echo $id; ?>" />
                    <?php $id; ?><?php } ?>
        <table width="675" border="0" cellspacing="10">
        <tr>
          <td valign="top"><strong>Staff Member</strong></td>
          </tr>
        <tr>
        <td valign="top"><input name="staffmember" type="text" name="staffmember" size="147" value="<?php echo $staffmember; ?>"</td>
        </tr>
        <tr>
          <td><strong>Staff Profile</strong></td>
          </tr>
        <tr>
        <td><textarea name="profile" cols="110" rows="10"><?php echo $profile; ?></textarea></td>
        </tr>
        </table>
                  <p></p>
                <input type="submit" name="submit" value="Submit" />
            </div>
            </form>
</body></html>
<?php }
    /*
       EDIT RECORD
    */
// if the 'id' variable is set in the URL, we know that we need to edit a record
if (isset($_GET['id']))
{
    // if the form's submit button is clicked, we need to process the form
    if (isset($_POST['submit']))
    {
        // make sure the 'id' in the URL is valid
        if (is_numeric($_POST['id']))
        {
            // get variables from the URL/form
            $id = $_POST['id'];
            $staffmember = htmlentities($_POST['staffmember'], ENT_QUOTES);
            $profile = htmlentities($_POST['profile'], ENT_QUOTES);
            if ($stmt = $mysqli->prepare("UPDATE staff SET staffmember = ?, profile = ?
                WHERE id=?"))
            {
                $stmt->bind_param("ssi", $staffmember, $profile, $id);
                $stmt->execute();
                $stmt->close();
            }
            // show an error message if the query has an error
            else
            {
                echo "ERROR: could not prepare SQL statement.";
            }
            // redirect the user once the form is updated
            header("Location: view-staff.php");
        }
        // if the 'id' variable is not valid, show an error message
        else
        {
            echo "Error!";
        }
    }
    // if the form hasn't been submitted yet, get the info from the database and show the form
    else
    {
        // make sure the 'id' value is valid
        if (is_numeric($_GET['id']) && $_GET['id'] > 0)
        {
            // get 'id' from URL
            $id = $_GET['id'];
            // get the recod from the database
            if($stmt = $mysqli->prepare("SELECT * FROM staff WHERE id=?"))
            {
                $stmt->bind_param("i", $id);
                $stmt->execute();
                $stmt->bind_result($id, $staffmember, $profile);
                $stmt->fetch();
                // show the form
                renderForm($staffmember, $profile, NULL, $id);
                $stmt->close();
            }
            // show an error if the query has an error
            else
            {
                echo "Error: could not prepare SQL statement";
            }
        }
        // if the 'id' value is not valid, redirect the user back to the view-staff2.php page
        else
        {
            header("Location: view-staff.php");
        }
    }
}

    /*
       NEW RECORD
    */
// if the 'id' variable is not set in the URL, we must be creating a new record
else
{
    // if the form's submit button is clicked, we need to process the form
    if (isset($_POST['submit']))
    {
        // get the form data
        $staffmember = htmlentities($_POST['staffmember'], ENT_QUOTES);
        $profile = htmlentities($_POST['profile'], ENT_QUOTES);
        // check that staffmember and profile are both not empty
        if ($staffmember == '' || $profile == '')
        {
            // if they are empty, show an error message and display the form
            $error = 'ERROR: Please fill in all required fields!';
            renderForm($staffmember, $profile, $error);
        }
        else
        {
            // insert the new record into the database
            if ($stmt = $mysqli->prepare("INSERT staff (staffmember, profile) VALUES (?, ?)"))
            {
                $stmt->bind_param("ss", $staffmember, $profile);
                $stmt->execute();
                $stmt->close();
            }
            // show an error if the query has an error
            else
            {
                echo "ERROR: Could not prepare SQL statement.";
            }
            // redirec the user
            header("Location: view-staff2.php");
        }
    }
    // if the form hasn't been submitted yet, show the form
    else
    {
        renderForm();
    }
}
// close the mysqli connection
$mysqli->close();
?>