I'm trying to insert CSV file into MySql but I'm getting an error.
Warning: mysql_real_escape_string() expects parameter 1 to be string,
My database table is:
:s_no :name :id :
:1 :Lim :678 :
:2 :Mary :623 :
:3 :Mimi :4124 :
The number is AUTO_INCREMENT, I wanted instead the Name And ID only.
<?php
$connect = mysql_connect("localhost","root","","skpj");
if(isset($_POST["submit"]))
{
if($_FILES['file']['name'])
{
$filename = explode('.',$_FILES['file']['name']);
if ($filename[1] == 'csv')
{
$handle = fopen($_FILES['file']['tmp_name'], "r");
while ($data = fgetcsv($handle))
{
$item1 = mysql_real_escape_string($connect, $data[0]);
$item2 = mysql_real_escape_string($connect, $data[1]);
$sql="INSERT into student( s_no, name, id) value ('','$item1','$item2')";
mysql_query($connect, $sql);
}
fclose($handle);
print "Done";
}
}
}
?>
<html>
<head></head>
<body>
<form method="post" enctype="multipart/form-data">
<div>
<p>Upload CSV: <input type="file" name="file" /></p>
<p><input type="submit" name="submit" value="Import" /></p>
</div>
</form>
</body>
</html>