Hello im using plain php to make a simple excel import to database, the values wont import. This is my code:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Upload Excel File</title>
</head>
<body>
<h1>Upload Excel File</h1>
<form method="post" action="upload.php" enctype="multipart/form-data">
<input type="file" name="file">
<br><br>
<input type="submit" value="Upload">
</form>
</body>
</html>
And this is the php:
<?php
require('db_config.php');
// Check if a file was uploaded
if (isset($_FILES["file"]) && $_FILES["file"]["error"] == 0) {
// Define allowed file extensions
$allowedExtensions = array("xlsx", "xls");
// Get file extension
$extension = pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION);
// Check if the file extension is allowed
if (in_array($extension, $allowedExtensions)) {
// Define the path to save the uploaded file
$filePath = "uploads/" . $_FILES["file"]["name"];
// Move the uploaded file to the defined path
move_uploaded_file($_FILES["file"]["tmp_name"], $filePath);
// Open the uploaded Excel file
$objPHPExcel = PHPExcel_IOFactory::load($filePath);
// Get the first worksheet in the Excel file
$worksheet = $objPHPExcel->getActiveSheet();
// Loop through the rows in the worksheet
foreach ($worksheet->getRowIterator() as $row) {
// Get the cell values in the current row
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
$cells = array();
foreach ($cellIterator as $cell) {
$cells[] = $cell->getValue();
}
// Insert the row data into the database
$sql = "INSERT INTO leaderboard (AFFILIATOR_NAME, TOP_N, USER_ID,USERNAME,WAGERED) VALUES ('".$cells[1]."', '".$cells[2]."', '".$cells[3]."','".$cells[4]."','".$cells[5]."')";
// Execute the SQL statement
// ...
}
// Delete the uploaded Excel file
unlink($filePath);
// Redirect the user to the success page
//header("Location: success.php");
echo 'Values inserted successfully!';
//exit;
} else {
// Invalid file extension
//header("Location: error.php");
echo 'Values failed to insert!';
/// exit;
}
} else {
// No file uploaded
echo 'No file uploaded!';
}
?>