首页 > 解决方案 > 用 php 更新 html 表单

问题描述

我正在尝试使用下面的 php 和 html 代码更新我的 phpmyadmin 数据库:我已成功添加第一行数据,但是一旦我再次尝试使用该表单,我无法添加另一行数据,除非我删除前一个我插入到数据库中。简而言之,我的数据库一次只能有 1 行。谢谢你的帮助 :)

<html>
<head>
</head>
<body>

    <form action="inserto.php" method="post">

        Customer ID: <input type="number" name="IDc">
            <br/>
        Stock ID: <input type="number" name="IDs">
            <br/>
        Date of Purchase: <input type="date" name="dob">
            <br/>
        Pay status: <input type="text" name="paystatus">
            <br/>
        Price Paid: <input type="text" name="price">
            <br/>
        Discount: <input type="text" name="discount">
            <br/>
        <input type="submit" value="Submit">
    </form>






</body>
</html>


<?php

    $con = mysqli_connect("localhost", "root", "", "jainam_ia");

    if(!$con)
    {
        echo 'Not connected';
    }

    $IDc=$_POST['IDc'];
    $IDs=$_POST['IDs'];
    $dob=$_POST['dob'];
    $paystatus=$_POST['paystatus'];
    $price=$_POST['price'];
    $discount=$_POST['discount'];

    $sql = "INSERT INTO tbl_orders (IDc, IDs, Date_of_purchase, Pay_Status, Price_Paid, Discount) VALUES ('$IDc', '$IDs', '$dob', '$paystatus', '$price', '$discount')";

    if(!mysqli_query($con,$sql))
    {
        echo 'Order not added';
    }
    else
    {
        echo 'Order added';
    }

    header("refresh:2; url=indexo.html");

?>

标签: phpmysqliforms

解决方案


很可能在您的 mysql 表tbl_orders中设置了具有primary_key属性但没有auto_increment属性的列。

添加属性示例auto_increment(您需要将id名称更改为主键列名称):

ALTER TABLE tbl_orders MODIFY id int AUTO_INCREMENT;

推荐阅读