I have these codes for the insertion of data into mysql database in php, unfortunately when I insert the data into the database they are duplicated. Any suggestion on what could be wrong with the codes.

// create short variable names
if (!$Publication_Date || !$Title || !$Write_Up)
echo '<h4>You have not entered all the required details.<br /></h4>';
echo '<h4>Please go back and try again.</h4>';
$Publication_Date = addslashes($Publication_Date);
$Title = addslashes($Title);
$Write_Up = addslashes($Write_Up);
include 'dbconnect.inc';
$query =  "INSERT INTO $Database (Publication_Date, Title, Write_Up) ".
"VALUES ('$Publication_Date', '$Title', '$Write_Up')";
mysql_query($query) or die('Error, query failed'); 
$result = mysql_query($query);
if ($result)
echo mysql_affected_rows().' Write-Up inserted into database.';


I added this to correct the duplication before inserting anything into the database, but when I insert data, I find nothing in the database.

$query = "select Title from $Database where Title = '$Title'";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
echo '<p>This article is already in the database.  Thanks.</p>';

Recommended Answers

All 2 Replies

First you have to pass the title to the database as lowercase or uppercase as you want so you can avoid problems caused by "typos" during the data insertion from the user or the admin.
Next you have to make the Title filed "UNIQUE" in the table structure so the database itself takes the job to prevent the user from inserting duplicated entries.

Member Avatar

You are calling your query twice. On line 19 then again on line 20.
Remove line 19.

commented: Even you are right but still using the "unique" index in the table safer and insures the "quality" of code +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.