search

Home  >  Q&A  >  body text

There is no problem with the code, why is it failing? "The ID inserted by the current user is 0"

<?php

if (trim($_POST['password']) != trim($_POST['repassword'])) {

exit('twice The password is inconsistent, please return to the previous page');

}

$username = trim($_POST['username']);

$password = md5(trim ($_POST['password']));

$time = time();

$ip = $_SERVER['REMOTE_ADDR'];

$conn = mysqli_connect('localhost', 'root', 'root');

//If there is an error, there is an error number
if (mysqli_errno($conn)) {

echo mysqli_error ($conn);

exit;
}

mysqli_select_db($conn, 'text');

mysqli_set_charset($conn, 'utf8');

$sql = "insert into user(username,password,createtime,createip) values('" . $username . "','" . $password . "','" . $time . "', '" . $ip . "')";

$result = mysqli_query($conn, $sql);

if ($result) {
echo 'success';
} else {
echo 'failed';

}

echo 'The ID inserted by the current user is' . mysqli_insert_id($conn);

mysqli_close( $conn);

?>

phpcn_u46746phpcn_u467462635 days ago1414

reply all(2)I'll reply

  • 小崔

    小崔2018-03-23 10:50:51

    Check if there is already that record in the database, it cannot be repeated

    reply
    0
  • 路过

    路过2017-10-23 13:10:43

    1. Whether the sql statement corresponds to the database field

    2. Field issues in the sql statement

    $ sql = "insert into user(`username`,`password`,`createtime`,`createip`) values ​​('" . $username . "','" . $password . "','" . $time . " ','" . $ip . "')";

    reply
    1
  • Cancelreply