0

我正在尝试创建一组协同工作的网页,以允许用户使用 PHP 查看、删除和编辑 MS Access 数据库的行。

Membership.php 显示 Access 数据库中成员的名称列表。他们的名字也是超链接,当点击时,将用户带到另一个页面 EditRecord.php,其中所有关于在 Membership.php 上点击名字的成员的信息都显示在文本框中,可以选择完全删除记录,或者只是更新某些领域。

Membership.php 和 EditRecord.php 显示如下。错误代码是我的 EditRecord.php 源代码的第 91 行,但出于隐私考虑,我从这篇文章中删除了一些内容。相反,该行已被标记为:

//--------这是错误行----------

代码

[会员资格.php]

<!DOCTYPE html>
<html lang="en">

 <head>
<meta charset="utf-8">
<link rel="stylesheet" href="Accounts.css">
<style type="text/javascript" src="Validate.js"></style>
<style type="text/javascript" scr="Redirect.js"></style>
<style type="text/javascript" src="Utilities.js"></style>
<title>Member Information Input</title>
</head>

<body>

<div id="content">
<?php
//Establish data connection using external file
require("connection.php");

//Issue SQL SELECT Statement
$sql = "SELECT * FROM Membership";

//Stores any results that match the search term.        
$rs = odbc_exec($conn, $sql);

//Set counter for search results to zero 
$results = 0;

//Iterates through search results and prints information on records that match  
while($row = odbc_fetch_array($rs))     

{
$results += 1;
echo '<p><a href="EditMember.php?ID=' . $row['ID'] . '" id="popup">' .   $row['FirstName'] . " " . $row['LastName'] . "</a></p>";


}

?>
</div>



</body>

</html>

[编辑记录.php]

<?php

//Retrieve ID value - if the page is loading for the first time, use $_GET[]. If the   
//delete or edit button has been clicked, use $_POST[]

if (isset($_GET['ID'])) {
$userID = $_GET['ID'];
}

else {
$userID=$_POST['ID'];
}

//Establish data connection
require("connection.php");

//If the Delete Button is clicked 

if (isset($_POST['DelBtn'])) {
//Issue SQL Statement to Delete Selected Record
$sqlDelete = "DELETE FROM Membership WHERE ID = $userID";

//Execute the SQL Delete Query
$rsDelete = odbc_exec($conn,$sqlDelete);

if(odbc_num_rows($rsDelete) == 1) {
    echo "Record successfully deleted!";
}
}

//If the Edit Button is clicked
else if (isset($_POST['EditBtn'])) {

//Collect form field values in scalar variables
$FirstName = $_POST['FirstName'];
$LastName = $_POST['LastName'];
$Address = $_POST['Address'];
$City = $_POST['City'];
$State = $_POST['State'];
$Email = $_POST['Email'];
$Gender = $_POST['Gender'];
$Comments = $_POST['Comments'];

//Issue SQL Statement to Update Selected Record
$sqlUpdate = "UPDATE Membership SET FirstName = '$FirstName', LastName = '$LastName', Address = '$Address', City = '$City', State = '$State'" . 
"Email='$Email', Gender = '$Gender', Comments = '$Comments'  WHERE ID = $userID";

//Execute the SQL UPDATE Query
$rsEdit = odbc_exec($conn,$sqlUpdate);

if(odbc_num_rows($rsEdit) == 1) {
    echo "Record successfully updated!";
}
}

//Issue SQL SELECT Statement to Select Record to Edit or Delete

$sql = "SELECT * FROM Membership WHERE ID = $userID";

//Execute the SQL Query

$rs = odbc_exec($conn, $sql);
odbc_close($conn);

?>

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<link rel="stylesheet" href="Accounts.css">
<style type="text/javascript" src="Validate.js"></style>
<style type="text/javascript" src="Utilities.js"></style>
<title>Member Information Input</title>
</head>

<body>

<div id="content">
    <form method="post" action="EditMember.php" name="EditForm">
        <?php
        // Loop through and display the recordset returned by SELECT statement. Display the record values in HTML Text Boxes
                    **//--------This is the error line----------
        while ($row = odbc_fetch_array($rs)) {
        ?>**

        First Name: <input type="text" name="FirstName" value="<?php echo $row['FirstName']?>"><br>
        Last Name:  <input type="text" name="LastName" value="<?php echo $row['LastName']?>"><br>
        Address:    <input type="text" name="Address" value="<?php echo $row['Address']?>"><br>
        City:       <input type="text" name="Telephone" value="<?php echo $row['City']?>"><br>
        State:      <input type="text" name="Telephone" value="<?php echo $row['State']?>"><br>
        Email:      <input type="text" name="Email" value="<?php echo $row['Email']?>"><br>
        Gender:     <input type="text" name="Telephone" value="<?php echo $row['Gender']?>"><br>
        Comments:   <input type="text" name="Comments" value="<?php echo $row['Comments']?>"><br><br>
                    <input type="hidden" name="ID" value="<?php echo $row['ID']?>" >

        <?php
        }
        ?>

        <input type="submit" name="EditBtn" value="Edit Record">&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name="DelBtn" value="Delete Record">

    </form>



</div>

<div id="footer">
    <?php require("Footer.php"); ?>
</div>

</body>

</html>

我也觉得这很奇怪,因为我的数据库中有五条记录,而不是四条。那是因为它从零开始计数吗?

任何见解或建议将不胜感激。

4

1 回答 1

0

您的问题是您odbc_close()在循环调用之前调用并关闭了连接odbc_fetch_array()。在获取所有行之前,您需要保持连接打开。

此外,错误消息中的“4”不是指行数或类似的东西;它只是odbc_exec()调用创建的资源的结果标识符的数字表示。

于 2013-05-08T10:07:52.627 回答