【问题标题】:PHP - Implementing a Primary key SQLPHP - 实现主键 SQL
【发布时间】:2016-09-10 17:49:08
【问题描述】:
    <?php
    require_once 'Connect.php';


    //Prepare HTML insert statement binding parameters
    $stmt = $conn->prepare("INSERT INTO records (`Title`, `FirstName`, `LastName`, `Gender`, `DOB`, `Mem.Expiry`, `Mem.Type`, `EmailAddress`) 
    VALUES (:Title, :Fname, :Lname, :Gender, :DOB, :MemX, :MemType, :Email)");



        $title = $_POST['Title'];
        $fname = $_POST['Fname'];
        $lname = $_POST['Lname'];
        $gender = $_POST['Gender'];
        $dob = $_POST['DOB'];
        $memx = $_POST['MemX'];
        $memtype = $_POST['MemType'];
        $email = $_POST['Email'];


    //Attempt row insertion by executing prepared statement
    try
    {
        //Insert a row

        $stmt ->bindParam(':Title', $title);
        $stmt ->bindParam(':Fname', $fname);
        $stmt ->bindParam(':Lname', $lname);
        $stmt ->bindParam(':Gender', $gender);
        $stmt ->bindParam(':DOB', $dob);
        $stmt ->bindParam(':MemX', $memx);
        $stmt ->bindParam(':MemType', $memtype);
        $stmt ->bindParam(':Email', $email);

        $stmt->execute();

    }
    catch (PDOException $e)
    {
        echo $e->getMessage();
    }


?>

我有一个更新连接到 localhost 的数据库的 Web 表单。我想实现一个主键。当我包含 ID 列并将其设置为主键时,如何实现它自动填充上面的代码?我在网上查过,但找不到任何有用的东西。

我清除了数据库并插入了一个主键。现在当我填写表格时,第一个输入将被上传,主键将为 0。在此之后没有其他信息被注册?

【问题讨论】:

  • 你不知道。如果它是自动增量,只需将其完全排除在 INSERT 查询之外。 MYSQL 会在该列中创建下一个数字
  • 当你离开 MYSQL 做所有的工作时,第一个 AutoIncrement 键通常是 1
  • 单独注意:$conn-&gt;prepare() 应该在您的try/catch 中,这与bindParamexecute 一样可能或实际上更有可能引发异常

标签: php mysql sql wamp primary-key


【解决方案1】:

我想你在找Auto Increment

【讨论】:

  • 我清除了数据库并插入了一个主键。现在,当我填写表格时,第一个输入将被上传,主键将为 0。之后没有其他信息被注册?
  • 现在每次向表中插入新行时,mysql 都应该创建一个新 id。 (从 1 开始,如果没有设置/给出其他值)。 CREATE 语句会有所帮助。 (或 ALTER 表,如果您使用此表)。
猜你喜欢
  • 2013-01-29
  • 2022-01-03
  • 1970-01-01
  • 2014-11-19
  • 2010-10-24
  • 2016-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多