-1

I'm trying to create a registration form to insert or update users credential in a simple mysql table

I've watched almost every question here but I can't figure why my code doesn't update or insert the user in the form

//check if the user is already registered and then override and update his credentials 
$check = "SELECT COUNT(*) FROM theElisa_signUp WHERE Email = '$email'"; 

if (mysqli_query($conn, $check) >= 1)
{
    $update = mysqli_query($conn, "UPDATE theElisa_signUp SET name = '$name', user = '$username', psw = '$password', expire_date = '$expire_date' WHERE email = '$email'");
} elseif (mysqli_query($conn, $check) == 0) {
    //inserire dati sign up dei nuovi registrati
    $query = mysqli_query($conn, "INSERT INTO theElisa_signUp (name, email, telephone, user, psw, role, expire_date) VALUES ('$name', '$email', 'not-set', '$username', '$password', '$role', '$expire_date')");
} else {
    header( "Location: ../admin/index.php" );
}

As you can see, I want to check if the user's email already exists. If exists (in this case, if it's >= 1) the code must UPDATE the values already set in the mysql table. If doesn't exist already (== 0) it will create a new user with the INSERT.

I hope my question is clear and respects the standards.

EDIT: i've tried also the ON DUPLICATE KEY UPDATE but still nothing.

$query=mysqli_query($conn, "INSERT INTO theElisa_signUp (name, email, telephone, user, psw, role, expire_date) VALUES ('$name', '$email', 'not-set', '$username', '$password', '$role', '$expire_date')
ON DUPLICATE KEY UPDATE name = '$name', user = '$username', password = '$password', expire_date = '$expire_date' ");

Really hoping someone can help in this. Unfortunately I'm a simple web designer but for this project they asked me to do this and I have zero code background.

1 Answers1

0

From PHP Manual

/* Select queries return a resultset */
if ($result = mysqli_query($link, "SELECT Name FROM City LIMIT 10")) {
    printf("Select returned %d rows.\n", mysqli_num_rows($result));

    /* free result set */
    mysqli_free_result($result);
}
David Alvarez
  • 1,226
  • 11
  • 23