-
Notifications
You must be signed in to change notification settings - Fork 16
/
updatedata.php
86 lines (84 loc) · 3 KB
/
updatedata.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
require_once "config.php";
session_name("staff");
session_start();
// Check if the user is logged in, if not then redirect him to login page
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
header("location: staff_login.php");
exit;
}
if(isset($_POST["submit"]))
{
if($_FILES['file']['name'])
{
$filename = explode(".", $_FILES['file']['name']);
if($filename[1] == 'csv')
{
$handle = fopen($_FILES['file']['tmp_name'], "r");
$count=0;
$count1=0;
while($data = fgetcsv($handle))
{
$item1 = mysqli_real_escape_string($conn, $data[0]);
$item2 = mysqli_real_escape_string($conn, $data[1]);
$item3 = mysqli_real_escape_string($conn, $data[2]);
//SET FOREIGN_KEY_CHECKS = 0;
//$query = "INSERT into marks(regdno, cgpa, backlogs) values('$item1','$item2','$item3')";
$query1="SELECT * from marks where regdno='$item1'";
$query = "UPDATE marks set cgpa='$item2',backlogs='$item3' WHERE regdno='$item1'";
try{
$result=mysqli_query($conn,$query1);
if(mysqli_num_rows($result)==1){
mysqli_query($conn, $query);
}
else{
throw new Exception("noupdate");
}
}
catch(Exception $php_errormsg){
$count=$count+1;
//echo "<br>Insertion error " .$count;
if($count==1){
echo "<script>alert('Some record are not updated as student regdno is not present');</script>";
}else{
$count=2;
}
}
$count1=$count1+1;
//SET FOREIGN_KEY_CHECKS = 1;
}
if($count==0){
fclose($handle);
//echo "Import is done";
echo "<script>alert('All records are updated');</script>";
}
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Allocate_amount</title>
<link type="text/css" rel="stylesheet" href="stylesheet.css">
</head>
<body>
<table><tr>
<td>
<a href="staff_access.php" class="button"><img src="back.jpg" alt="back" width="40" height="40"></a>
</td></tr></table><br><br>
<form method="post" enctype="multipart/form-data" action="#">
<table align="center">
<tr><td><br><br><br><br><br><br></td></tr>
<tr><td align="right"><h1>Updating </h1></td>
<td><h1>data in database:</h1></td></tr>
<tr><td>
<label for="file">Select CSV File:</label></td><td>
<input type="file" id="file" name="file" required><br></td></tr>
<tr><td><br><br></td></tr>
<tr><td></td><td><input type="submit" id="submit" value="submit" name="submit" style="display:none;">
<label for="submit"><img src="update1.jpg" alt="submit" width="80" height="30"></label></td></tr>
</table>
</form>
</body>
</html>