我有一张学生详细信息表。我有两个csv文件,这些文件包含现有学生和新学生数据的更新信息。如果数据不在学生表中,这意味着学生离开了学校。
以下是我的代码,但在第二个文件中没有更新信息。 我附上我的代码。如果有人有简单的解决方案,请建议
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "student";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (isset($_POST['submit'])) {
$sql = "SELECT * FROM e_student";
$result_main = $conn->query($sql);
$filea = $_FILES['filea'];
$ext = pathinfo($_FILES['filea']['name'], PATHINFO_EXTENSION);
if ($ext == "csv" && $_FILES["filea"]["error"] == 0) {
$target = "upload/" . $_FILES["filea"]["name"];
move_uploaded_file($_FILES["filea"]["tmp_name"], $target);
if (($handle = fopen($target, "r")) !== FALSE) {
$array = $fields = array();
$i = 0;
while (($row = fgetcsv($handle, 4096)) !== false) {
if (empty($fields)) {
$fields = $row;
continue;
}
foreach ($row as $k => $value) {
$array[$i][$fields[$k]] = $value;
}
$i++;
}
fclose($handle);
}
}
$ext1 = pathinfo($_FILES['fileb']['name'], PATHINFO_EXTENSION);
if ($ext1 == "csv" && $_FILES["fileb"]["error"] == 0) {
$target1 = "upload/" . $_FILES["fileb"]["name"];
move_uploaded_file($_FILES["fileb"]["tmp_name"], $target1);
if (($handle = fopen($target1, "r")) !== FALSE) {
$array1 = $fields1 = array();
$j = 0;
while (($row1 = fgetcsv($handle, 4096)) !== false) {
if (empty($fields1)) {
$fields1 = $row1;
continue;
}
foreach ($row1 as $k => $value) {
$array1[$j][$fields1[$k]] = $value;
}
$j++;
}
fclose($handle);
}
}
foreach ($result_main as $rel) {
$clk = 'no';
foreach ($array as $arr) {
if(isset($arr1['SCD']))
{
if (strcmp(trim($rel['objId']), trim($arr['BCEID'])) == 0) {
$clk = 'yes';
$name = $arr['FirstName'];
$conditn = $rel['objId'];
$last_name = $arr['LegalSurname'];
$year_level = $arr['YearLevelName'];
$email = $arr['BCEEmail'];
$username = $arr['BCELogin'];
$StEnrollmentStatus = 1;
$sql1 = "UPDATE e_student SET Name = '$name',Lastname='$last_name',stYearLevel='$year_level',email='$email',username='$username' WHERE objId ='$conditn'";
$result = $conn->query($sql1);
if($result)
{
updateparent($rel['objId'], $array1,$conn);
}
}
}
}
if ($clk == 'no') {
$conditn = $rel['objId'];
$status = 0;
$pastoral = "Not Currently Enrolled";
$sql1 = "UPDATE e_student SET stEnrollmentStatus = '$status',stPastoral='$pastoral' WHERE objId ='$conditn'";
$result = $conn->query($sql1);
}
}
foreach ($array as $ar1) {
foreach ($result_main as $rel1) {
if (strcmp(trim($ar1['BCEID']), trim($rel1['objId'])) != 0) {
$name = $ar1['FirstName'];
$last_name = $ar1['LegalSurname'];
$year_level = $ar1['YearLevelName'];
$email = $ar1['BCEEmail'];
$objid = $ar1['BCEID'];
$username = $ar1['BCELogin'];
$StEnrollmentStatus = 1;
$sql3 = "INSERT INTO e_student(Name,Lastname,stYearLevel,email,objId,username,stEnrollmentStatus) VALUES('$name','$last_name','$year_level','$email','$objid','$username','$StEnrollmentStatus')";
$result1 = $conn->query($sql3);
if($result1)
{
updateparent1($objid, $array1,$conn);
}
}
}
}
echo "<h3><center>SUCCESSFULLY UPDATED</center></h3>";
}
function updateparent($objid, $array1,$conn) {
foreach ($array1 as $arr1) {
if(isset($arr1['SCD']))
{
if (strcmp(trim($objid), trim($arr1['SCD'])) == 0) {
$parent = $arr1['PTI1'] . $arr1['PFN1'] . $arr1['PSN1'];
echo $parent;
$email = $arr1['PEM1'];
}
$sql2 = "UPDATE e_student SET stParentName = '$parent',stParentEmail='$email' WHERE objId ='$objid'";
$result = $conn->query($sql2);
}
}
}
function updateparent1($objid, $array1,$conn) {
foreach ($array1 as $arr1) {
if(isset($arr1['SCD']))
{
echo $arr1['SCD'];
if (strcmp(trim($objid), trim($arr1['SCD'])) == 0) {
$parent = $arr1['PTI1'] . $arr1['PFN1'] . $arr1['PSN1'];
$email = $arr1['PEM1'];
}
$sql2 = "UPDATE e_student SET stParentName = '$parent',stParentEmail='$email' WHERE objId ='$objid'";
$result = $conn->query($sql2);
}
}
}
?>