Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Form Validation
- <!DOCTYPE HTML>
- <html>
- <head>
- <style>
- .error {color: #FF0000;}
- </style>
- </head>
- <body>
- <?php
- // define variables and set to empty values
- $nameErr = $emailErr = $genderErr = $websiteErr = "";
- $name = $email = $gender = $comment = $website = "";
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- if (empty($_POST["name"])) {
- $nameErr = "Name is required";
- } else {
- $name = test_input($_POST["name"]);
- // check if name only contains letters and whitespace
- if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
- $nameErr = "Only letters and white space allowed";
- }
- }
- if (empty($_POST["email"])) {
- $emailErr = "Email is required";
- } else {
- $email = test_input($_POST["email"]);
- // check if e-mail address is well-formed
- if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
- $emailErr = "Invalid email format";
- }
- }
- if (empty($_POST["website"])) {
- $website = "";
- } else {
- $website = test_input($_POST["website"]);
- // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
- if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
- $websiteErr = "Invalid URL";
- }
- }
- if (empty($_POST["comment"])) {
- $comment = "";
- } else {
- $comment = test_input($_POST["comment"]);
- }
- if (empty($_POST["gender"])) {
- $genderErr = "Gender is required";
- } else {
- $gender = test_input($_POST["gender"]);
- }
- }
- function test_input($data) {
- $data = trim($data);
- $data = stripslashes($data);
- $data = htmlspecialchars($data);
- return $data;
- }
- ?>
- <h2>PHP Form Validation Example</h2>
- <p><span class="error">* required field.</span></p>
- <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
- Name: <input type="text" name="name" value="<?php echo $name;?>">
- <span class="error">* <?php echo $nameErr;?></span>
- <br><br>
- E-mail: <input type="text" name="email" value="<?php echo $email;?>">
- <span class="error">* <?php echo $emailErr;?></span>
- <br><br>
- Website: <input type="text" name="website" value="<?php echo $website;?>">
- <span class="error"><?php echo $websiteErr;?></span>
- <br><br>
- Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
- <br><br>
- Gender:
- <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">Female
- <input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">Male
- <span class="error">* <?php echo $genderErr;?></span>
- <br><br>
- <input type="submit" name="submit" value="Submit">
- </form>
- <?php
- echo "<h2>Your Input:</h2>";
- echo $name;
- echo "<br>";
- echo $email;
- echo "<br>";
- echo $website;
- echo "<br>";
- echo $comment;
- echo "<br>";
- echo $gender;
- ?>
- </body>
- </html>
- ---------------------------------------------------------
- //FILES
- file_uploads = On
- HTML FILE
- <!DOCTYPE html>
- <html>
- <body>
- <form action="upload.php" method="post" enctype="multipart/form-data">
- Select image to upload:
- <input type="file" name="fileToUpload" id="fileToUpload">
- <input type="submit" value="Upload Image" name="submit">
- </form>
- </body>
- </html>
- COMPLETE PHP FILE
- <?php
- $target_dir = "uploads/";
- $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
- $uploadOk = 1;
- $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
- // Check if image file is a actual image or fake image
- if(isset($_POST["submit"])) {
- $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
- if($check !== false) {
- echo "File is an image - " . $check["mime"] . ".";
- $uploadOk = 1;
- } else {
- echo "File is not an image.";
- $uploadOk = 0;
- }
- }
- // Check if file already exists
- if (file_exists($target_file)) {
- echo "Sorry, file already exists.";
- $uploadOk = 0;
- }
- // Check file size
- if ($_FILES["fileToUpload"]["size"] > 500000) {
- echo "Sorry, your file is too large.";
- $uploadOk = 0;
- }
- // Allow certain file formats
- if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
- && $imageFileType != "gif" ) {
- echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
- $uploadOk = 0;
- }
- // Check if $uploadOk is set to 0 by an error
- if ($uploadOk == 0) {
- echo "Sorry, your file was not uploaded.";
- // if everything is ok, try to upload file
- } else {
- if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
- echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
- } else {
- echo "Sorry, there was an error uploading your file.";
- }
- }
- ?>
- --------------------------------------------------
- SQL operations
- open connection
- <?php
- $servername = "localhost";
- $username = "username";
- $password = "password";
- // Create connection
- $conn = new mysqli($servername, $username, $password);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- echo "Connected successfully";
- ?>
- <?php
- $servername = "localhost";
- $username = "username";
- $password = "password";
- // Create connection
- $conn = new mysqli($servername, $username, $password);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- Create database
- $sql = "CREATE DATABASE myDB";
- if ($conn->query($sql) === TRUE) {
- echo "Database created successfully";
- } else {
- echo "Error creating database: " . $conn->error;
- }
- $conn->close();
- ?>
- CREATE TABLE
- <?php
- $servername = "localhost";
- $username = "username";
- $password = "password";
- $dbname = "myDB";
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- //sql to create table
- $sql = "CREATE TABLE MyGuests (
- id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
- firstname VARCHAR(30) NOT NULL,
- lastname VARCHAR(30) NOT NULL,
- email VARCHAR(50),
- reg_date TIMESTAMP
- )";
- if ($conn->query($sql) === TRUE) {
- echo "Table MyGuests created successfully";
- } else {
- echo "Error creating table: " . $conn->error;
- }
- $conn->close();
- ?>
- INSERT DATA
- <?php
- $servername = "localhost";
- $username = "username";
- $password = "password";
- $dbname = "myDB";
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- $sql = "INSERT INTO MyGuests (firstname, lastname, email)
- VALUES ('John', 'Doe', 'john@example.com')";
- if ($conn->query($sql) === TRUE) {
- echo "New record created successfully";
- } else {
- echo "Error: " . $sql . "<br>" . $conn->error;
- }
- $conn->close();
- ?>
- INSERT MULTIPLE
- $sql = "INSERT INTO MyGuests (firstname, lastname, email)
- VALUES ('John', 'Doe', 'john@example.com');";
- $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
- VALUES ('Mary', 'Moe', 'mary@example.com');";
- $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
- VALUES ('Julie', 'Dooley', 'julie@example.com')";
- LAST ID:
- $conn->insert_id;
- <?php
- $servername = "localhost";
- $username = "username";
- $password = "password";
- $dbname = "myDB";
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- // prepare and bind
- $stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email) VALUES (?, ?, ?)");
- $stmt->bind_param("sss", $firstname, $lastname, $email);
- // set parameters and execute
- $firstname = "John";
- $lastname = "Doe";
- $email = "john@example.com";
- $stmt->execute();
- $firstname = "Mary";
- $lastname = "Moe";
- $email = "mary@example.com";
- $stmt->execute();
- $firstname = "Julie";
- $lastname = "Dooley";
- $email = "julie@example.com";
- $stmt->execute();
- echo "New records created successfully";
- $stmt->close();
- $conn->close();
- ?>
- SELECT DATA
- $sql = "SELECT id, firstname, lastname FROM MyGuests";
- DELETE DATA
- $sql = "DELETE FROM MyGuests WHERE id=3";
- UPDATE DATA
- $sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";
- LIMIT DATA
- $sql = "SELECT * FROM Orders LIMIT 15, 10";
Add Comment
Please, Sign In to add comment