0

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.

<?php
// create short variable names
$Database=$HTTP_POST_VARS['Database'];
$Publication_Date=$HTTP_POST_VARS['Publication_Date'];
$Title=$HTTP_POST_VARS['Title'];
$Write_Up=$HTTP_POST_VARS['Publication'];
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>';
exit;
}
$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>';
exit;
}
3
Contributors
2
Replies
30
Views
4 Years
Discussion Span
Last Post by Zagga
0

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.

1

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

Votes + Comments
Even you are right but still using the "unique" index in the table safer and insures the "quality" of code
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.