检查表中是否存在值不起作用

时间:2021-04-26 12:29:33

I am trying to write a script to insert values from an XML file into an SQL database.

我正在尝试编写一个脚本,将XML文件中的值插入到SQL数据库中。

However i can not get the script to correctly check if a value already exists in the SQL database.

但是我无法让脚本正确检查SQL数据库中是否已存在某个值。

heres my current code:

继承我目前的代码:

$result=mysql_query("SELECT * FROM `categories_description` WHERE categories_name`='$qCategory'");
if (mysql_num_rows($result) > 0){
    echo "cat exists";
}
else if($qCategory != ""){
mysql_query("INSERT INTO `categories` (`categories_id`, `categories_image`, `parent_id`, `sort_order`, `date_added`, `last_modified`, `categories_status`) VALUES ('$catid', NULL, '0', NULL, NULL, NULL, '1');");
mysql_query("INSERT INTO `categories_description` (`categories_id`, `language_id`, `categories_name`, `categories_description`) VALUES ('$catid', '1', '$qCategory', '$qCategory');");
$catid +=1;
}

I have tried tons of things to get this to work, but it just doesn't want to. The problem I am having is if I have duplicate value it will insert it anyway. Does anyone know how to fix this? or what it going wrong? Many thanks in advance.

我已经尝试了很多东西来实现它,但它只是不想这样做。我遇到的问题是,如果我有重复值,它会插入它。有谁知道如何解决这一问题?或者它出了什么问题?提前谢谢了。

1 个解决方案

#1


0  

Try to fix this line first, you missed ` at the beginning of categories_name:

首先尝试修复此行,您在categories_name的开头错过了`

"SELECT * FROM `categories_description` WHERE categories_name`='$qCategory'"

#1


0  

Try to fix this line first, you missed ` at the beginning of categories_name:

首先尝试修复此行,您在categories_name的开头错过了`

"SELECT * FROM `categories_description` WHERE categories_name`='$qCategory'"