· 9 years ago · May 04, 2017, 01:08 AM
1//Form Validation
2<!DOCTYPE HTML>
3<html>
4<head>
5<style>
6.error {color: #FF0000;}
7</style>
8</head>
9<body>
10
11<?php
12// define variables and set to empty values
13$nameErr = $emailErr = $genderErr = $websiteErr = "";
14$name = $email = $gender = $comment = $website = "";
15
16if ($_SERVER["REQUEST_METHOD"] == "POST") {
17 if (empty($_POST["name"])) {
18 $nameErr = "Name is required";
19 } else {
20 $name = test_input($_POST["name"]);
21 // check if name only contains letters and whitespace
22 if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
23 $nameErr = "Only letters and white space allowed";
24 }
25 }
26
27 if (empty($_POST["email"])) {
28 $emailErr = "Email is required";
29 } else {
30 $email = test_input($_POST["email"]);
31 // check if e-mail address is well-formed
32 if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
33 $emailErr = "Invalid email format";
34 }
35 }
36
37 if (empty($_POST["website"])) {
38 $website = "";
39 } else {
40 $website = test_input($_POST["website"]);
41 // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
42 if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
43 $websiteErr = "Invalid URL";
44 }
45 }
46
47 if (empty($_POST["comment"])) {
48 $comment = "";
49 } else {
50 $comment = test_input($_POST["comment"]);
51 }
52
53 if (empty($_POST["gender"])) {
54 $genderErr = "Gender is required";
55 } else {
56 $gender = test_input($_POST["gender"]);
57 }
58}
59
60function test_input($data) {
61 $data = trim($data);
62 $data = stripslashes($data);
63 $data = htmlspecialchars($data);
64 return $data;
65}
66?>
67
68<h2>PHP Form Validation Example</h2>
69<p><span class="error">* required field.</span></p>
70<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
71 Name: <input type="text" name="name" value="<?php echo $name;?>">
72 <span class="error">* <?php echo $nameErr;?></span>
73 <br><br>
74 E-mail: <input type="text" name="email" value="<?php echo $email;?>">
75 <span class="error">* <?php echo $emailErr;?></span>
76 <br><br>
77 Website: <input type="text" name="website" value="<?php echo $website;?>">
78 <span class="error"><?php echo $websiteErr;?></span>
79 <br><br>
80 Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
81 <br><br>
82 Gender:
83 <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">Female
84 <input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">Male
85 <span class="error">* <?php echo $genderErr;?></span>
86 <br><br>
87 <input type="submit" name="submit" value="Submit">
88</form>
89
90<?php
91echo "<h2>Your Input:</h2>";
92echo $name;
93echo "<br>";
94echo $email;
95echo "<br>";
96echo $website;
97echo "<br>";
98echo $comment;
99echo "<br>";
100echo $gender;
101?>
102
103</body>
104</html>
105
106
107
108
109
110---------------------------------------------------------
111//FILES
112
113
114file_uploads = On
115
116HTML FILE
117<!DOCTYPE html>
118<html>
119<body>
120
121<form action="upload.php" method="post" enctype="multipart/form-data">
122 Select image to upload:
123 <input type="file" name="fileToUpload" id="fileToUpload">
124 <input type="submit" value="Upload Image" name="submit">
125</form>
126
127</body>
128</html>
129
130
131COMPLETE PHP FILE
132
133<?php
134$target_dir = "uploads/";
135$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
136$uploadOk = 1;
137$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
138// Check if image file is a actual image or fake image
139if(isset($_POST["submit"])) {
140 $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
141 if($check !== false) {
142 echo "File is an image - " . $check["mime"] . ".";
143 $uploadOk = 1;
144 } else {
145 echo "File is not an image.";
146 $uploadOk = 0;
147 }
148}
149// Check if file already exists
150if (file_exists($target_file)) {
151 echo "Sorry, file already exists.";
152 $uploadOk = 0;
153}
154// Check file size
155if ($_FILES["fileToUpload"]["size"] > 500000) {
156 echo "Sorry, your file is too large.";
157 $uploadOk = 0;
158}
159// Allow certain file formats
160if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
161&& $imageFileType != "gif" ) {
162 echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
163 $uploadOk = 0;
164}
165// Check if $uploadOk is set to 0 by an error
166if ($uploadOk == 0) {
167 echo "Sorry, your file was not uploaded.";
168// if everything is ok, try to upload file
169} else {
170 if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
171 echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
172 } else {
173 echo "Sorry, there was an error uploading your file.";
174 }
175}
176?>
177
178--------------------------------------------------
179SQL operations
180
181open connection
182
183<?php
184$servername = "localhost";
185$username = "username";
186$password = "password";
187
188// Create connection
189$conn = new mysqli($servername, $username, $password);
190
191// Check connection
192if ($conn->connect_error) {
193 die("Connection failed: " . $conn->connect_error);
194}
195echo "Connected successfully";
196?>
197
198<?php
199$servername = "localhost";
200$username = "username";
201$password = "password";
202
203// Create connection
204$conn = new mysqli($servername, $username, $password);
205// Check connection
206if ($conn->connect_error) {
207 die("Connection failed: " . $conn->connect_error);
208}
209
210
211
212Create database
213$sql = "CREATE DATABASE myDB";
214if ($conn->query($sql) === TRUE) {
215 echo "Database created successfully";
216} else {
217 echo "Error creating database: " . $conn->error;
218}
219
220$conn->close();
221?>
222
223
224CREATE TABLE
225
226
227<?php
228$servername = "localhost";
229$username = "username";
230$password = "password";
231$dbname = "myDB";
232
233// Create connection
234$conn = new mysqli($servername, $username, $password, $dbname);
235// Check connection
236if ($conn->connect_error) {
237 die("Connection failed: " . $conn->connect_error);
238}
239
240//sql to create table
241$sql = "CREATE TABLE MyGuests (
242id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
243firstname VARCHAR(30) NOT NULL,
244lastname VARCHAR(30) NOT NULL,
245email VARCHAR(50),
246reg_date TIMESTAMP
247)";
248
249if ($conn->query($sql) === TRUE) {
250 echo "Table MyGuests created successfully";
251} else {
252 echo "Error creating table: " . $conn->error;
253}
254
255$conn->close();
256?>
257
258
259
260
261INSERT DATA
262
263
264
265<?php
266$servername = "localhost";
267$username = "username";
268$password = "password";
269$dbname = "myDB";
270
271// Create connection
272$conn = new mysqli($servername, $username, $password, $dbname);
273// Check connection
274if ($conn->connect_error) {
275 die("Connection failed: " . $conn->connect_error);
276}
277
278$sql = "INSERT INTO MyGuests (firstname, lastname, email)
279VALUES ('John', 'Doe', 'john@example.com')";
280
281if ($conn->query($sql) === TRUE) {
282 echo "New record created successfully";
283} else {
284 echo "Error: " . $sql . "<br>" . $conn->error;
285}
286
287$conn->close();
288?>
289
290
291
292INSERT MULTIPLE
293
294
295
296
297$sql = "INSERT INTO MyGuests (firstname, lastname, email)
298VALUES ('John', 'Doe', 'john@example.com');";
299$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
300VALUES ('Mary', 'Moe', 'mary@example.com');";
301$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
302VALUES ('Julie', 'Dooley', 'julie@example.com')";
303
304
305
306
307LAST ID:
308
309
310$conn->insert_id;
311
312
313<?php
314$servername = "localhost";
315$username = "username";
316$password = "password";
317$dbname = "myDB";
318
319// Create connection
320$conn = new mysqli($servername, $username, $password, $dbname);
321
322// Check connection
323if ($conn->connect_error) {
324 die("Connection failed: " . $conn->connect_error);
325}
326
327// prepare and bind
328$stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email) VALUES (?, ?, ?)");
329$stmt->bind_param("sss", $firstname, $lastname, $email);
330
331// set parameters and execute
332$firstname = "John";
333$lastname = "Doe";
334$email = "john@example.com";
335$stmt->execute();
336
337$firstname = "Mary";
338$lastname = "Moe";
339$email = "mary@example.com";
340$stmt->execute();
341
342$firstname = "Julie";
343$lastname = "Dooley";
344$email = "julie@example.com";
345$stmt->execute();
346
347echo "New records created successfully";
348
349$stmt->close();
350$conn->close();
351?>
352
353
354
355SELECT DATA
356
357$sql = "SELECT id, firstname, lastname FROM MyGuests";
358
359
360DELETE DATA
361
362
363$sql = "DELETE FROM MyGuests WHERE id=3";
364
365
366
367UPDATE DATA
368
369$sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";
370
371
372LIMIT DATA
373
374
375$sql = "SELECT * FROM Orders LIMIT 15, 10";