使用php更新html表单

时间:2018-06-03 03:49:49

标签: php mysqli html-form

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

<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");

?>

1 个答案:

答案 0 :(得分:0)

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

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

ALTER TABLE tbl_orders MODIFY id int AUTO_INCREMENT;