just doing an INSERT using mySQL which works fine...
只是使用mySQL进行INSERT工作正常...
mysql_query("INSERT INTO FLIGHTS_AVAILABLE
(aircraftID, aircraftType, maxSeats) VALUES('$theaircraftID', '$addType', '$maxCapacity' ) ")
or die(mysql_error());
echo '<p>';
echo "The following details were added into the database:";
echo "<hr>";
echo $theaircraftID . $addType . $maxCapacity;
This works fine, however I've looked online and whilst I have heard of a feature called IF NOT EXISTS, there are not very good explanations online about it's use.
这工作正常,但我看过网上,虽然我听说过一个名为IF NOT EXISTS的功能,但在网上对它的使用并没有很好的解释。
Is there anyway, I can run this query above, providing that if for example $theaircraftID = 10, and there is already a row in the database where the aircraftID = 10, then the query would not be run?
无论如何,我可以在上面运行这个查询,假设如果例如$ theaircraftID = 10,并且数据库中已经有一行,其中aircraftID = 10,那么查询将不会运行?
Thanks for any help you may be able to suggest,
感谢您提供的任何帮助,
Tom
汤姆
2 个解决方案
#1
3
Add a UNIQUE INDEX
on 'FLIGHTS_AVAILABLE'.'aircraftID'
. MySQL will block additional INSERT
s with the same value. Additionally you can use ON DUPLICATE KEY EXISTS
to run an UPDATE
incase the INSERT
give a DUPLICATE KEY
constraint error.
在'FLIGHTS_AVAILABLE'上添加UNIQUE INDEX。'aircraftID'。 MySQL将阻止具有相同值的其他INSERT。此外,您可以使用ON DUPLICATE KEY EXISTS运行UPDATE,INSERT提供DUPLICATE KEY约束错误。
#2
0
You can try to create unique index by required columns and then use INSERT IGNORE and mysql_affected_rows.
您可以尝试按必需列创建唯一索引,然后使用INSERT IGNORE和mysql_affected_rows。
#1
3
Add a UNIQUE INDEX
on 'FLIGHTS_AVAILABLE'.'aircraftID'
. MySQL will block additional INSERT
s with the same value. Additionally you can use ON DUPLICATE KEY EXISTS
to run an UPDATE
incase the INSERT
give a DUPLICATE KEY
constraint error.
在'FLIGHTS_AVAILABLE'上添加UNIQUE INDEX。'aircraftID'。 MySQL将阻止具有相同值的其他INSERT。此外,您可以使用ON DUPLICATE KEY EXISTS运行UPDATE,INSERT提供DUPLICATE KEY约束错误。
#2
0
You can try to create unique index by required columns and then use INSERT IGNORE and mysql_affected_rows.
您可以尝试按必需列创建唯一索引,然后使用INSERT IGNORE和mysql_affected_rows。