Reputation:
I want to insert some values into my book table and I created a PHP page with HTML form inside it. The problem is my values gets inserted but it throws a primary key error on the webpage. The code is as below.
<!DOCTYPE html>
<html>
<head>
<title>New Books</title>
</head>
<body>
<?php
if (isset($_POST['submit'])) {
$server="localhost";
$username="root";
$password="";
$db="library";
$conn= new mysqli($server,$username,$password,$db);
if ($conn->connect_error) {
echo "Server Error Occured Please Try Again Later";
}
$bid=$_POST['bid'];
$bname=$_POST['bname'];
$baut=$_POST['baut'];
$bc=$_POST['bc'];
$blf=$_POST['blf'];
$bs=$_POST['bs'];
$sql="insert into books(BOOK_ID,BOOK_NAME,BOOK_AUTHOR,BOOK_CATEGORY,BOOK_LOST_FINE, BOOK_STATUS) values('$bid','$bname','$baut','$bc','$blf','$bs')";
$result=$conn->query($sql);
if ($conn->query($sql)===TRUE) {
echo "New Book Added Successfully";
}
else{
echo "Please Recheck The Values Entered" . $conn->error;
$secondsWait = 5;
header("Refresh:$secondsWait");
}
}
?>
<div class="header">
<a href="images/456.jpg" class="logo">Lowa State University</a>
<div class="header-right">
<a class="active" href="#">Main Page</a>
<a href="contacts.html">Due Fines</a>
<a href="aboutus.html">Contact Us</a>
<a href="">Profile</a>
</div>
</div>
<form action="" method="post">
<input type="text" name="bid" placeholder="BOOK ID"><br><br>
<input type="text" name="bname" placeholder="BOOK NAME"><br><br>
<input type="text" name="baut" placeholder="BOOK AUTHOR"><br><br>
<input type="text" name="bc" placeholder="BOOK CATEGORY"><br><br>
<input type="number" name="blf" placeholder="BOOK LOST FINE"><br><br>
<input type="text" name="bs" placeholder="BOOK STATUS"><br><br>
<input type="submit" name="submit" value="Add Book"><br><br>
<input type="reset" name="reset" value="Clear Fields">
</form>
</body>
</html>
Now if I press the add button this error shows up:
Error Image:
but the values are already entered into the SQL table.
Upvotes: 1
Views: 99
Reputation: 628
When you add any field as primary key that mean it must have unique record, no duplication allowed. you trying to add duplicate(same record) in same table.
You should add BOOK_ID as primary key + auto increment in database and when you insert new record, you don't need to add BOOK_ID manually at code. i automatically incremented from previous record and added automatically.
Just do like this
$sql="insert into
books(BOOK_NAME,BOOK_AUTHOR,BOOK_CATEGORY,BOOK_LOST_FINE,
BOOK_STATUS) values('$bname','$baut','$bc','$blf','$bs')";
and remove following line $result=$conn->query($sql);
Note [add BOOK_ID
AS PRIMARY KEY + AUTO INCREMENT in Table]
Upvotes: 2
Reputation: 193
Just like hingu Devang and Bira mentioned, make your ID column Auto_Increment and do not manually place a value there because it will automatically assign a value.
As for your issue that the query is being executed twice, try removing the line:
$result=$conn->query($sql);
Because you already have a query which is:
if($conn->query($sql)===TRUE)
Upvotes: 1