Hi guys,
I have the following code that I just cannot get to work!
Can you please let me know where I have gone wrong?
<?php
//db_connect.php
$con=mysqli_connect("localhost","root","password","database");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
function get_file_extension($file_name) {
return end(explode('.',$file_name));
}
function errors($error){
if (!empty($error))
{
$i = 0;
while ($i < count($error)){
$showError.= '<div class="msg-error">'.$error[$i].'</div>';
$i ++;}
return $showError;
}// close if empty errors
} // close function
if (isset($_POST['upfile'])){
// check feilds are not empty
if(get_file_extension($_FILES["uploaded"]["name"])!= 'csv')
{
$error[] = 'Only CSV files accepted!';
}
if (!$error){
$tot = 0;
$handle = fopen($_FILES["uploaded"]["tmp_name"], "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
for ($c=0; $c < 1; $c++) {
//only run if the first column if not equal to firstname
if($data[0] !='firstname'){
mysql_query("INSERT INTO company(
company_name,
address_street,
address_city,
address_province,
address_postalcode,
address_country,
main_tel,
second_tel,
main_email,
second_email
)VALUES(
'".mysql_real_escape_string($data[0])."',
'".mysql_real_escape_string($data[1])."',
'".mysql_real_escape_string($data[2])."',
'".mysql_real_escape_string($data[3])."'
'".mysql_real_escape_string($data[4])."'
'".mysql_real_escape_string($data[5])."'
'".mysql_real_escape_string($data[6])."'
'".mysql_real_escape_string($data[7])."'
'".mysql_real_escape_string($data[8])."'
'".mysql_real_escape_string($data[9])."'
'".mysql_real_escape_string($data[10])."'
)")or die(mysql_error());
}
$tot++;}
}
fclose($handle);
$content.= "<div class='success' id='message'> CSV File Imported, $tot records added </div>";
}// end no error
}//close if isset upfile
$er = errors($error);
$content.= <<<EOF;
<h3>Import CSV Data</h3>
$er;
<form enctype="multipart/form-data" action="" method="post">
File:<input name="uploaded" type="file" maxlength="20" /><input type="submit" name="upfile" value="Upload File">
</form>
EOF;
echo $content;