1

这是我的代码,我想更新数据库字段但面临错误。我打印查询数据发布是正确的,但为什么 ID=0 我在 db 表中有大写 ID,但为什么请回答我想通过 ajax 更新的问题。1UPDATE formSET st_name= 'hello', st_class= 'update', st_address= 'SystemConfig' WHERE ID= 0 因为 id 为 0 这就是它不更新数据的原因,但为什么。?

这是我的 ajx 函数

   <script>
  $(document).ready(function() {

  $(".Update").fancybox({
    'transitionIn'  :   'fade',
    'transitionOut' :   'fade',
    'speedIn'       :   600, 
    'speedOut'      :   200, 
    'overlayShow'   :   false
});
var Id = $(this).attr("id");
var parent = $(this).parent();  
  $("#updateform").bind("submit", function() {
     $.ajax({
        type        : "POST",
        cache       : false,
        url     : "http://localhost/testproj/index.php/form/update",
        data        : $(this).serializeArray(),
            success:function(data){
                $.fancybox(data);
            }
    });


    return false;
  });

  });     

 **VIEW**



 </head>
<body>          




     <form id="Form" action="" method="post"
     name="Form" onsubmit="return submitForm();"  ;>
    <br></br>
    <table id = "fixedme" border ='0' cellpadding='0' cellspacing='0'>
    <tr>
    <td>Message:  </td>
<td><input id='Name' name='Name' type='text' size='20' maxlength='50'/></td>


                        <td>Action</td>
        <td><select name="action" id="action">
            <option value="">--Select--</option>
        <option value="insert">Insert</option>
       <option value="update">Update</option>
        <option value="delete">Delete</option></select></td>


                        <td>Action By</td>
            <td><select name="actionBy" id="actionBy">
            <option value="">--Select--</option>
        <option value="Transaction">Transaction</option>
        <option value="ManageOperation">Manage Operation</option>
        <option value="FeeRate">Fee Rate</option>
        <option value="SystemConfig">System Config</option>
        <option value="Compliance">Compliance</option>
        <option value="Usermanagement">User Management</option>
        <option value="OnlineSenderAdmin">Online Sender Admin</option>
        </select></td>
            </tr>
   <tr><td collspan='2'><input type="submit" name="submit" id="submit" value="submit" class="submit"/></td></tr></table>
        </form>
</div>


  <div style="display:none;" id = "updateDialog"  >
    <form id="updateform" action="" method="post">
    <p><h1 align="center">Update</h1></p>
        <p>
           <label>Message:</label>
           <input type="text" id="name" name="name" />
        </p>

        <p>
           <label>Action On:</label>
          <select name="action" id="action">
            <option value="">--Select--</option>
            <option value="insert">Insert</option>
            <option value="update">Update</option>
            <option value="delete">Delete</option></select>
        </p>
        <p>
           <label>ActionBy:</label>
           <select name="actionBy" id="actionBy">
    <option value="">--Select--</option>
    <option value="Transaction">Transaction</option>
    <option value="ManageOperation">Manage Operation</option>
    <option value="FeeRate">Fee Rate</option>
    <option value="SystemConfig">System Config</option>
    <option value="Compliance">Compliance</option>
    <option value="Usermanagement">User Management</option>
    <option value="OnlineSenderAdmin">Online Sender Admin</option>
    </select>
        </p>
        <p><input type="submit" name="save" 
             id="save" value="save" class="save"/>

        <input type="submit" name="Close" 
        id="Close" value="Close" class="submit"/>       
        </form>
  </div>


<div id="message">
<form id="myForm" action="" method="" name="myForm" class="myForm"><br></br>
<table id = "fixedme" border ='1' cellpadding='0' cellspacing='0'>
    <tr>
                        <th>ID:  </th>
                        <th>Message: </th>
                        <th>Action On:</th>
                        <th>ActionBy </th>
                        <th>Actions </th>

    </tr>
            <tbody>
            <?php
    if($result!="")
    {
    foreach($result as $rows){ ?>
    <tr id="ide"><td><span><?php echo $ID=$rows->ID;?></span></td> 
    <td><span><?php echo $name=$rows->st_name;?></span></td>
    <td><span><?php  echo $action=$rows->st_address; ?></span></td>
    <td><span><?php echo  $actionby=$rows->st_class; ?> </span></td>
    <td><a href="#"  class="delete" id="<?php echo $ID=$rows->ID;?>" 
        name="delete" >Delete</a>
    <a href="#updateDialog"  class="Update" id="<?php echo $ID=$rows->ID ;?>" 
    name="Update">Update</a>
    </td></tr>
            <?php }}?>

            </tbody>
            </table>
            </form>
            </div>
            </div>
        </div>
</body>
 </html>



 **MODEL**

     public function submit_updated_data($id,$data) 
        {
            $this->load->database();
            $this->db->where('ID', $id);
            $query2 =$this->db->update( 'form', $data);

        }



**CONTROLLER**


  public function update()
 {
        $id=$this->input->post('ID');
        $name=$this->input->post('name');
        $action= $this->input->post('action');  
        $actionBy = $this->input->post('actionBy'); 
        $data = array(

                    'st_name' => $name,
                    'st_class' =>$action,
'st_address'=>$actionBy                             
                    );
    $this->form_model->submit_updated_data($id,$data);
                            $this->index();

}

1UPDATE `form` SET `st_name` = 'hello', `st_class` = 'update', `st_address` = 'SystemConfig' WHERE `ID` = 0
4

1 回答 1

1

我认为这是因为您没有提交要在 $_POST 数组中更新的记录的 ID。

提交的所有数据都是从您的表单输入构建的数组,但您没有一个用于“id”的数组......所以:

$id=$this->input->post('ID');

...没有从表格中得到任何东西。

于 2012-12-19T21:08:47.257 回答