PDA

View Full Version : Data being duplicated


bddotnet
10-16-07, 11:54 PM
I have this code.


<HTML>
<HEAD>
<link href="http://www.okfqhr.com/style2.css" rel="stylesheet" type="text/css">
</HEAD>
<BODY>


<?php
$con = mysql_connect("localhost","database","pwd");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}mysql_select_db("database", $con);

$sql="INSERT INTO links (name, url, category)
VALUES
('$_POST[name]','$_POST[url]','$_POST[category]')";if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo '<center><img src="http://www.okfqhr.com/images/head.jpg"><br><p class="sectionheader">1 link added.</p><br><a href="http://www.okfqhr.com/links.php">OkFQHR Links</a>'; mysql_close($con)
?>


</BODY>
</HTML>


Why, when I try to insert a link via a page on my site, do I get the error:

Duplicate entry "person's name" for key 1

The value $name is the primary key. Do I need an ID variable as the primary key to make this work?

PS - The same exact code is used for my announcements page and it works just fine. (with obvious changes of table name and variables, of course).

DawG
10-29-07, 01:53 PM
Are you still having problems?

What value are you trying to pass?

Does that value already exist in the table?

Personally I wouldn't make name a primary key. This limits you from having two links named anything the same on your page. But, if that is how it is, then it is.

Neat Pete
10-29-07, 02:48 PM
You can't have duplicate values for the primary key. That's by definition of what a primary key is. If you have a design that needs the same value twice for the primary key, then your choice for the primary key was not correct.

Look up one to many relationship in Google.

patr547
10-29-07, 05:43 PM
1) this may sound like a stupid question, but did the values get inserted (even with the error)?

2) is there a duplicate name in the table?

3) if all else fails, try adding an auto_increment ID column and make that the key, see if it removes the error (i'm sure you have your reasons for using the name as the PK, but i, like DawG, would not use a name as a PK, most of the time i go for an auto_increment number, or a combination of columns)

bddotnet
11-4-07, 11:38 AM
I finally figured out what I was doing wrong!

This is the form:


<form action="http://www.okfqhr.com/insert_link.php" method="post" name="links">
<blockquote>
<p>Name of site:
<input type="text" name="name" class="textfield">
The name of the person or company to whom the site belongs</p>
<p>URL:
<input name="url" type="text" id="url" value="http://">
If no website, erase everything in this box. Otherwise, enter the
website address after http://</p>
<p>Category:
<select name="category">
<option value="Select" selected>Select</option>
<option value="National Associations &amp; Affiliates">National
Associations &amp; Affiliates</option>
<option value="Member Sites">Member Sites</option>
<option value="Other Equine Sites of Interest">Other Equine Sites
of Interest</option>
</select>
What category does the site fall under?</p>
<p align="center">
<input type="submit" name="Submit" value="Submit" class="nav">
<input type="reset" name="Reset" value="Reset" class="nav">
</p>
</blockquote>
</form>


This is the action of the form:


<?php
$con = mysql_connect("username.powwebmysql.com","database_name","database_password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}mysql_select_db("database_name", $con);

$sql="INSERT INTO table_name (field1, field2, field3)
VALUES
('$_POST[field1]','$_POST[field2]','$_POST[field3]')";if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo '<center><img src="http://www.okfqhr.com/images/head.jpg"><br><p class="sectionheader">1 link added.</p><br><a href="http://www.okfqhr.com/links.php">OkFQHR Links</a>'; mysql_close($con)
?>