1 回答
TA贡献1845条经验 获得超8个赞
这是因为您的“if ($conn->query($sql) === TRUE”)在 while 循环之外。
你在 while 循环中构建你的 $sql,但你只在 while 循环之后执行查询,因此你只插入最后一条记录。
//建议
<?php
$servername = "localhost";
$username = "root";
$password = "mypswrd";
$dbname = "mydb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$dir = "myFiles"; // directory path to files
// Open a directory, and read its contents
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
echo $file . "</br>";
$sql = "INSERT INTO myTable (fileName) VALUES ('$file')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
}
else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
closedir($dh);
}
}
$conn->close();
?>
- 1 回答
- 0 关注
- 72 浏览
添加回答
举报