I can't insert data. I don't know what went wrong.
Ftd2014
Ftd2014 2017-07-08 17:50:48
0
2
1793

<?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);


##?>

Ftd2014
Ftd2014

只是不想跟这世界脱轨

reply all(2)
邓琪豪

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 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 author 2017-07-12 13:59:11
洪涛

What is the execution result?


  • reply Teacher, the execution result is: "Failed, the ID inserted by the current user is: 0"
    Ftd2014 author 2017-07-09 21:09:28
  • reply 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 author 2017-07-12 14:01:11
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!