所有的mysqli命令对我来说都是新手,我已经研究了几个小时,但还没有找到解决我问题的答案。非常感谢任何帮助。
以下代码有效,但会在while语句中抛出布尔错误。
<?php
include 'dbc.php';
page_protect();
?>
<?php
$username = "***";
$password = "***";
$hostname = "***";
$database = "***";
if(isset($_FILES['uploaded_file'])) {
if($_FILES['uploaded_file']['error'] == 0) {
//connection to the database
$dbLink = new mysqli($hostname, $username, $password, $database);
if(mysqli_connect_errno($dbLink)) {
die("MySQL connection failed: ". mysqli_connect_error($dbLink));
}
$file = $_FILES ['uploaded_file']['tmp_name'];
$handle = fopen($file, "r");
$firstRow = fgetcsv($handle, 0, ",","'");
$datePosition = array_search("date", $firstRow, true);
$userPosition = array_search("user", $firstRow, true);
$volumePosition = array_search("volume", $firstRow, true);
$fatPosition = array_search("fat", $firstRow, true);
$proteinPosition = array_search("protein", $firstRow, true);
$lactosePosition = array_search("lactose", $firstRow, true);
$solidsPosition = array_search("solids", $firstRow, true);
$plate_countPosition = array_search("plate_count", $firstRow, true);
$coliformsPosition = array_search("coliforms", $firstRow, true);
$ecoliPosition = array_search("ecoli", $firstRow, true);
$uniquenumberPosition = array_search("uniquenumber", $firstRow, true);
if ($uniquenumberPosition == 0 && $datePosition == 1 && $userPosition == 2 && $volumePosition == 3 && $fatPosition == 4 && $proteinPosition == 5 && $lactosePosition == 6 && $solidsPosition == 7 && $plate_countPosition == 8)
{
}
else
{
echo '<center><img src="title-pic.jpg"><br><br><br>';
die("Did you choose a CSV file? If you did, the columns are not in the right order or the heading is mispelled. Fix the csv file and then try again...or upload a proper CSV file.");
}
echo '<center><img src="title-pic.jpg"><br><br><br>';
$row = 1;
while (($data = fgetcsv($handle, 0, ",","'")) !== FALSE)
{
if($row == 0)
{
// skip the first row
}
else
{
//csv format data like this
//$data[0] = uniquenumber
//$data[1] = date
//$data[2] = user
//$data[3] = volume
//$data[4] = fat
//$data[5] = protein
//$data[6] = lactose
//$data[7] = solids
//$data[8] = plate_count
//$data[9] = coliforms
//$data[10] = ecoli
//Get number of rows
$sql= 'SELECT * FROM laboratory';
$result=mysqli_query($dbLink, $sql);
if($result === FALSE) {
die(mysqli_error()); // TODO: better error handling
}
while($row=mysqli_fetch_assoc($result))
{
$un = $row['uniquenumber'];
if ($data[0] == $un)
{
echo $un;
$newdate = strtotime($data[1]);
$query = "
UPDATE laboratory SET uniquenumber = ".$data[0].", date = ".$newdate.", user = ".$data[2].", volume = ".$data[3].", fat = ".$data[4].", protein = ".$data[5].", lactose = ".$data[6].", solids = ".$data[7].", plate_count = ".$data[8].", coliforms = ".$data[9].", ecoli = ".$data[10].", official = 'N' WHERE uniquenumber = ".$un."";
$result = $dbLink->query($query);
}
}
}
$row++;
}
}
else {
echo '<center><img src="title-pic.jpg"><br><br><br>';
echo '<center><p>You did not choose a file to upload or you did not chose a proper CSV file.</p></center><br><br>';
}
}
else {
echo '<center><img src="title-pic.jpg"><br><br><br>';
echo 'Error! The file was not uploaded!';
}
echo '<center><p>Click <a href="uploads.php">here</a> to go back.</p></center>';
?>