Jump to content

Help with checking duplicates


lingo5

Recommended Posts

Hi, I`m inserting anew user in the DB like this:

 

$query = "INSERT INTO database.t_usuario (usuario_nombre,usuario,password) ".
"VALUES ('$usuario_nombre','$usuario','$password')";

mysql_query($query) or die('Error, query failed : ' . mysql_error()); 


header("Location: PC_users_display.php"); 
}

what I need to do is to check if the inserted usuario_nombre already exists on the DB and if so echo a message. Thanks.

Link to comment
Share on other sites

$query = "INSERT IGNORE database.t_usuario (usuario_nombre,usuario,password) ".
"VALUES ('$usuario_nombre','$usuario','$password')";

mysql_query($query) or die('Error, query failed : ' . mysql_error()); 

if(mysql_affected_rows==1)
header("Location: PC_users_display.php"); 
}
else{
echo "Duplicate Entry Detected.";
}
}

Link to comment
Share on other sites

Sorry... missed the opening { bracket on the if statement:

 

$query = "INSERT IGNORE database.t_usuario (usuario_nombre,usuario,password) ".
"VALUES ('$usuario_nombre','$usuario','$password')";

mysql_query($query) or die('Error, query failed : ' . mysql_error()); 

if(mysql_affected_rows==1){
header("Location: PC_users_display.php"); 
}
else{
echo "Duplicate Entry Detected.";
}

Link to comment
Share on other sites

I've changed my mind and I want to insert users in a different table. This is how I insert a nw user at the moment:

$query = "INSERT INTO database.t_clientes (company_name,cliente_calle,cliente_numero,cliente_local,cliente_poblacion,cliente_cp,cliente_provincia,cliente_tel,cliente_fax,cliente_movil,cliente_personadecontacto,cliente_email,cliente_password,cliente_logo) ".

"VALUES ('$company_name','$cliente_calle','$cliente_numero','$cliente_local','$cliente_poblacion','$cliente_cp','$cliente_provincia','$cliente_tel','$cliente_fax','$cliente_movil','$cliente_personadecontacto','$cliente_email','$cliente_password','$filePath')";

mysql_query($query) or die('Error, query failed : ' . mysql_error()); 

header("Location: PC_clientes_display.php"); 
}

 

I want to check if '$cliente_email' (client's email address) already exists before inserting the new user. If so, print an error message.

Link to comment
Share on other sites

I assume you are using MySQL.

 

Create a UNIQUE INDEX on the field that you do not want duplicated. (see MySQL manual).

 

 

Wrap your code that actually inserts in a TRY{...}CATCH{...} block. If it enters the CATCH block,

examine the error message, and perform further processing. Or just notify the user of the error message.

 

 

Alternatively, before inserting, perform a

 

SELECT * FROM t_clientes  where cliente_email= '$cliente_email'

 

It that query returned rows (i.e., row >= 1)

 

throw a "email already used" exception.

Link to comment
Share on other sites

Thanks a lot ebmigue, this is how i've finally done it and it works:

 

mysql_select_db($database_MySQLconnect, $MySQLconnect);

$query_checkdup_RS = sprintf("SELECT * FROM t_clientes WHERE cliente_email = %s", GetSQLValueString($colname_checkdup_RS, "text"));

$checkdup_RS = mysql_query($query_checkdup_RS, $MySQLconnect) or die(mysql_error());

$row_checkdup_RS = mysql_fetch_assoc($checkdup_RS);

$totalRows_checkdup_RS = mysql_num_rows($checkdup_RS);

 

if($totalRows_checkdup_RS==1)

{

echo "Duplicate Entry Detected.";

}

 

else{

 

$query = "INSERT INTO databse.t_clientes (company_name,cliente_calle,cliente_numero,cliente_local,cliente_poblacion,cliente_cp,cliente_provincia,cliente_tel,cliente_fax,cliente_movil,cliente_personadecontacto,cliente_email,cliente_password,cliente_logo) ".

 

"VALUES ('$company_name','$cliente_calle','$cliente_numero','$cliente_local','$cliente_poblacion','$cliente_cp','$cliente_provincia','$cliente_tel','$cliente_fax','$cliente_movil','$cliente_personadecontacto','$cliente_email','$cliente_password','$filePath')";

 

mysql_query($query) or die('Error, query failed : ' . mysql_error());

 

header("Location: PC_clientes_display.php");

}

}

 

Now I want to print the error message within the html code instead of inside the head section.

I have tried this


$duplicate = "<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="CP_SiNoText">la direccón de email ya existe en la base de datos.</td>
</tr>
</table>";

 

and then rinting it inside the html section like this

 

<?php echo $duplicate;?>

but nothing happens. What am I doing wrong?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.