0

i have database with this tables

users 
  user_id 
  user_name 
  user_email
  user_password 
  user_reg_date 

and i have second table user_tokens

user_tokens 
  token_id 
  token_user_id
  token_user_agent 
  toke_hash 
  token_created 
  token_expires 

foreighn key token_user_id is references to user_id

php code here

    $sql = 'INSERT INTO user_tokens 
            (token_user_id, token_user_agent, token_hash, token_created, token_expires) 
            VALUES (:userid, :uagent, :thash, :tcurrent, :texpires)';

            $ua = serialize($visitor->get_userspecs());
            $time = intval(time());

            $binder = array(
                ':userid'           => $userid,
                ':uagent'           => serialize($visitor->get_userspecs()),
                ':thash'            => $userhash,
                ':tcurrent'         => time(),
                ':texpires'         => strtotime('+2 Days')
            );

            $this->preAction($sql, $binder);

            if(!$this->doAction()) {

                return null;
            }

php request is using PDO

    $this->stmt = $this
                ->get_con()
                ->prepare($this->sql);

            if (!$this->stmt) 
                throw new RuntimeException('SQL preparing failure! '.$stmt->errorCode());

            if (!empty($this->prepared)) {

                foreach ($this->prepared as $key => $value) {

                    $this->stmt->bindParam($key, $value);
                }
            } 

            $check = $this->stmt->execute();

i as i know when i printing $binder -> via print_r $user_id shows 4 this is same as in users -> user_id i got

when i try execute sql i got error

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`modulecms`.`user_tokens`, CONSTRAINT `user_tokens_ibfk_1` FOREIGN KEY (`token_user_id`) REFERENCES `users` (`user_id`))

in log i got this

  2020-01-06T18:50:47.015764Z      26 Prepare   SELECT token_hash as token 
                FROM user_tokens WHERE token_user_id = ? LIMIT 1
2020-01-06T18:50:47.015803Z    26 Close stmt    
2020-01-06T18:50:47.015826Z    26 Execute   SELECT token_hash as token 
                FROM user_tokens WHERE token_user_id = '4' LIMIT 1
2020-01-06T18:50:47.015987Z    26 Close stmt    
2020-01-06T18:50:47.016396Z    26 Prepare   INSERT INTO user_tokens 
            (token_user_id, token_user_agent, token_hash, token_created, token_expires) 
            VALUES (?, ?, ?, ?, ?)
2020-01-06T18:50:47.016467Z    26 Execute   INSERT INTO user_tokens 
            (token_user_id, token_user_agent, token_hash, token_created, token_expires) 
            VALUES ('1578509447', '1578509447', '1578509447', '1578509447', '1578509447')
2020-01-06T18:50:47.017541Z    26 Close stmt

what's my problem? why i have this all placeholders re-writed by timestamp?

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    Does user ID `1578509447` exist? That looks ike a timestamp instead of a user id... – aynber Jan 06 '20 at 18:56
  • 2
    You need to use `bindValue`, not `bindParam`. The latter binds to a reference, so you're setting all the columns to the last `$value`. – Barmar Jan 06 '20 at 18:58
  • You don't even need to use that loop. Just do `$this->stmt->execute($this->prepared);` – Barmar Jan 06 '20 at 18:59
  • to aynber: it rewrites by timestime userid must be 4 and in array in binder variable it is, but after execution it all params rewrites with timestamp, i'dont know why, sorry for my english – Mixmax Makster Jan 06 '20 at 19:01

0 Answers0