警告:odbc_fetch_array(): 4在第91行EditRecord.php中不是有效的ODBC结果资源


Using PHP to pull data from Access Database PHP Warning: odbc_fetch_array(): 4 is not a valid ODBC result resource in EditRecord.php on line 91

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

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

Membership.php和EditRecord.php如下所示。错误代码是我的EditRecord.php源代码的第91行,但我为了隐私从这篇文章中删去了一些东西。相反,该行被标记为:

//-------- 这是一个错误 ----------

[Membership.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>
[EditRecord.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>

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

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

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