Codeigniter更新数据库字段错误

时间:2012-12-17 15:45:47

标签: ajax jquery codeigniter-2

这里是我的代码我想更新数据库字段,但面临error.i打印查询数据发布是正确的但为什么ID = 0我在db表中有资本ID,但为什么请回答我是什么问题我想更新通过ajax。 1UPDATE form SET 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

1 个答案:

答案 0 :(得分:1)

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

提交的所有数据都是根据表单输入构建的数组,但您没有'id'的数据......所以:

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

...没有从表格中获得任何内容。