Jump to content

[SOLVED] Checking for duplicate records


tartis

Recommended Posts

I am trying to check for category names in my database using PHP and MYSQL.  The problem is that if I enter a new platform name in lower case, I am still able to enter a duplicate in upper case, or just the opposite.  As an example, if I create a platform named xxx, my code keeps me from creating a new one in the same case.  If I try XXX it creates the new duplicate record.  I can never again use xxx, but I can always duplicate XXX.  I hope that that makes sense.  Here is the code:

 

<?php
$con = mysql_connect("servername", "username", "password");
if (!$con)
{
   echo "<h2>Sorry, we cannot process your request at this time, please try again later</h2>\n";
   echo "<a href=\"index.php?content=newquestion\">Try again</a><br>\n";
   echo "<a href=\"index.php\">Return to Home</a>\n";
   exit;
}mysql_select_db("knowledge", $con) or die('Could not connect to database');
$plat_name = $_POST['plat_name'];
$baduser = 0;
// Check if platform name was entered
if (trim($plat_name) == '')
{
   echo "<h2><br><br>Sorry, you must enter a new platform name.</h2><br>\n";
   echo "<a href=\"index.php?content=newplatform\">Try again</a><br>\n";
   echo "<a href=\"index.php\">Return to Home</a>\n";
   $baduser = 1;
   exit;
}
//Check if Platform is already in database
$query = "SELECT plat_name from platform where plat_name = '$plat_name'";
$result = mysql_query($query);
$row = mysql_fetch_array($result, MYSQL_ASSOC);if ($row['plat_name'] == $plat_name)
{
   echo "<h2>Sorry, that Platform name is already taken.</h2><br>\n";
   echo "<a href=\"index.php?content=newplatform\">Try again</a><br>\n";
   echo "<a href=\"index.php\">Return to Home</a>\n";
   $baduser = 1;
}if ($baduser != 1)

{
   //Everything passed, enter question into database
   $query = "INSERT into platform (plat_name)  VALUES ('$plat_name')";
   $result = mysql_query($query) or die('Sorry, we are unable to process your request.' . mysql_error());
}
?>

Link to comment
Share on other sites

Run a check against the DB and use LOWER() in the SQL on the field you do not want duplicated and check it against the field entered. Use strtolower in php to create the same effect so that you know both items are being checked case insensitive. That way it does not matter if they enter it all upper or lower etc, it should still show that the record is in the database.

 

Another item you can try is adding the "UNIQUE" identifier to the table column's attribute when creating the table. It should make the field be required to be unique, but I would still do the above as an extra measure for case sensitivity.

Link to comment
Share on other sites

You can always use strtolower() or strtoupper() to convert everything in both variables to upper case or lowercase.

 

Try this:

<?php
$con = mysql_connect("servername", "username", "password");
if (!$con)
{
   echo "<h2>Sorry, we cannot process your request at this time, please try again later</h2>\n";
   echo "<a href=\"index.php?content=newquestion\">Try again</a><br>\n";
   echo "<a href=\"index.php\">Return to Home</a>\n";
   exit;
}mysql_select_db("knowledge", $con) or die('Could not connect to database');
$plat_name = $_POST['plat_name'];
$baduser = 0;
// Check if platform name was entered
if (trim($plat_name) == '')
{
   echo "<h2><br><br>Sorry, you must enter a new platform name.</h2><br>\n";
   echo "<a href=\"index.php?content=newplatform\">Try again</a><br>\n";
   echo "<a href=\"index.php\">Return to Home</a>\n";
   $baduser = 1;
   exit;
}
//Check if Platform is already in database
$query = "SELECT plat_name from platform where plat_name = '$plat_name'";
$result = mysql_query($query);
$row = mysql_fetch_array($result, MYSQL_ASSOC);if (strtolower($row['plat_name']) == strtolower($plat_name))//Changed this...
{
   echo "<h2>Sorry, that Platform name is already taken.</h2><br>\n";
   echo "<a href=\"index.php?content=newplatform\">Try again</a><br>\n";
   echo "<a href=\"index.php\">Return to Home</a>\n";
   $baduser = 1;
}if ($baduser != 1)

{
   //Everything passed, enter question into database
   $query = "INSERT into platform (plat_name)  VALUES ('$plat_name')";
   $result = mysql_query($query) or die('Sorry, we are unable to process your request.' . mysql_error());
}
?>

 

[EDIT]Bah, went a little fasetr than me there. =/[/EDIT]

Hope it helps.

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.