自动编号 Php 表单域

Auto Number Php form field

我有一个 php 表单,我可以在其中向 mysql 数据库添加条目。我试图让 php 表单上的数字字段自动编号。因此,不必自己为每个条目输入数字,它会自动出现在数字字段框中,然后自动递增到下一个条目的下一个数字。我已将 phpmyadmin 中的数字字段设置为自动递增。如果有人能告诉我如何编写代码,我将不胜感激。

这是 php 代码和我目前正在使用的 html。

php代码

<html>
<head>
    <title>Add Data</title>
</head>

<body>
<?php
//including the database connection file
include_once("config.php");

if(isset($_POST['Submit'])) {    
    $Number = $_POST['Number'];
    $Link = $_POST['Link'];
    $Article = $_POST['Article'];
    $Date = $_POST['Date'];
    $Name = $_POST['Name'];

    // checking empty fields
    if(empty($Number) || empty($Link) || empty($Article) || empty($Date) || empty($Name)) {

        if(empty($Number)) {
            echo "<font color='red'>Number field is empty.</font><br/>";
        }

        if(empty($Link)) {
            echo "<font color='red'>Link field is empty.</font><br/>";
        }

        if(empty($Article)) {
            echo "<font color='red'>Article field is empty.</font><br/>";
        }

        if(empty($Date)) {
            echo "<font color='red'>Date field is empty.</font><br/>";
        }

        if(empty($Name)) {
            echo "<font color='red'>Name field is empty.</font><br/>";
        }
        //link to the previous page
        echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";
    } else { 
        // if all the fields are filled (not empty) 

        //insert data to database        
        $sql = "INSERT INTO crypto(Number, Link, Article, Date, Name) VALUES(:Number, :Link, :Article, :Date, :Name)";
        $query = $dbConn->prepare($sql);

        $query->bindparam(':Number', $Number);
        $query->bindparam(':Link', $Link);
        $query->bindparam(':Article', $Article);
        $query->bindparam(':Date', $Date);
        $query->bindparam(':Name', $Name);
        $query->execute();

        // Alternative to above bindparam and execute
        // $query->execute(array(':Number' => $Number, ':Date' => $Date, ':Name' => $Name, ':Link' => $Link, ':Article' => $Article));

        //display success message
        echo "<font color='green'>Data added successfully.";
        echo "<br/><a href='index.php'>View Result</a>";
    }
}
?>
</body>
</html>

html代码

<html>
<head>
    <title>Add Data</title>
  <!-- append ?v=2 to icon url -->
  <link rel="shortcut icon" href="/logo.jpg">
  <link rel="apple-touch-icon" href="/logo.jpg" />
</head>

<body>
    <a href="index.php">Home</a>
    <br/><br/>

    <form action="add.php" method="post" name="form1">
        <table width="25%" border="0">
            <tr> 
                <td>Number</td>
                <td><input type="text" name="Number"></td>
            </tr>
                        <tr> 
                <td>Link</td>
                <td><input type="text" name="Link" style="width: 450px;" /></td>

            </tr>
            <tr> 
                <td>Article</td>
                <td><input type="text" name="Article" style="width: 800px;" /></td>

            </tr>
            <tr> 
                <td>Date</td>
                <td><input type="text" name="Date"></td>
            </tr>
            <tr> 
                <td>Name</td>
                <td><input type="text" name="Name"></td>
                        </tr>
            <tr> 
                <td></td>
                <td><input type="submit" name="Submit" value="Add"></td>
            </tr>
        </table>
    </form>
</body>
</html>

您没有自动递增的主键您可以创建一个(或直接在 create table 中创建)

ALTER TABLE crypto CHANGE Number Number INT(10) AUTO_INCREMENT PRIMARY KEY;

然后您插入不需要此列)因为它是自动管理的)而只是

$sql = "INSERT INTO crypto(Link, Article, Date, Name) 
              VALUES(:Link, :Article, :Date, :Name)";