mysql querying problem
Posted: Thu Sep 20, 2007 8:16 am
Hi guys,
I'm building a system which takes a tab delimited file and inserts it into a database. However, they need to fit into 'sections' and need to be distinct, so before adding a new section I query the database to see if there's already one there.
However, this is acting weird and works for some parts but some of the sections are getting past this and being duplicated. Is there anything wrong with my code below?
Thanks in advance guys!
I'm building a system which takes a tab delimited file and inserts it into a database. However, they need to fit into 'sections' and need to be distinct, so before adding a new section I query the database to see if there's already one there.
However, this is acting weird and works for some parts but some of the sections are getting past this and being duplicated. Is there anything wrong with my code below?
Code: Select all
foreach($sections as $section){
$sectionExistSql = "SELECT * FROM categories_description
WHERE categories_name = '" . $section . "'";
$sectionExistQue = mysql_query($sectionExistSql);
$sectionExist = mysql_num_rows($sectionExistQue);
if($sectionExist == 0 && $section !== NULL){
echo "<p>inserted: " . $section . "</p>";
//KFH SECTION
$sql = "INSERT INTO categories (parent_id, sort_order, date_added)
VALUES ('0',
'0',
'" . $date . "')";
$query = mysql_query($sql);
$sql = "INSERT INTO categories_description (categories_id, categories_name)
VALUES ('" . $cat_id . "',
'" . $section . "')";
$query = mysql_query($sql);
$cat_id++;
}else{
echo "<p>discarded: " . $section . "</p>";
}
}