Bruno Herrera Bruno Herrera - 3 years ago 75
PHP Question

Form post to db only once, php

a form is posted to db only once, i have to delete the row in db for the form to post new form data again. I tried several modifications from internet but didnt allowed me to post after the first successful time

Form:

<form action="post.php" method="POST">
<h5>A form:</h5>
<input class="right-inputs" id="field1" type="number" name="field1" placeholder="Enter stuff">
<input class="right-inputs" id="field2" type="text" name="field2" value="" readonly>
<script src="js/insert.js"></script>
<button class="btn-cmn" type="submit" name="submit">post</button>
</form>


post.php:

<?php

include_once 'db.php';

$var1 = $_POST['field1'];
$var2 = $_POST['field2'];

$sql = "INSERT INTO `a_table` (column1, column2) VALUES ('$var1', '$var2')";

mysqli_query($conn, $sql);
mysqli_close($conn);

header("Location: page.php?post=success");

?>


db.php:

<?php

$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "database_sample";

$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);

?>

Answer Source

I think You have Primary Key in your table that not automatic generate value. So you can't add more row while have same Primary key.. Make sure you generate unique key for primary or have auto increment number for this.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download