2012-03-19 64 views
0

我正在使用PEAR DB進行基本的添加/編輯/刪除表單。我的添加和刪除功能正常工作,但我的編輯功能正在創建重複記錄,而不是編輯活動記錄。MySQL UPDATE使用梨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.'; 
    } 

?> 

回答

0

你在你的表單動作爲重點,用「模式」,但你正在尋找$_GET['action'],而不是$_GET['mode']。這是導致交換機運行的默認設置,因爲沒有$_GET['action']

+0

謝謝,這有很大的幫助!沒有完全解決它,因爲我的$ _GET和$ _POST數據中有一些其他錯誤,但這讓我走上了正確的軌道。 – deewilcox 2012-03-20 14:59:38