[英]SQL throws an error when two columns have the same value
I want to insert a record if it does not exist without using unique ID, and I found this answer here: MySQL: Insert record if not exists in table . 我想在不使用唯一ID的情况下插入不存在的记录,并且在这里找到以下答案: MySQL:如果table中不存在,则插入记录 。
In my case I have a table: 就我而言,我有一张桌子:
+----+---------+---------+---------+---------+-----------+-------------+-----------+
| ID | dRelIns | vRelIns | dRelDel | vRelDel | cRelAktiv | iRelDateiId | iRelKatId |
+----+---------+---------+---------+---------+-----------+-------------+-----------+
| 1 | blabla | blabla | NULL | NULL | J | 3 | 5 |
+----+---------+---------+---------+---------+-----------+-------------+-----------+
| 2 | blabla | blabla | blabla | blabla | N | 3 | 1 |
+----+---------+---------+---------+---------+-----------+-------------+-----------+
| 3 | blabla | blabla | NULL | NULL | J | 3 | 2 |
+----+---------+---------+---------+---------+-----------+-------------+-----------+
I am getting an array ($_POST) with id iRelKatId and another on id iRelDateiId . 我得到一个ID为iRelKatId的数组($ _POST),另一个为ID iRelDateiId的数组 。 I check if the id already exists and cRelAktiv = 'J', if not I want to insert a new one. 我检查ID是否已经存在,并且cRelAktiv ='J',如果不是,我想插入一个新的ID。 If some entry exists but it's not in the list, I want to set cRelAktiv to 'N'. 如果存在某些条目但它不在列表中,我想将cRelAktiv设置为'N'。
My PHP script with the SQL queries: 我的带有SQL查询的PHP脚本:
$list=implode("','", $_POST["kat"]);
$sql="UPDATE tabRel_UDK SET dRelDel=NOW(),
vRelDel=USER(),
cRelAktiv='N'
WHERE iRelDateiId='$_POST[id]' AND cRelAktiv='J' AND iRelKatId NOT IN('$list')";
$result = mysql_query($sql) or die(Error (" . mysql_error() . ").");
foreach ($_POST["kat"] as $value) {
$sql="INSERT INTO tabRel_UDK (dRelIns, vRelIns, cRelAktiv, iRelDateiId, iRelKatId)
SELECT * FROM (SELECT NOW(), USER(), 'J', '$_POST[id]','$value') AS tmp
WHERE NOT EXISTS (
SELECT iRelDateiId,iRelKatId,cRelAktiv FROM tabRel_UDK
WHERE iRelDateiId = '$_POST[id]' AND iRelKatId='$value' AND cRelAktiv='J') LIMIT 1;";
$result = mysql_query($sql) or die("Error (" . mysql_error() . ").");
}
This script works for me, but when both ids have the same value(for example 5), it throws an error Duplicate column name '5' because of SELECT * FROM (SELECT NOW(), USER(), 'J', '$_POST[id]','$value')
这个脚本对我的作品,但是当两个IDS具有相同的值(例如5),它抛出一个错误重复的列名“5”,因为SELECT * FROM (SELECT NOW(), USER(), 'J', '$_POST[id]','$value')
Any ideas how to make it works, or should I make 2-3 SQL queries and check the ids manually in PHP? 有任何想法如何使它起作用,还是应该在PHP中进行2-3个SQL查询并手动检查ID?
I suspect $_POST[id]
and $value
have the same value, and so you appear to be selecting the same column twice. 我怀疑$_POST[id]
和$value
具有相同的值,因此您似乎两次选择同一列。 This should be suppressed in code, so you only select it once - or you should be giving each an alias so this does not happen. 这应该在代码中取消显示,因此您只能选择一次-否则您应该给每个别名起一个别名,这样就不会发生。
I wouldn't recommend returning a resultset with column names starting with a number anyway - in some database systems that would not be permitted, unless it is quoted. 无论如何,我不建议返回带有以数字开头的列名的结果集-在某些数据库系统中,除非用引号引起来,否则这是不允许的。 Give them string prefixes as aliases, ending in _<number>
if you must. 给他们提供字符串前缀作为别名,如果必须的话,以_<number>
结尾。
Thus, your subselect string might look like this: 因此,您的子选择字符串可能如下所示:
"SELECT * FROM (
SELECT
NOW(),
USER(),
'J',
'{$_POST['id']}' AS val_1,
'{$value}' AS val_2
) WHERE ..."
More importantly, having $_POST[id]
in your code will open you up to SQL injection vulnerabilities - always escape data before using it. 更重要的是,在代码中包含$_POST[id]
将使您面临SQL注入漏洞的困扰-在使用数据之前,请务必先转义数据。 Even better, switch to MySQL PDO and use parameterisation. 更好的是,切换到MySQL PDO并使用参数化。
Lastly, the variable should be $_POST['id']
- PHP assumes that you meant a string index, but it will raise a warning if you skip the quotes. 最后,变量应为$_POST['id']
-PHP假定您的意思是字符串索引,但是如果您跳过引号,它将发出警告。 Turn on warnings so you can see mistakes like this. 打开警告,这样您就可以看到类似的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.