Q:

Duplicate entry '1' for key 'PRIMARY' what is going wrong?

I am trying to add 1 to the last ID. if the last ID was 6, I would like to add 1 which will make the next ID value 7. at the moment I keep getting this error: Duplicate entry '1' for key 'PRIMARY'. what is going wrong?
            <?php
            require_once 'connect.php';
            if (isset($_POST['reg'])){ 
                        $dob = $_POST['date'];
                        $Student_ID = $_POST['Student_ID'];
                        $gender = $_POST['gender'];
                        $course = $_POST['Course'];
                        $email = $_POST['inputEmail'];
                        $password = $_POST['inputPassword'];
                        $FN = $_POST['FirstName'];
                        $SN = $_POST['SecondName']; 

                        $cur_ID = "SELECT MAX(ID) + 1 FROM `tblaccounts`";
                        $IDresult = mysqli_query($connection, $cur_ID) or die("Database Connection Failed" . mysqli_error($connection));
                        $IDcount = mysqli_num_rows($IDresult);
                        $id = $IDcount;

                    $sql = "INSERT INTO tblaccounts (Email, Password, Student_ID, ID, FirstName, SecondName, Course, Gender, DoB) VALUES ('".$email."','".$password."','".$Student_ID."','".$id."','".$FN."','".$SN."','".$course."','".$gender."','".$dob."')"; 
                    $result = mysqli_query($connection, $sql) or die("Database Connection Failed" . mysqli_error($connection));
                    $count = mysqli_num_rows($result);

            if ($count == 1){
                    echo "Account created!";
                    header('Location: profile.php');
                    //session_start();
                    //$id = session_id();
                    $cookie_name = "user";
                    $cookie_value = $username;
                    setcookie($cookie_name, $cookie_value);

                    //$result2 =  mysqli_query($connection, "SELECT Student_ID FROM `tblaccounts` WHERE Email='$username'");
                    $row2 = mysqli_fetch_assoc($userid);
                    $cookie_name2 = "userID";
                    $cookie_value2 = $userid['Student_ID'];
                    setcookie($cookie_name2, $cookie_value2);
                } else { 
                    echo "Login Failed!:";#
                    ?><br/><a href ="login.php">Go back to the login screen.</a><?php
                }
            }
            ?>

A:

This is too long for a comment.
First, don't munge query strings with parameter values.  Learn to use proper parameters.  This prevents unexpected syntax errors.  And, it protects against SQL injection attacks.  If you are learning to program using SQL, it is all the more important to learn to do so correctly.
Second, don't increment ids yourself.  MySQL (and basically all other databases) support auto-increment ids.  In MySQL, you would declare this as:
create table tblaccounts (
    accountid int auto_increment primary key,
    . . .

Having the database do the work is very important.  It ensures that the values are always set correctly.  If you have two threads inserting at the same time, then you can get the same value -- even if the code were "correctly" written for one thread.

