This was all working fine a few days ago but now it isn't writing the data to the database anymore and I can't figure out why.
I tried to see if it was an error with getting the data from the CSV file but when I try and upload the data it is successfully echoed back, so it's not that part of the code.
The connection is fine as it's not throwing the 'database connection error'. I have other parts of the system that still work OK with this connection.
I think it is something to do with the table itself in the database rather than the PHP code though. When the INSERT stopped working I tried creating a new identical table and writing to that instead but that didnt work either.
PHP:
<?php
//connect to the database
$server = 'xx';
$connectionInfo = array(xx);
$connection = sqlsrv_connect($server, $connectionInfo);
if (!$connection)
{
die('Database connection error');
}
session_start();
if (isset($_FILES['csv']['size']) == 0)
{
echo "<p2>". "Hello, ". $_SESSION['name']. ". You can upload your CSV file for your school, ". $_SESSION['school']. ", below."."</p2>";
$school = $_SESSION['school'];
}
if (isset($_FILES['csv']['size']) > 0) {
//get the csv file
$file = $_FILES['csv']['tmp_name'];
$handle = fopen($file,"r");
//loop through the csv file and insert into database
do {
if (isset($data[0]))
{
$school = $_SESSION['school'];
$species = $data[0];
$count = $data[1];
$surveyor = $_SESSION['name'];
date_default_timezone_set('Europe/London');
$upload_date = date('d/m/y h:i:s a', time());
echo "<p2>". $school. " ". $species. " ". $count. " ". $surveyor. " ". $upload_date. "<br>". "</p2>";
$parameters = array($school, $species, $count, $surveyor, $upload_date);
$Query = "INSERT INTO findings(School, Species, Count, Surveyor, Upload_Date) VALUES (?,?,?,?,?)";
$results = sqlsrv_query($connection, $Query, $parameters);
}
} while ($data = fgetcsv($handle,1000,",","'"));
echo "<p2>". "Upload complete". "</p2>". "<br>";
}
?>
<body>
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
<br />
<input name="csv" type="file" id="csv" accept=".csv" />
<input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>
Table:
CREATE TABLE [dbo].[findings]
(
[School] VARCHAR(50) NOT NULL,
[Species] VARCHAR(50) NOT NULL,
[Count] INT NOT NULL,
[Surveyor] VARCHAR(50) NOT NULL,
[Upload_Date] DATETIME NOT NULL
)
This is my first time posting on here so please do let me know if you need any further infomation
You need to prepare the SQL query before you can run it multiple times, your code appears to be executing the query multiple times as it is in a do while loop..
Some of your params need to be passed by reference ex. $species, &count
// Set up the proc params array - be sure to pass the param by reference
$parameters = array($school,
&$species,
&$count, ..., ..., ...
);
Do something like this outside your do while...
$stmt = sqlsrv_prepare( $connection, $Query, $parameters);
if( !$stmt ) {
die( print_r( sqlsrv_errors(), true));
}
Inside the loop ...
re asign referenced vars
$species = $data[0];
$count = $data[1];
if(!sqlsrv_execute( $stmt )) {
die( print_r( sqlsrv_errors(), true));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With