MySQL UPDATE正在使用Pear DB创建新记录

时间:2012-03-19 21:36:49

标签: php mysql forms pear

我正在使用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.';
    }

?>

1 个答案:

答案 0 :(得分:0)

您在表单操作中使用“模式”作为键,但您正在寻找$_GET['action']而不是$_GET['mode']。这导致您的交换机默认运行,因为没有$_GET['action']