我正在尝试使用 php 将 csv 的联系人数据上传到 mysql 数据库 phbook,但是当我通过 phpmyadmin 检查或打印数据库的数据时.. 我什么也没看到,或者说表是空的,
我使用的代码如下;
<?php
/* conenction to DB */
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("phbook", $con);
/* connection ends*/
if ( isset( $_FILES['userfile'] ) )
{
$csv_file = $_FILES['userfile']['tmp_name'];
if ( ! is_file( $csv_file ) )
exit('File not found.');
$sql = '';
if (($handle = fopen( $csv_file, "r")) !== FALSE)
{
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$sql .= "INSERT INTO `contact` SET
`fname` = '$data[0]',
`lname` = '$data[1]',
`phone` = '$data[2]',
`mob` = '$data[3]',
`email` = '$data[4]';
";
}
fclose($handle);
}
// Insert into database
//exit( $sql );
exit( "Complete!" );
}
mysql_close($con);
?>
<!DOCTYPE html>
<html>
<head>
<title>CSV to MySQL Via PHP</title>
</head>
<body>
<form enctype="multipart/form-data" method="POST">
<input name="userfile" type="file">
<input type="submit" value="Upload">
</form>
</body>
</html>