vinman64
vinman64

Reputation: 55

Auto Number Php form field

I have a php form where I add entries to a mysql database. I am trying to have the number field on the php form be automatically numbered. So, instead of having to type in the number myself for each entry, it would appear in the number field box automatically on it's own and then automatically increment to the next number on the following entry. I have set the number field in phpmyadmin to auto increment. If anyone could show me how to code this, I'd really appreciate it.

Here is the php code and the html I'm currently using.

php code

<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 code

<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>

Upvotes: 1

Views: 3460

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133370

You have not a primary key with auto increment you can create one (or create directly in create table)

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

then you insert don't need this column )because is managed automatically) but simply

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

Upvotes: 2

Related Questions