简体   繁体   中英

PHP/SQL insert a record if it not exists in that row on the db, form an scandir array

I tried it with if exists on sql but it says error.

if (isset($_POST['ReScan'])){
$dateien = 'dateien/';
$directoryscanarray = scandir($dateien);
print_r($directoryscanarray);

$arrlength = count($directoryscanarray);

for($x = 2; $x <  $arrlength; $x++) {
$foldername = $directoryscanarray[$x];
$sql = "INSERT INTO localfiles (foldername) VALUES ('$foldername')";

maybe:

WHERE NOT EXISTS SELECT foldername FROM localfiles WHERE name='$foldername'";

You can make the foldername an unique key and use

INSERT INTO ... ON DUPLICATE KEY UPDATE ...

Where on duplicate key you would just say the key equals itself

http://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html

INSERT ... ON DUPLICATE KEY (do nothing)

您可以尝试以下操作(未经测试):

$sql = " INSERT INTO localfiles (foldername) VALUES ('$foldername') ON DUPLICATE KEY UPDATE field_1="value_1", field_2="value_2" WHERE foldername='$foldername'";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM