Reputation: 1892
This is my MySQL error.
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 2
I googled it and read something about it, bud I couldn't understand.
How to solve it?
This is the main piece of addStudent.php
:
require_once('../db.php');
$db = new DB();
if (isset($_POST['st_fname']) && isset($_POST['st_lname']) && isset($_POST['st_class']) && isset($_POST['st_grade']))
{
$db->addStudent($_POST["st_fname"], $_POST["st_lname"], $_POST["st_class"], $_POST["st_grade"], $_POST["checkOlamp"]);
}
and this is a part of db.php
:
public function addStudent($fname, $lname, $classnum, $grade, $olamp)
{
$query = "INSERT INTO t_student (s_fname, s_lname, s_class, s_grade, s_olamp) VALUES('$fname', '$lname', '$classnum', '$grade', '$olamp');";
$this->execute($query);
}
And the t_student has a filed as primary
key which is auto increment.
Upvotes: 5
Views: 31918
Reputation: 1
Another easy way to solve "SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry" is to pass the values in the input fields of form like this: you can see value before placeholder... note:articles is the DB table name
<div class="form-group">
<label for="exampleInputTitle">Title</label>
<input type="title" name="title" class="form-control" id="exampleInputTitle"
aria-describedby="emailHelp"
value="<?php echo $articles->title;?>"placeholder="Enter title">
<small id="titleHelp" class="form-text text-muted"></small>
</div>
Upvotes: 0
Reputation: 99921
It means that the values of some column in your table must be unique, and you are trying to insert a duplicate row.
BTW, your function is vulnerable to SQL injection, you should always escape your data before including it in a SQL query.
Upvotes: 5