Search code examples
phpmysqlmysqliuser-registration

My form only adds one user, I can add another one when i delete the previous user in my database. Anyone has any tips?


I know my code looks bad, but I just started using PHP. I can't fix the problem and I have been looking online for hours.

    $sql = "INSERT INTO user (username, pass) VALUES ('$username', '$pass')"; 
    $sql_check = "SELECT * FROM user WHERE username = '$username'";
    $res = $conn->query($sql);
    $res_check = $conn->query($sql_check);


    if ($res_check->num_rows > 0) {
        $melding = "Gebruikersnaam is al in gebruik";  
    } else if($res) {
        $melding =  "Gebruiker geregistreerd";
    } else {
        $melding =  "Gebruiker niet geregistreerd";
    }

Solution

  • you should check if your table already has an unique AND auto incrementing index!

    Explanation: If you add an entry the database will give it an unique id (counting from 1). But if the field is not set as "auto incrementing" the index will always be zero and therefore it will fail to add another entry.

    In phpmyadmin: go to "structure" and edit the field ID. Check "AI" (auto increment) and make sure that it is used as primary key (key symbol).

    enter image description here