catfan / Medoo

The lightweight PHP database framework to accelerate the development.
https://medoo.in
MIT License
4.83k stars 1.15k forks source link

insert always return 0 and there is no error message #411

Closed newjie closed 6 years ago

newjie commented 8 years ago

I just started to use medoo to make my database learning live easier. My table has 5 fields:Primary ID,varchar Unique userid,varchar date,int status,varchar mark. I am able to do 'SELECT', so there is problem with the connection, but when I do insert:

$db = new medoo([...]);

  function insertRecord($database,$userid,$date,$status,$mark=NULL){
    return $database->insert('atdc',[
      "userid" => $userid,
      "date" => $date,
      "status" => $status,
      "mark" => $mark
    ]);
  }

  $r = insertRecord($db,'das','2016-06-12',0,'dad');

  var_dump($r);
  var_dump($db->error());

$r is always 0(if data inserted, it should be the last id), no matter what data I use. But $db->error() returns 00000, which I believe means no error. The data is not inserted, also no error message in PHP error log. I can't figure out what is possibly wrong, please help.

catfan commented 8 years ago

Check out the userid is the primary key with int type and auto-increase.

newjie commented 8 years ago

Do you mean you want me to check whether userid is the primary key and auto increases? No it is not. "id" is.

catfan commented 8 years ago
  1. Check your id is set to primary and auto-incease
  2. Check the column is null possible
newjie commented 8 years ago

yes they are

rakshazi commented 8 years ago

@catfan same problem.

rakshazi commented 8 years ago

@catfan @newjie problem has been solved! Just add following lines to your database config:

'option' => [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        ],
rbdoer commented 8 years ago

same problem,and i read the medoo code;the insert function : $this->exec('INSERT INTO ' . $this->table_quote($table) . ' (' . implode(', ', $columns) . ') VALUES (' . implode($values, ', ') . ')');//this return 1

$lastId[] = $this->pdo->lastInsertId();//but if table has no auto-increment primary key,it returns 0

flippolis commented 7 years ago

If a table has no autoincrement fields, what is the correct way to check for errors after an insert?

repat commented 7 years ago

@flippolis Check out this post http://stackoverflow.com/a/40404306/2517690

coder-liyang commented 7 years ago

Same problem.

poncianodiego commented 7 years ago

For me is returning the string but not updating or retriving data from db

e.i I see: INSERT INTO "items" ("stock_number", "position") VALUES ('value1', 'val')

with debuggin on but there are no new inputs

I've tried setting the error options and I've put insert within a try catch and nothing.

catfan commented 6 years ago

insert is now return PDOStatement object. You can now check out more info about this.

https://medoo.in/api/insert

pwwiur commented 6 years ago

i had the same problem, and got that my mysql has been updated and it does not accept "tableName" for it's syntax. and also for column names.