What I'm trying to do is read a .txt file and save each of the rows and columns in their respective fields in MySQL. The problem is that it is not saved in the BD. So I have the text in the file:
NOMBRE,EDAD,SEXO
WALTHER,28,M
MARTHE,26,F
JULIO,28,M
MARIA,25,F
ESTEBAN,24,M
and so I have my code:
<html>
<head>
<title>Leer archivo</title>
</head>
<body>
<table>
<?php
$f = fopen("archivo.txt", "r") or exit("no se puede leer archivo!");
$arr_to_insert = array();
while (!feof($f)) {
$arrM = explode(',',fgets($f));
echo '<tr><td name="NOMBRE">' . $arrM[0] . '</td><td name="EDAD">' . $arrM[1] . '</td><td name="SEXO">' . $arrM[2] . '</td> </tr>';
$arr_to_insert[] = $arrM;
}
fclose($f);
if (isset($_POST['submit'])) {
include 'conexion.php';
foreach($arr_to_insert as $ai){
$sql="INSERT INTO ninguno (NOMBRE, EDAD, SEXO) VALUES ('{$ai[0]}','{$ai[1]}','{$ai[2]}')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error());
}
}
mysqli_close($con);
}
?>
</table>
</form>
</body>
</html>