Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

213
Visualizações
solution for duplicate username

I see some php course then I learn mostly by practical way so my question is very newbie , I didn't understand why this code don't work solution for duplicate username while registre , and every time i receive probleme with header

function signup($conn) {

    $uid = $_POST['uid'];
    $pwd = $_POST['pwd'];



    if (isset($_POST['signupSubmit'])) {



        $query = mysql_query("select * from user where uid='$uid'");
        $encrypted_password = password_hash($pwd, PASSWORD_DEFAULT);
        if(mysql_num_rows(query)>0)
        {
             echo "<script> window.location.replace('test.php') </script>" ;

        }

        else {

        $sql = "insert into usi (uid, pwd)  values('$uid','$encrypted_password')";

        $result = $conn->query($sql);
        header("Location: test.php");




    }

}

}
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

this:

$uid = $_POST['uid'];
$pwd = $_POST['pwd'];
if (isset($_POST['signupSubmit'])) {

should be:

 if (isset($_POST['signupSubmit'])) {
     $uid = $_POST['uid'];
     $pwd = $_POST['pwd'];

i don't have solution for mysql_ but here is PDO example i made, hope it helps

$dbhost = "localhost";
$dbname = "database";
$mysqlusr = "root";
$mysqlpass = "";

try {
    $db = new PDO("mysql:host={$dbhost};dbname={$dbname}", $mysqlusr, $mysqlpass);
    $db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
    } 
    catch (PDOException $e){
        echo $e->getMessage();
    }

if(isset($_POST['signupSubmit'])){
    $uid = $_POST['uid'];
    $pwd = $_POST['pwd'];

    $encrypted_password = password_hash($pwd, PASSWORD_DEFAULT);

    try {
        $sql = "SELECT uid FROM user WHERE uid=:uid";           
        $statement = $db->prepare($sql);
        $statement->bindParam(':uid', $uid, PDO::PARAM_STR);
        $statement->execute();
        $result = $statement->fetch(PDO::FETCH_ASSOC);

        if (empty($result)) {
            try { 
                $sql = "INSERT INTO user (uid, pwd) VALUES (:uid, :pwd)";
                $statement = $db->prepare($sql);
                $statement->bindParam(':uid', $uid, PDO::PARAM_STR);
                $statement->bindParam(':pwd', $encrypted_password, PDO::PARAM_STR);
                $statement->execute();

                echo "new user registered";
             }
            catch (PDOException $e){
                echo $e->getMessage();
            }
        }
            else {

                echo "username already exists";
            }
    }
    catch (PDOException $e){
        echo $e->getMessage();
    }
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda