search

Home  >  Q&A  >  body text

There is a problem with thinkphp5.1 ajax updating data, please give me some advice

After using AJAX to submit the data to be updated, I want to know what data is received in the controller. Is it the data in my table? Or is it the data for judgment?


## public function update(Request $request){

$status=0;

$id = $request->get ('id');

$data = $this->request->param();

// dump($data); What is the amount of data here? Let’s take a look. It doesn’t work with dump, and it can’t be executed?

$a=Db::name('dh')->where('id',$id)->update($data);

if (!$a ) {

              $message = 'Failed to join. ';

} else {

              $status = 1;  

          session('admin', $a); Join successfully! ';

} }  

  return ['status' => $status, 'message' => $message];

}

--------------------------------------------- -------------------------------------

圐圙圐圙2370 days ago1956

reply all(3)I'll reply

  • PhpNewer

    PhpNewer2018-05-19 17:12:33

    $data is all the data of the form submitted by your front end through ajax. This is an associative array. tp5 uses json by default. Are you using dump output to verify the data in $data?

    updata() Shouldn’t the sql statement of mysql be executed here? How can I execute $data directly?

    reply
    0
  • 圐圙

    圐圙2018-05-19 14:13:21

    $name= $this->request->param('name');
    $add= $this->request->param('add');
    $time= $this->request->param('update_time');
    $a = Db::name('dh')
    ->where('id',$s)
    - >data(['name' => $name,
    'add'=>$add,
    'update_time'=>$time
    ])
    ->update( );

    Do you have to ask me to write like this? What if I have many fields to change?

    reply
    0
  • 圐圙

    Then I change it to the above, won’t it work? If there are many fields, do we have to receive them one by one and write them in?

    圐圙 · 2018-05-20 13:33:14
  • Cancelreply