dongqiyang3711 2013-05-07 23:37 采纳率: 100%
浏览 76

使用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.

  • 写回答

1条回答

  • douzhoubing2805 2013-05-08 10:07
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题