Home  >  Q&A  >  body text

I can't insert data. I don't know what went wrong.

<?php


// Determine duplicate passwords

if(trim($_POST['pwd']) != trim($ _POST['rpwd'])){

exit('The two passwords are inconsistent, please return to the previous page');

}


// Ready to write data

$username = trim($_POST['username']); // Visible data

$password = md5($_POST[' pwd']); // Visible data, md5 is a way to encrypt passwords


$time = time(); // Invisible data, returns unix timestamp , the user's registration time

$ip = $_SERVER['REMOTE_ADDR']; // Invisible data, returns the IP address, the user's registered IP, we can use ip2long to convert it to integer storage


// Connect to the database server, determine errors, select the database and set the character set

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


if(mysqli_errno($conn)){

echo mysqli_error($conn);

exit;

}


##mysqli_select_db($conn, 'user');

mysqli_set_charset($conn, 'utf8');


// Combined SQL statement

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


// Send statement and determine status

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


if($result){

echo 'success' . "<br />";

}else{

echo 'failure' . "<br />";

}


// Use mysqli_insert_id() to print out the auto-incremented primary key ID

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


// Close the database connection

mysqli_close($conn);


##?>

Ftd2014Ftd20142684 days ago1801

reply all(2)I'll reply

  • 一个点线面

    一个点线面2017-07-11 14:50:17

    There is nothing wrong with the PHP code. You can print out whether the data submitted in the form exists, and then test whether the $result value exists to find errors. Also, is your database table really set to have an ID auto-increment? Anyway, I succeeded, haha

    Run result:

    微信截图_20170711144846.png

    reply
    0
  • Ftd2014

    I... finally... found... the error point, in $sql = "insert into user(username, password, createtime, createip) values('" . $username ."', '" . $password . "', '" . $time . "', '" . $time . "', '" . $ip . "')"; This sentence contains an extra '" . $time . "' , I guess I was dozing off and wrote this more...Thank you!

    Ftd2014 · 2017-07-12 13:59:11
  • PHP中文网

    PHP中文网2017-07-09 12:46:33

    What is the execution result?


    reply
    0
  • Ftd2014

    Teacher, the execution result is: "Failed, the ID inserted by the current user is: 0"

    Ftd2014 · 2017-07-09 21:09:28
    Ftd2014

    Teacher, I found the error point, a low-level error, in $sql = "insert into user(username, password, createtime, createip) values('" . $username ."', '" . $password . "', '" . $time . "', '" . $time . "', '" . $ip . "')"; This sentence contains an extra '" . $time . "' , thank you teacher!

    Ftd2014 · 2017-07-12 14:01:11
  • Cancelreply