salve a tutti, oggi lavorando su uno script che permette di caricare file csv e aggiornare i dati contemporaneamente ho riscontrato un problema che non capisco, anche dopo molte ore di prova non so come fare e chiedo l'aiuto degli esperti:
lo script in oggetto è questo:
<?php
##CSV to MYSQL made by Paul Mcilwaine
##Please feel free to distribute and modify this program, all i ask is that you notify me of
##updated code.
error_reporting(E_ALL);
$config['host'] = 'localhost'; //host name
$config['user'] = '*';//database username
$config['pass'] = '*';//database password
mysql_connect($config['host'],$config['user'],$config['pass']);
session_start();
if (!isset($_SESSION['page']) || !isset($_GET['page'])) {
$_SESSION['page'] = 1;
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<form action="?page=2" method="post">
<tr>
<td colspan="2"><div align="center"><font size="+2" face="Verdana, Arial, Helvetica, sans-serif">Select the Database</font></div></td>
</tr>
<tr>
<td>Database: </td>
<td><select name="database">
<?php
$query = mysql_query("SHOW DATABASES");
while($show = mysql_fetch_array($query)) {
echo '<option value="'.$show['Database'].'">'.$show['Database'].'</option>';
}
?>
</select></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value=" Submit "></td>
</tr>
</form>
</table>
<?php
} else {
switch($_GET['page']) {
case '2':
if (!isset($_SESSION['page']) && $_SESSION['page'] != 1) {
die('An Error has occured');
} else {
$_SESSION['page'] = 2;
$_SESSION['dbase'] = $_POST['database'];
mysql_select_db($_SESSION['dbase']);
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<form action="?page=3" method="post">
<tr>
<td colspan="2"><div align="center"><font size="+2" face="Verdana, Arial, Helvetica, sans-serif">Select
Table From <?php echo $_SESSION['dbase']; ?></font></div></td>
</tr>
<tr>
<td>Tables: </td>
<td><select name="table">
<?php
$query = mysql_query("SHOW TABLES");
while($r = mysql_fetch_array($query)) {
echo '<option value="'.$r['Tables_in_'.$_SESSION['dbase'].''].'">'.$r['Tables_in_'.$_SESSION['dbase'].''].'</option>';
}
?>
</select></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value=" Submit "></td>
</tr>
</form>
</table>
<?php
}
break;
case '3':
if (!isset($_SESSION['page'])) {
header("location: ".basename($_SERVER['PHP_SELF']));
}
$_SESSION['page'] = 3;
$_SESSION['table'] = $_POST['table'];
?>
<table width="77%" border="0" cellspacing="0" cellpadding="3" align="center">
<form action="?page=4" method="post" enctype="multipart/form-data">
<tr>
<td colspan="2"><div align="center"><font size="+2" face="Verdana, Arial, Helvetica, sans-serif">Upload
CSV File</font></div></td>
</tr>
<tr>
<td width="22%">File</td>
<td width="78%"><input type="file" name="csvfile"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value=" Submit "></td>
</tr>
</form>
</table>
<?php
break;
case '4':
if (!isset($_SESSION['page'])) {
header("location: ".basename($_SERVER['PHP_SELF']));
}
$_SESSION['page'] = 4;
$ext = explode(".",$_FILES['csvfile']['name']);
$num = count($ext)-1;
if ($ext[$num] != "csv") {
die ('Please upload CSV Files only');
} else {
if ($_FILES['csvfile']['error'] != 0) {
die ('File Not Uploaded');
} else {
$_SESSION['filename'] = $_FILES['csvfile']['name'];
$move = move_uploaded_file($_FILES['csvfile']['tmp_name'],$_SERVER['DOCUMENT_ROOT'].''.dirname($_SERVER['PHP_SELF'])."/".$_SESSION['filename']);
if (!$move) {
die ('File Didnt Upload');
} else {
echo 'file uploaded Click here to add CSV to MySQL';
}
}
}
break;
case '5':
if (!isset($_SESSION['page']) || !isset($_SESSION['dbase']) || !isset($_SESSION['table'])) {
header("location: ".basename($_SERVER['PHP_SELF']));
}
$_SESSION['page'] = 5;
mysql_select_db($_SESSION['dbase']);
$show = mysql_query("SHOW FIELDS FROM ".$_SESSION['table']."");
echo "TABLE: ".$_SESSION['table']."
";
$feildsnum = 0;
while($do = mysql_fetch_array($show)) {
$feild[$do['Field']] = $do['Field'];
$feildsnum++;
}
$numSQL = 1;
$sqlKey = null;
foreach ($feild as $key ) {
if ($numSQL == $feildsnum) {
$sqlKey .= $key;
} else {
$sqlKey .= $key.", ";
}
$numSQL++;
}
$fp = fopen($_SESSION['filename'],"r");
$contents = fread($fp, filesize("dati.csv"));
$newLine = explode("\n",$contents);
$rows = 1;
foreach ($newLine as $key => $value) {
$rows++;
if (empty($value)) {
} else {
$feilds = explode(",",$value);
$num = count($feilds);
if ($num > $feildsnum) {
die ('Error: There are too many feilds in the CSV File.');
break;
} else {
if ($num < $feildsnum) {
$values = null;
$i = 1;
foreach ($feilds as $key => $v) {
if ($i == $num) {
$values .= "'".$v."'";
} else {
$values .= "'".$v."',";
}
$i++;
}
mysql_query("INSERT INTO ".$_SESSION['table']."
($sqlKey)
VALUES($values)
")or die(mysql_error() ."
".$rows." ".__LINE__);
} else {
$values = null;
$i = 1;
foreach ($feilds as $key => $v) {
if ($i == $num) {
$values .= "'".$v."'";
} else {
$values .= "'".$v."',";
}
$i++;
}
mysql_query("INSERT INTO ".$_SESSION['table']."
($sqlKey)
VALUES($values)
")or die(mysql_error() ."
".$rows." ".__LINE__);
}
}
}
}
echo 'All '.($rows-2).'Records Added to '.$_SESSION['table'];
unset($_SESSION['table']);
unset($_SESSION['dbase']);
unset($_SESSION['page']);
break;
}
}
?>
va tutto bene fino al punto evidenziato che credo aggiorni i dati nella tabella sql.
e mi restituisce a schermo questo:
TABLE: FILE_TOT
Column count doesn't match value count at row 1
2 202
la tabella si chiama FILE_TOT
e dentro ci sono questi record :
ID , CATEGORIA , DESCRIZIONE , ESTENSIONE , DIMENSIONE_KB , DATA , ORA
vi ringrazio ancora e spero possiate aiutarmi.