简体   繁体   English

PHP的; mysql;如何插入user_id或date_created?

[英]php; mysql;How to insert user_id or date_created?

I made a simple registration form. 我做了一个简单的注册表。 How should i edit this form to add something like : user_id, or date_created? 我应该如何编辑此表单以添加诸如user_id或date_created之类的内容? When i add user_id column to PHPMyAdmin, the user can't register, but when i have only the values : 'First Name' ; 当我将user_id列添加到PHPMyAdmin时,用户无法注册,但是当我只有以下值时:'First Name'; 'Last Name' ; '姓' ; 'Email' ; “电子邮件”; 'Password' inside database ,everything works. 数据库内的“密码”,一切正常。

    <!DOCTYPE html>
<html>
<head>
    <title>Registration</title>
</head>
<body>
<?php
$lclhost = "localhost";
$pass = "";
$root = "root";
$database = "regis";
$con=mysqli_connect ("$lclhost", "$root", "$pass") or die("connection fail".mysql_error());
mysqli_select_db($con, $database) or die("database fail".mysql_error());
?>
<form method="get">
    First Name : <input type="text" name="fname"><br>
    Last Name : <input type="text" name="lname"><br>
    Email: <input type="text" name="email"><br>
    Password: <input type="Password" name="password">
    <input type="submit" name="btnsubmit">
</form>
<?php 
if (isset($_GET['btnsubmit']))
{
    $fname = $_GET['fname'];
    $lname = $_GET['lname'];
    $email = $_GET['email'];
    $password = $_GET['password'];
    $registration = "INSERT INTO tbl_info values ('".$fname."','".$lname."','".$email."','".$password."')";
    mysqli_query($con,$registration);
    echo "Succes!";
}
?>
</body>
</html>

Because of your query doesnt specify columns, mysql will only work if the number of column values you provided is exactly the same as the number of columns in the database. 由于查询未指定列,因此,只有在您提供的列值的数量与数据库中的列数完全相同的情况下,mysql才会起作用。

Adding an extra column to the database will cause the query to break because the number of columns is not longer the same. 向数据库中添加额外的列将导致查询中断,因为列数不再相同。

You have two options: 您有两种选择:

OPTION 1: Add column names in your query: 选项1:在查询中添加列名:

 "INSERT INTO tbl_info(Firstname, Lastname, EmailAddress, Password)   
  values ('".$fname."','".$lname."','".$email."','".$password."')";  

OPTION 2: Always make sure the number of columns in the database is exactly the same as the number of values you provide in your query 选项2:始终确保数据库中的列数与您在查询中提供的值数完全相同

NB: If you have an auto-increament id field, you do not have to include it for the first option because it will auto-add value whenever you insert a record. 注意:如果您有一个自动删除ID字段,则不必在第一个选项中包含它,因为只要您插入一条记录,它将自动添加值。 But you do need to include it for the second option, pass empty string or null as a value 但是您确实需要将其包含在第二个选项中,将空字符串或null作为值传递

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM