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.
- db.php is something that I always use it instead of mysql_connection function in php, but I don’t know what it is exactly. I know something that called “PDO” is used there.
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.