· 6 years ago · Sep 22, 2019, 06:36 AM
1<?php
2//connect to database
3$db= new mysqli('localhost','root','','assignment1');
4 if (mysqli_connect_error())
5 {//display the details of any connection errors
6 echo 'Error connecting to database:<br/>'.mysqli_connect_error();
7 exit;
8 }
9 session_start();
10 if ( !isset( $_SESSION['volunteermail'] ))
11 {
12 header('Location: login.php');
13 exit;
14 }
15 //process submitted form
16 if (isset($_POST['Address1']))
17 {
18 //create short variable names from the data received from the form
19 $Address1 = $_POST['Address1'];
20 $Address2 = $_POST['Address2'];
21 $Suburb = $_POST['Suburb'];
22 $Postcode = $_POST['Postcode'];
23 $PhoneNo = $_POST['PhoneNo'];
24 $password = $_POST['password'];
25 $confirmPassword = $_POST['confirmPassword'];
26
27 //we create this variable and set it to an empty string... if it remains empty by the end
28 //of our validation code, then there was no error found
29 $error_message='';
30 //first we'll check if any of our required fields are empty all at once
31 if (empty($Address1)||empty($Address2)||empty($Suburb)||empty($Postcode)||empty($PhoneNo)||empty($password)||empty($confirmPassword))
32 {
33 $error_message='One of the required values was blank.';
34 }
35 //check postcode must have 4 digit only
36 elseif (strlen($Postcode)!=4)
37 {
38 $error_message='Your postcode must have 4 digit';
39 }
40 //now we'll check if the phone number is not numberic
41 elseif (!is_numeric($PhoneNo))
42 {
43 $error_message='Your phone n0 is not numberic.';
44 }
45 //check the phone number must have 11 digit only based on the australia phoneNo standard
46 elseif (strlen($PhoneNo)>10)
47 {
48 $error_message='Your Phone Number must have 10 digit or below';
49 }
50 //now we'll check if the password is long enough
51 elseif (strlen($password)<5)
52 {
53 $error_message='your password is not long enough, must be 5 and above';
54 }
55 //check confirmPassword are matching with the origin password or not
56 elseif ($password != $confirmPassword)
57 {
58 $error_message='your password do not match.';
59 }
60
61 //now we'll check if the email address already exists in the database
62 //$email_query= "SELECT email FROM volunteers WHERE email='".$emailaddress."' AND vol_time_id !=" .$_GET['edit_mailid'];
63 //$email_results=$db->query($email_query);
64
65 //if ($email_results-> num_rows > 0)
66 //{
67 // $error_message='Your email address already exist, choose another.';
68
69 //}
70
71
72
73 //if the error message variable is not empty(i.e. an error has been found)
74 if($error_message!='')
75 {
76 //we'll just provide the user with the error message and a back link if there is an error
77 //the exit command tells the server/PHP to stop processing the script at that point
78 echo 'error:'.$error_message.'<a href="javascript: history.back();">go back</a>.';
79 echo '</body></html>';
80 exit;
81 }
82 else
83 {
84 $query = "UPDATE volunteers SET address_In_1='".$Address1."', address_In_2='".$Address2."', suburb='".$Suburb."',poscode='".$Postcode."',
85 PhoneNo='".$PhoneNo."',Password='".$password."',Confirm_password='".$confirmPassword."'
86 WHERE email='".$_SESSION['volunteermail']."'";
87
88 $result= $db->query($query);
89
90 if ($result)
91 {
92 echo'<p>User details inserted into database!</p><a href="javascript: history.back();">go back</a>.';
93 }
94 else
95 {
96 echo'<p>Error updating details. Error message:</p>';
97 echo '<p>'.$db->error.'</p>';
98 }
99
100 }
101}
102
103 //fetch the user's details and store them in $rows
104 $query= 'SELECT * FROM volunteers WHERE email= "'. $_SESSION['volunteermail'].'"' ;
105 $result = $db->query($query);
106 $row=$result->fetch_assoc();
107
108
109?>
110<!DOCTYPE html>
111<html>
112<head>
113 <title>Edit User Form</title>
114 </head>
115
116<body>
117<h2><strong>User Details</strong></h2>
118<form name="EditUserForm" method="post" action="editUsers.php">
119 <table style="width: 500px; border: 0px;" cellspacing="1" cellpadding="1">
120 <tr>
121 <td colspan="2"><strong>Personal Details</strong></td>
122 </tr>
123 <tr style="background-color: #FFFFFF;">
124 <td>Address1</td>
125 <td>
126 <textarea name="Address1" cols="30" rows="4"><?php echo $row['address_In_1'] ?></textarea>*</td>
127 </tr>
128 <tr style="background-color: #FFFFFF;">
129 <td>Address2</td>
130 <td>
131 <textarea name="Address2" cols="30" rows="4"><?php echo $row['address_In_2'] ?></textarea>
132 </td>
133 </tr>
134 <tr style="background-color: #FFFFFF;">
135 <td>Suburb</td>
136 <td>
137 <input value="<?php echo $row['suburb'] ?>" name="Suburb" type="text" style="width: 200px;" maxlength="100" />*</td>
138 </tr><tr style="background-color: #FFFFFF;">
139 <td>Postcode</td>
140 <td>
141 <input value="<?php echo $row['poscode'] ?>" name="Postcode" type="text" style="width: 200px;" maxlength="100" />*</td>
142 </tr>
143 <tr style="background-color: #FFFFFF;">
144 <td>PhoneNo</td>
145 <td>
146 <input value="<?php echo $row['PhoneNo'] ?>" name="PhoneNo" type="text" style="width: 150px;" maxlength="15" />*</td>
147 </tr>
148 <tr>
149 <td colspan="2"> </td>
150 </tr>
151 <tr style="background-color: #FFFFFF;">
152 <td>Password</td>
153 <td>
154 <input value="<?php echo $row['Password'] ?>" name="password" type="password" style="width: 200px;" maxlength="20" />*</td>
155 </tr>
156 <tr style="background-color: #FFFFFF;">
157 <td>Confirm Password</td>
158 <td>
159 <input value="<?php echo $row['Confirm_password'] ?>" name="confirmPassword" type="password" style="width: 200px;" maxlength="20" />*</td>
160 </tr>
161 <tr>
162 <td colspan="2"> </td>
163 </tr>
164 <tr style="background-color: #FFFFFF;">
165 <td>
166 <input type="reset" name="reset" value="Reset" />
167 <input type="submit" name="submit" value="Submit" /></td>
168 <td>
169 <div align="right">* indicates required field</div></td>
170 </tr>
171 </table>
172</form>
173</body>
174</html>