Hi all I'm trying to make a form to insert data to a DB from a CSV file (I need it to let people working for me to update directly their own tables without sending me the files... and ask me to do it )
I have 2 problems:
- I had to add backslashes to the part of the code checking for the file type as I don't know what to put to limit the use of CSV files...
- the script work until the DELETE (I added data with myadmin in the table) but there is not INSERTION of the data coming from th CSV file...
this is the codeof the form
<form action="upload.php" method="post" enctype="multipart/form-data">
<FONT FACE="VERDANA" SIZE="+2" COLOR="DARKBLUE">
Seleziona il FILE da caricare:<BR>
<input type="file" name="upfile"><BR>
<BR>
<input type="hidden" name="MAX_FILE_SIZE" value="10000">
<input type="submit" value="Invia il file">
</form>
and this is the upload.php code
<?
// QUESTE RIGHE RENDONO LO SCRIPT COMPATIBILE CON LE VERSIONI
// DI PHP PRECEDENTI ALLA 4.1.0
if(!isset($_FILES)) $_FILES = $HTTP_POST_FILES;
if(!isset($_SERVER)) $_SERVER = $HTTP_SERVER_VARS;
/********************* VARIABILI DA SETTARE ********************/
// Directory dove salvare i files Uploadati ( chmod 777, percorso assoluto)
$upload_dir = $_SERVER["DOCUMENT_ROOT"] . "/upload";
// Eventuale nuovo nome da dare al file uploadato
$new_name = "";
// $allowed_types = array("excel/csv");
// Se $new_name è vuota, il nome sarà lo stesso del file uploadato
$file_name = ($new_name) ? $new_name : $_FILES["upfile"]["name"];
if(trim($_FILES["upfile"]["name"]) == "") {
die("Non hai indicato il file da uploadare !");
}
// if(@is_uploaded_file($_FILES["upfile"]["tmp_name"])) {
// if(!in_array($_FILES["upfile"]["type"],$allowed_types)) {
// die("Il file non è di un tipo consentito, sono ammessi solo i seguenti: " . implode(",", $allowed_types) . ".");
// echo "<a href=\"java script:history.go(-1);\">Torna indietro</a><br>";
// exit;
// }
// }
// e poi qui recuperi il file...elimini tutti i dati dalla tabella..
$_DATAFILE=file($_FILES[nome_campo_file]['tmp_name']);
$db = mysql_connect("localhost", "fan**mbo", "" ) or die("Problem connecting" );
mysql_select_db("fan***mbo_it_db",$db)or die("Problem selecting database" );
mysql_query("DELETE FROM prova");
while(list($key,$_VALUES)=each($DATAFILE)) {
$_PARAMS=split(",",$_VALUES);
//Qui aggiunge i vari parametri
mysql_query("INSERT INTO prova ('prova1', 'prova2') VALUES ('{$_PARAMS[0]}','{$_PARAMS[1]}')");
if (mysql_error()) echo "Errore in MySQL: " . mysql_error();
}
?>
what's wrong ???
thanx for your help !!!