這幾天前都工作正常,但現在它不再寫數據到數據庫了,我不知道爲什麼。SQL/PHP插入不工作
我試圖查看從CSV文件獲取數據是否出錯,但是當我嘗試上載數據時,它已成功回顯,因此它不是代碼的一部分。
連接沒問題,因爲它沒有拋出'數據庫連接錯誤'。我有系統的其他部分仍然可以使用此連接。
我認爲這與表格本身在數據庫中有關,而不是PHP代碼。當INSERT停止工作時,我試着創建一個新的相同的表並寫入該表,而不是那個工作。
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>
表:
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
)
這是我第一次張貼在這裏,所以請讓我知道如果你需要任何進一步的信息來源
那麼,看看你的http服務器錯誤日誌文件。這是最可能的地方,你可以閱讀這個問題。 – arkascha
也有一些基本的錯誤檢查和處理將很好地... – arkascha