INSERT INTO `test`.`lecturer` (`lecturerID`, `course_ID`, `lecturerFirstName`, `lecturerLastName`, `lectureremail`, `lecturerpassword`) VALUES (NULL, NULL, 'test', 'test', 'a@gmail.com', '123456');
提供的是我输入我的phpmyadmin的测试代码,讲话是主键并且还有自动增量,据我所知,我不想在那里键入任何东西,sql server会为我输入下一个号码,但是我继续得到#1062 - 重复录入' 0'关键' PRIMARY'。任何人都可以给我一些见解为什么我面对这个?感谢。
完整代码:
<?php include_once "/_core/core.php"; ?>
<?php
if(isset($_POST['id']))
{
$delid = mysql_real_escape_string($_POST['id']);
$sql="DELETE FROM lecturer WHERE lecturerID='$delid'";
mysql_query($sql);
}
if(isset($_POST['fname']) and ($_POST['lname']) and ($_POST['email']) and ($_POST['password']))
{
$fname = mysql_real_escape_string($_POST['fname']);
$lname = mysql_real_escape_string($_POST['lname']);
$email = mysql_real_escape_string($_POST['email']);
$password = mysql_real_escape_string($_POST['password']);
echo $fname, $lname, $email, $password;
$sql = 'INSERT INTO lecturer (lecturerID, course_ID, lecturerFirstName, lecturer_LastName, lectureremail, lecturerpassword) VALUES (NULL,"0","'.$fname.'","'.$lname.'","'.$email.'","'.$password.'")';
mysql_query($sql);
}
?>
<?php
if(!empty($_SESSION['Admin']) && !empty($_SESSION['Username']))
{
?>
<?php include "_core/menubar.php"; ?>
<br><br><br>
<form class="form-horizontal" action="add_lecturer.php" method="POST">
<fieldset>
<!-- Form Name -->
<legend>Add lecturer</legend>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="fname">First Name</label>
<div class="col-md-4">
<input id="fname" name="fname" type="text" placeholder="First Name" class="form-control input-md" required="">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="lname">Last Name</label>
<div class="col-md-4">
<input id="lname" name="lname" type="text" placeholder="Last Name" class="form-control input-md" required="">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="email">Email</label>
<div class="col-md-5">
<input id="email" name="email" type="text" placeholder="test@apu.edu.my" class="form-control input-md" required="">
</div>
</div>
<!-- Password-->
<div class="form-group">
<label class="col-md-4 control-label" for="password">Password</label>
<div class="col-md-4">
<input id="password" name="password" type="password" placeholder="*****" class="form-control input-md" required="">
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="submit"></label>
<div class="col-md-4">
<button id="submit" name="submit" class="btn btn-success">Submit</button>
</div>
</div>
</fieldset>
</form>
<?php
$query = ('SELECT * FROM lecturer');
$q = mysql_query($query);
if(mysql_num_rows($q) > 0)
{
?>
Lecturer List
<table class="table table-striped table-hover">
<form action="add_lecturer.php" method="post">
<?php
while($row = mysql_fetch_array($q)) {
echo "<tr>";
echo "<td>" . $row['lecturerID'] . "</td>";
echo "<td>" . $row['lecturerFirstName'] . "</td>";
echo "<td>" . $row['lecturerLastName'] . "</td>";
echo "<td>" . $row['lectureremail'] . "</td>";
echo '<td><input type="submit" name="deleteItem" value="Delete" /></td>';
echo '<td><input type="hidden" id="id" name="id" value="'.$row['lecturerID'].'" /></td>';
echo "</tr>";
}
?>
</table>
</form>
<?php
}
else
{
echo "You have no lecturers added!";
}
?>
<?php
}
else
{
echo "<meta http-equiv='refresh' content='2;index.php' />";
?>
<?php include "../_class/login.php"; ?>
<?php
}
?>
</div>
<?php include "../_class/footer.php"; ?>
答案 0 :(得分:1)
您必须将NULL
传递给自动增量列而不是Blank
值,如此
INSERT INTO `test`.`lecturer`
(`lecturerID`, `course_ID`, `lecturerFirstName`, `lecturerLastName`, `lectureremail`, `lecturerpassword`)
VALUES (NULL, NULL, 'test', 'test', 'a@gmail.com', '123456');
答案 1 :(得分:1)
您应该将NULL
值分配给该自动增量列:
INSERT INTO `test`.`lecturer`
(`lecturerID`, `course_ID`, `lecturerFirstName`, `lecturerLastName`, `lectureremail`, `lecturerpassword`)
VALUES
(NULL, NULL, 'test', 'test', 'a@gmail.com', '123456');
或者您可以尝试插入而不在查询中指定该列,如下所示:
INSERT INTO `test`.`lecturer`
(`course_ID`, `lecturerFirstName`, `lecturerLastName`, `lectureremail`, `lecturerpassword`)
VALUES
(NULL, 'test', 'test', 'a@gmail.com', '123456');
答案 2 :(得分:1)
INSERT INTO `test`.`lecturer` (`lecturerID`, `course_ID`, `lecturerFirstName`, `lecturerLastName`, `lectureremail`, `lecturerpassword`)
VALUES ('', '', 'test', 'test', 'a@gmail.com', '123456');
你必须在sql数据库中自动增加你的分配键