Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Checking for duplicates before adding data

Author  Topic 

ncncnc
Starting Member

9 Posts

Posted - 2012-05-08 : 18:09:33
Hi,

I'm making a website that uses data stored in SQL.

One of my pages allows the user to enter a new product into a product table. I was wondering if somebody could help me with some validation or Query that will not allow the user to enter a product name that already exists in the table?

 $nameNew = $_POST['new];
$nameID = $_POST['newID];
$namePrice = $_POST['newPrice];

$editQuery = "INSERT INTO products
VALUES ($nameNew, $newID, $newPrice)"'

$describeQuery = "SELECT ID, Name, Price FROM Products";


$results = sqlsrv_query($conn, $describeQuery);

echo '<table border="1" BORDERCOLOR=Black>';
echo '<tr><th bgcolor = "LightBlue">Name</th><th bgcolor = "LightBlue" >ID</th> <th bgcolor = "LightBlue" >Price</th></tr>';


while($row = sqlsrv_fetch_array($results, SQLSRV_FETCH_ASSOC))
{
echo '<tr>';
echo '<td >' .$row['Name'].'</td>';
echo '<td>' .$row['ID'].'</td>';
echo '<td>' .$row['Price'].'</td>';

echo '</tr>';
}

echo '</table>';
sqlsrv_close($conn);




Can anybody help I'm totally out of ideas.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-05-08 : 18:13:12
Shouldn't you just add a unique constraint and then handle the exception?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

ncncnc
Starting Member

9 Posts

Posted - 2012-05-08 : 18:17:27
I'm not sure I understand.

Aren't unique constraints only used when you create a new table? I'm only inserting new data. I'm sorry I'm asking questions like an idiot, but I've only taken a crash course.
Go to Top of Page

ncncnc
Starting Member

9 Posts

Posted - 2012-05-08 : 18:24:05
Am I right in thinking you can add them in SQL management view?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-05-08 : 18:32:42
A constraint can be added at any time. It protects the data in the database. If you want the data to be unique in a column, then you add a unique constraint to it.

I use ALTER TABLE ADD CONSTRAINT to add my constraints. I don't use the GUI.

http://msdn.microsoft.com/en-us/library/ms190273.aspx

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

ncncnc
Starting Member

9 Posts

Posted - 2012-05-08 : 18:35:47
Ah thanks a lot you've been a big help.

One final thing, what will happen if the user does enter a duplicate value. Will nothing happen or will an error be thrown?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-05-08 : 18:42:25
Your application will receive an exception, which you'll need to handle.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -