簡體   English   中英

MySQL UPDATE正在使用Pear DB創建新記錄

[英]MySQL UPDATE is Creating New Record with Pear DB

我正在使用PEAR DB處理基本的添加/編輯/刪除表單。 我的添加和刪除功能正常運行,但是我的編輯功能正在創建重復記錄,而不是編輯活動記錄。

我已經搜索了論壇,MySQL手冊和php.net,但是那里沒有太多的文檔。 我正在工作的站點很舊,而且我了解使用Pear DB的人很少,因為像CodeIgniter這樣的框架現在已經成為了發展之路。 相信我,我會盡可能切換此站點。 就是說,任何幫助解決此問題將不勝感激!

這是我的代碼:

<?php 

require 'controller.php';
global $dbc;
$member_id = $_GET['memberid'];
$aid = $_GET['aid'];

/* Handle Accounting Actions -- Add, Edit, Delete */

$action = isset( $_GET['action'] ) ? $_GET['action'] : "";

switch ($action) {

  case 'add':
    add_aid();
    break;

  case 'edit':
    edit_aid();
    break;

  case 'delete':
    delete_aid();
    break;

  default:
    add_aid();
    break;
}


function add_aid()
{
    global $dbc;
    $member_id = $_GET['memberid'];
    $aid = $_GET['aid'];

?>


    <form name="add accounting record" action="?module=members&amp;mode=edit" method="post">        

    <input type="hidden" id="member_id" name="member_id" value="<?=$member_id?>" />

        <table style="border: 1px solid #000;" align="center">
            <tr style="background-color: #525f3b;">
                <th style="background-color: #525f3b;color: #b5d67a;text-transform: uppercase; align:center; font-family: Arial, Helvetica, sans-serif;" colspan="5" >Accounting</th>
            </tr>

            <tr style="background-color:#b5d67a; color:#525f3b;text-transform: uppercase; font-family: Arial, Helvetica, sans-serif; font-size:12px;">
                <td>Actions</td><td>Year</td><td>Dues Pd. (Date)</td><td>Amount Pd.</td><td>Attended<br>Conference<br>(Date)</td>
            </tr>

            <tr style="background-color:#b5d67a; border-right: 1px solid #000; color:#525f3b;text-transform: uppercase; font-family: Arial, Helvetica, sans-serif;">
                <td align="center">

                    <a href="?module=memberss&amp;mode=edit&amp;aid='<?php print $data['aid'];?>" title="Edit"><img src="images/icons/icon-edit.png" class="inline_icon" alt="Edit" /></a> <a onclick="return window.confirm(\'Are you sure you want to delete this record?\');" href="?module=members&amp;action=delete&amp;aid=<?php print $data[0];?>" title="Delete"><img src="images/icons/icon-delete.png" class="inline_icon" alt="Delete" /></a>

                </td>

                <td>
                    <input type="text" id="year" name="year" value="<?=$_POST['year']?>" size="10" maxlength="4" />
                </td>

                <td>
                    <input type="text" id="dues_paid_date" value="<?=$_POST['dues_paid_date']?>" name="dues_paid_date" size="20" maxlength="10"  />
                </td>

                <td>
                    <input type="text" id="amount_paid" value="<?=$_POST['amount_paid']?>" name="amount_paid" size="20" maxlength="16" />
                </td>

                <td>
                    <input type="text" id="attended_conf" value="<?=$_POST['attended_conf']?>" name="attended_conf" size="20" maxlength="10" />
                </td>

            </tr>

        </table>
        <div style="float:right;">
            <input class="form_button" style="float:right;" type="submit" value="CLOSE WINDOW" onclick="window.opener.location.href = window.opener.location.href; window.close();">
        </div>
        <div style="float:right;">
            <input class="form_button" type="submit" value="SAVE DATA" onclick="this.form.submit();" />
        </div
    </form>

</div>
<?php

    if (isset($_POST['year']) &&
        isset($_POST['dues_paid_date']) &&
        isset($_POST['amount_paid']) &&
        isset($_POST['attended_conf']))

        {
            $member_id = (int)$_POST['member_id'];
            $year = ($_POST['year']);
            $dues = ($_POST['dues_paid_date']);
            $amount = ($_POST['amount_paid']);
            $conference = ($_POST['attended_conf']);

        $res =& $dbc->query("INSERT INTO accounting (member_id,year, dues_paid_date, amount_paid, attended_conf) VALUES ('".$member_id."','".$year."','".$dues."','".$amount."','".$conference."')",null,null);
        }
}

function edit_aid()
{
    global $dbc;
    $member_id = $_GET['memberid'];
    $aid = $_GET['aid'];

    $dbQuery = "SELECT *
                FROM accounting
                WHERE member_id='".$member_id."'
                AND aid='".$aid."'
                LIMIT 0,1";
    $dbResult = $dbc->query( $dbQuery,__FILE__,__LINE__ );
    $adata = $dbResult->fetchRow( DB_FETCHMODE_ASSOC );

    $year = ($adata['year']);
    $dues = ($adata['dues_paid_date']);
    $amount = ($adata['amount_paid']);
    $conference = ($data['attended_conf']);

    ?>

    <form name="edit contact" action="?module=members&amp;mode=edit" method="post">     

        <input type="hidden" id="member_id" name="member_id" value="<?=$member_id?>" />

        <input type="hidden" id="aid" name="aid" value="<?=$aid?>" />
        <?php //echo $member_id; ?>

            <label value="<?=$adata['Year']?>">Year</label>
                <input type="text" id="year" name="year" value="<?=$adata['year'];?>" size="20" maxlength="20" />

            <label value="<?=$adata['dues_paid_date']?>">Dues Paid Date</label>
                <input type="text" id="dues_paid_date" name="dues_paid_date" value="<?=$adata['dues_paid_date']?>" size="20" maxlength="20"  />
            <label value="<?=$adata['amount_paid']?>">Amount Paid</label>
                <input type="text" id="amount_paid" name="amount_paid" value="<?=$adata['amount_paid']?>" size="45" maxlength="60" />
            <label value="<?=$adata['attended_conf']?>">Attended Conference</label>
                <input type="text" id="attended_conf" name="attended_conf" value="<?=$adata['attended_conf']?>" size="30" maxlength="30" />

            <div style="float:right;">
                <input class="form_button" type="submit" value="SAVE DATA" onclick="this.form.submit();" />
            </div
        </form>

    <?php
    $dbQuery = "UPDATE accounting
        SET member_id='".$member_id."', year='".$year."', dues_paid_date='".$dues."', amount_paid='".$amount."', attended_conf='".$conference."' 
            WHERE member_id='".$member_id."'
            AND aid='".$aid."'";
    $dbc->query( $dbQuery,__FILE__,__LINE__ );

    $_SESSION['msg'] = 'The contact was successfully updated.';
    }


function delete_aid()
{
    global $dbc;
    $member_id = $_GET['memberid'];
    $aid = $_GET['aid'];

    $dbQuery = "SELECT *
                FROM accounting
                WHERE member_id='".$member_id."'
                AND aid='".$aid."'
                LIMIT 0,1";
    $dbResult = $dbc->query( $dbQuery,__FILE__,__LINE__ );
    $adata = $dbResult->fetchRow( DB_FETCHMODE_ASSOC );

    ?>

    /* Echo the data, confirm we want to delete */

<input class="form_button" style="float:right;" type="submit" value="REMOVE RECORD" onclick="window.opener.location.href = window.opener.location.href; window.close();">
            </div>
    <?php
    $dbQuery = "DELETE FROM accounting
                WHERE member_id='".$member_id."'
                AND aid='".$aid."'";
    $dbc->query( $dbQuery,__FILE__,__LINE__ );

    $_SESSION['msg'] = 'The contact was successfully deleted.';
    }

?>

您在表單操作中使用“模式”作為鍵,但您正在尋找$_GET['action']而不是$_GET['mode'] 由於沒有$_GET['action']這將導致您的交換機運行默認值

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM