0
我想通過php導入數據庫中的csv文件,但它不能正常工作。我不知道我在這裏誤認爲是我的代碼使用php mysql導入csv文件到數據庫
<?php
if (isset($_POST["submit"])) {
$host = "localhost"; // Host name.
$db_user = "root"; //mysql user
$db_password = ""; //mysql pass
$db = 'moodle'; // Database name.
$conn = mysql_connect($host, $db_user, $db_password) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
$filename = $_FILES["file"]["name"];
$ext = substr($filename, strrpos($filename, "."), (strlen($filename) - strrpos($filename, ".")));
//we check,file must be have csv extention
if ($ext == "csv") {
$file = fopen($filename, "r");
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE) {
$sql = "INSERT into mdl_user(username,email) values('$emapData[0]','$emapData[1]')";
mysql_query($sql);
}
fclose($file);
echo "CSV File has been successfully Imported.";
} else {
echo "Error: Please Upload only CSV File";
}
}
?>
這裏是表單代碼
<form enctype="multipart/form-data" method="post" action="import_csv.php">
<input type="file" name="file" id="file">
<input type="submit" value="submit">
</form>
我沒有找到我哪裏錯
誰能幫助我
感謝提前...
你有什麼錯誤? –
不,它沒有顯示任何錯誤too..can你請幫助我,我試圖從過去6小時..但我無法得到結果 – user200
mysql_ *函數從PHP 5.5.0起已棄用,並刪除PHP 7.0.0。切換您的代碼以使用[PDO](https://secure.php.net/manual/en/pdo.prepared-statements.php)或[mysqli](http://php.net/manual/en/mysqli。改爲quickstart.prepared-statements.php)。一定要使用準備好的語句/參數綁定,所以你不必擔心任何引用問題。 – aynber