Reputation: 23
Hello guys I have a problem with a script
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root';
$dbname = 'site';
$con = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$con)
{
die(mysql_error());
}
mysql_select_db($dbname, $con);
$sql = "REPLACE INTO stiri (id, titlu, continut, link, categorie, data) VALUES ('','$titlu','$text','$link','Liga 1','$data')";
mysql_query($sql);
mysql_close($con);
This part is in a php foreach part and every time I run the script I get duplicate entry, how can I prevent this? I could use UNIQUE Constraint but I want the link to be unique which is longer than 125 chars..
Upvotes: 0
Views: 628
Reputation: 360812
I'm guessing id
is a primary key field in your table? You're trying to insert an empty string (''
) into it. If that's an INT field, mysql will convert ''
into 0. After the first such insert, you'll run into the duplicate key problem.
Change id
to be an auto_increment field, and insert a null
value, e.g.
REPLACE INTO stiri (id, ...) VALUES (null, ....)
so mysql can generate an ID for you automatically.
Upvotes: 1