mysql - Insert the mysqli_insert_id inside separate table using PHP -
i'd submit new registered user auto_increment id inside table1 , @ same time insert mysqli_insert_id or last_insert_id ever want call inside table2. can echo id out, need inside table2. how can accomplish that? please help!
here code reference:
<?php if(isset($_post['submit'])) { require 'connect.php'; $sql = "insert table1 (username, password) values ('".$_post["username"]."','".$_post["password"]."')"; if (mysqli_query($con, $sql)) { $last_id = mysqli_insert_id($con); echo "new record created successfully. last inserted id is: " . $last_id; } else { echo "error: " . $sql . "<br>" . mysqli_error($con); } mysqli_close($con); } ?>
use variable last inserted id (successful) query insert other table.
this rough sketch don't know db schema second table , quite unclear, need (fill in) rest.
if (mysqli_query($con, $sql)) { $last_id = mysqli_insert_id($con); echo "new record created successfully. last inserted id is: " . $last_id; $sql = mysqli_query($con, "insert table2 (col) values ('$last_id')") or die(mysqli_error($con)); }
passwords
i noticed may storing passwords in plain text. not recommended.
use 1 of following:
- crypt_blowfish
crypt()
bcrypt()
scrypt()
- on openwall
- pbkdf2
- pbkdf2 on php.net
- php 5.5's
password_hash()
function. - compatibility pack (if php < 5.5) https://github.com/ircmaxell/password_compat/
other links:
important sidenote column length:
if , when decide use password_hash()
or compatibility pack (if php < 5.5) https://github.com/ircmaxell/password_compat/, important note if present password column's length lower 60, need changed (or higher). manual suggests length of 255.
you need alter column's length , start on new hash in order take effect. otherwise, mysql fail silently.
Comments
Post a Comment