-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewedit.php
More file actions
173 lines (141 loc) · 4.56 KB
/
newedit.php
File metadata and controls
173 lines (141 loc) · 4.56 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<html>
<head>
<title></title>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "demo_form";
$con="";
$Name="";
$Roll = substr($_SERVER['REQUEST_URI'],-9);
$Dept = "";
$Email = "";
$Address = "";
$About = "";
$message = "";
$name_error = "";
$roll_error = "";
$email_error = "";
$result = "";
$row = "";
// Create connection
$con = mysqli_connect($servername, $username, $password);
// Check connection
if (!$con) {
$message = "Not connected to server";
}
//echo "Connected successfully";
if(!mysqli_select_db($con,$dbname))
{
$message = "Database not selected";
}
$sql_get = "SELECT * FROM student WHERE Roll_Number = ".$Roll;
if($result = mysqli_query($con,$sql_get))
{
while($row = mysqli_fetch_row($result)){
$Name = $row[0];
$Dept = $row[2];
$Email = $row[3];
$Address = $row[4];
$About = $row[5];
}
if(empty($Name)){
$details = "Sorry! No match found";
}
else{
$details = " Name : ".$Name."\n Roll number : ".$Roll."\n Department : ".$Dept."\n Email : ".$Email
."\n Address : ".$Address."\n About : ".$About;
}
mysqli_free_result($result);
}
if(isset($_POST["submit"])){
//Create a function to validate the input data
function test_input($link,$data){
$data = trim($data);
$data = stripslashes($data);
$data = strip_tags($data);
$data = htmlspecialchars($data);
$data = mysqli_real_escape_string($link,$data);
return $data;
}
if(empty($_POST["name"])){
$name_error = "*Name is required.";
$message = "Data not inserted.";
}
else{
$Name = test_input($con,$_POST['name']);
if(!preg_match("/^[a-zA-Z ]*$/",$Name)){
$name_error = "Only letters and white spaces allowed.";
$message = "Data not inserted.";
}
}
$Dept = test_input($con,$_POST['dept']);
$Email = test_input($con,$_POST['email']);
if(!empty($Email)){
if(!filter_var($Email,FILTER_VALIDATE_EMAIL)){
$email_error = "Invalid email format.";
$message = "Data not inserted.";
}
$email_end = "@nitt.edu"; //email should end with @nitt.edu
$pos = stripos($Email,$email_end);
$email_length = strlen($Email);
if(($email_length-$pos)===9){
$email_error = "";
}
else{
$email_error = "Email should end with @nitt.edu";
$message = "Data not inserted.";
}
}
$Address = test_input($con,$_POST['address']);
$About = test_input($con,$_POST['about']);
if(empty($message)){
$sql_update = "UPDATE ".$dbname.".student SET Name = '".$Name."' ,Department = '".$Dept."' ,Email = '".$Email."' "
.",Address = '".$Address."' ,About = '".$About."'"." WHERE Roll_Number=".$Roll;
if(!mysqli_query($con,$sql_update)){
$message = "Data not Updated.";
}
else
{
$message = "Data successfully Updated!";
}
}
}
?>
<form method="post" name ="editForm" action=" <?php echo ("http://".$_SERVER["HTTP_HOST"].$_SERVER['REQUEST_URI']);?> ">
<p>Name* :
<input type="text" name="name" value="<?php echo $Name;?>">
<span class="error"><?php echo $name_error;?></span>
</p>
<p>Roll Number* : <input type="text" name="roll" value="<?php echo $Roll;?>" readonly></input>
<span class = "error"><?php echo $roll_error;?></span>
</p>
<p>Department :
<select name="dept">
<option value="archi" <?php if($Dept == "archi") echo "selected";?> >ARCHI</option>
<option value="civil" <?php if($Dept == "civil") echo "selected";?> >CIVIL</option>
<option value="cse" <?php if($Dept == "cse") echo "selected";?> >CSE</option>
<option value="eee" <?php if($Dept == "eee") echo "selected";?> >EEE</option>
<option value="ece" <?php if($Dept == "ece") echo "selected";?> >ECE</option>
<option value="ice" <?php if($Dept == "ice") echo "selected";?> >ICE</option>
<option value="mech" <?php if($Dept == "mech") echo "selected";?> >MECH</option>
<option value="meta" <?php if($Dept == "meta") echo "selected";?> >META</option>
<option value="prod" <?php if($Dept == "prod") echo "selected";?> >PROD</option>
</select>
</p>
<p>Email Address : <input type="text" name="email" value="<?php echo $Email;?>" >
<span class = "error"><?php echo $email_error;?></span>
</p>
<p>Physical Address : <input type="text" name="address" value="<?php echo $Address;?>" ></p>
<p>About Me : <br><textarea name ="about" rows="5" cols="30" ><?php echo $About; ?></textarea></p>
<input type="submit" name = "submit" value="Submit"/>
</form>
<?php echo $message?>
<form method="post" action=<?php echo "http://".$_SERVER['HTTP_HOST']."/viewStudent.php" ?>>
<input type="submit" name = "submit" value="VIEW STUDENT"/>
</form>
</body>
</html>