使用PHP从Access数据库中提取数据PHP警告:odbc_fetch_array():4不是第91行的EditRecord.php中的有效ODBC结果资源

I'm trying to create a set of webpages that work together to allow users to view, delete, and edit rows of a MS Access database using PHP.

Membership.php shows a list of the names of members in the Access database. Their names are also hyperlinks that, when clicked, take users to another page EditRecord.php where all of information on the member whose name was clicked on Membership.php is displayed in text boxes with the option to completely delete the record, or just update certain fields.

Membership.php and EditRecord.php are displayed below. The error code is for line 91 of my source for EditRecord.php, but I cut some things out of this post for privacy. Instead, the line has been marked like so:

//--------This is the error line----------

code

[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>

I also find this strange, because there are five records in my database, not four. Is that because it starts counting at zero?

Any insight or advice would be greatly appreciated.

douyong8801
douyong8801 您的删除和更新部分是否有效?
7 年多之前 回复

1个回答



您的问题是您正在调用 odbc_close()</ code>并在循环调用之前关闭连接 odbc_fetch_array()</代码>。 在获取所有行之前,您需要保持连接处于打开状态。</ p>

此外,错误消息中的“4”不会引用多行或类似行 那; 它只是 odbc_exec()</ code>调用创建的资源的结果标识符的数字表示。</ p>
</ div>

展开原文

原文

Your problem is that you are calling odbc_close() and closing the connection before your loop calls odbc_fetch_array(). You need to leave the connection open until after you've fetched all of the rows.

Also, the "4" in the error message does not refer to a number of rows or anything like that; it's just the numeric representation of result identifier for the resource created by the odbc_exec() call.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐