How handle errors when inserting data? |
Error dump:
mysqli_sql_exception #1062 Duplicate entry 'john' for key 'users.username' A part of my code: Code: $dataUser = [
Here is my example, if you have any questions, feel free to ask. https://pastebin.com/et677dB5
Hope it will help a little bit. (07-25-2021, 09:02 PM)brabus Wrote: Here is my example, if you have any questions, feel free to ask. https://pastebin.com/et677dB5helped, but it would help more if it were possible to do this with an if, I just read in the documentation that the save or insert methods return false when they fail
use a try catch block
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
use validation, for your case: duplicated username entry
use this rules: 'username' => 'required||is_unique[users.username]' // usename already exist in db or 'username' => 'is_not_unique[users.username]' // username not exist in db give validation error if username already exist or not
If has any error but you have set validation rule in model
If(! $model-save($data)){ $model->errors; }
Enlightenment Is Freedom
|
Welcome Guest, Not a member yet? Register Sign In |