doutan5337 2019-07-04 10:15
浏览 67
已采纳

php codeigniter不要将变量更新为mysql

I have a problem with my PHP project with CodeIgniter.

I have a form in which I pass variables to the Database and execute an UPDATE Query by modifying the data on the Database.

The problem is that when I insert the new data it does not give me error, it shows me the view in which it shows the data of the table but the data are not modified.

Can anyone help me understand please?

MODELS Hello_Model.php

function displayrecordsById($id)
 {
 $query=$this->db->query("SELECT * FROM Todolist WHERE id='".$id."'");
 return $query->result();
 }

 function updaterecords($testo,$stato,$id)
 {
     $this->db->query("update Todolist SET testo='$testo',stato='$stato' WHERE id='".$id."'");

 } 

Views update_records.php

<html>
<head>
<title>Registration form</title>
</head>

<body>
 <?php
  //$i=1;
  foreach($data as $row)
  {
  ?>
    <form method="post">
        <table width="600" border="1" cellspacing="5" cellpadding="5">
  <tr>
    <td width="230">Id </td>
    <td width="329"><input type="text" name="id" value="<?php echo $row->id; ?>"/></td>
  </tr>
  <tr>
    <td>Testo </td>
    <td><input type="text" name="testo" value="<?php echo $row->testo; ?>"/></td>
  </tr>
  <tr>
    <td>Stato</td>
    <td><input type='text' name='stato' value= "<?php echo $row->stato; ?> "/></td>
  </tr>
  <tr>
    <td colspan="2" align="center">
    <input type="submit" name="update" value="Update Records"/></td>
  </tr>
</table>
    </form>
    <?php } ?>
</body>
</html>
</html>

Controllers Hello.php

public function updatedata()
 {
 $id=$this->input->get('id');
 $result['data']=$this->Hello_Model->displayrecordsById($id);
 $this->load->view('update_records',$result); 

 if($this->input->post('update'))
 {
 $n=$this->input->post('testo');
 $e=$this->input->post('stato');
 $this->Hello_Model->updaterecords($id,$n,$e);
 redirect("http://simone.fabriziolerose.it/index.php/Hello/dispdata");
        echo "Success!";

 }
 }

I took the code from this tutorial

  • 写回答

1条回答 默认 最新

  • douzhan5058 2019-07-04 10:24
    关注

    i think problem in calling model function check:

    change this:

    $this->Hello_Model->updaterecords($id,$n,$e);
    

    To

     $this->Hello_Model->updaterecords($n,$e,$id);
    

    and you change your custom query to CI query like:

    For Update

    $this->db->where("id",$id);
    $this->db->update("Todolist",array("testo"=>$testo,"stato"=>$stato));
    

    For Select:

    $this->db->where("id",$id);
    $query=$this->db->get("Todolist");
    $row=$query->row();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗