Home  >  Q&A  >  body text

This question is a bit strange? It seems that the time and IP cannot be obtained

/*$sql = "insert into kui3(username,password,createtime,createip)values('$username','$password',222,3333)";*/

Use the above code to change the underlined part, and you can successfully write


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

Use The code in the course, that is, the code above, always fails to write. I don’t know what the problem is?

$邂♥逅♥愛♥~$邂♥逅♥愛♥~2352 days ago1371

reply all(6)I'll reply

  • 寻觅 beyond

    寻觅 beyond2018-02-09 14:33:59

    Then when inserting, enclose the time in quotation marks, because the time is stored in the form of a string, both $time and ip in your question should be enclosed in parentheses

    reply
    0
  • 寻觅 beyond

    寻觅 beyond2018-02-09 14:32:29

    `createtime` int(80) DEFAULT NULL, isn’t this int(80) a bit scary? And you can use the date type of the database

    reply
    0
  • 卿立平

    卿立平2018-02-08 20:44:59

    Do we need to create a table in the database first? That is to say, the table statement must be executed first before the connection can be successful:

    CREATE TABLE `kui3` (

    `id` int(32) NOT NULL AUTO_INCREMENT,

    `username` varchar(30) DEFAULT NULL,

    `password` varchar(32) DEFAULT NULL,

    `createtime` int(80) DEFAULT NULL,

    `createip` varchar( 60) DEFAULT NULL,

    PRIMARY KEY (`id`)

    ) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8 |

    Yes?

    reply
    0
  • PHP中文网

    PHP中文网2018-02-06 12:25:47

    String concatenation of SQL statements is incorrect

    reply
    0
  • $邂♥逅♥愛♥~

    $邂♥逅♥愛♥~2018-02-05 23:20:45

    Success code:

    The table statement is as follows:

    CREATE TABLE `kui3` (

    `id` int(32) NOT NULL AUTO_INCREMENT ,

    `username` varchar(30) DEFAULT NULL,

    `password` varchar(32) DEFAULT NULL,

    `createtime` int(80) DEFAULT NULL,

    `createip` varchar(60) DEFAULT NULL,

    PRIMARY KEY (`id`)

    ) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8 |


    The complete code of the connect.php file is as follows:

    ##<?php

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

    exit('The two passwords are 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, 'kui');

    mysqli_set_charset($conn, 'utf8');

    $sql = "insert into kui3(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);

    ?>

    reply
    1
  • $邂♥逅♥愛♥~

    $邂♥逅♥愛♥~2018-02-05 23:14:22

    CREATE TABLE IF NOT EXISTS user (

    id int(11) NOT NULL,

    username varchar(30) NOT NULL,

    password char( 32) NOT NULL,

    createtime int(11) NOT NULL,

    createip int(11) DEFAULT NULL

    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;


    There are two questions here:

    The first one should be, password varchar(32) not null;

    The second and last createip type should be set to string,


    The transfer is OK after modification


    The ID inserted successfully by the current user is 13


    reply
    1
  • Cancelreply