Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <style>
- table, th, td {
- border: 1px solid black;
- border-collapse: collapse;
- }
- th, td {
- padding: 5px;
- text-align: left;
- }
- </style>
- <body>
- <form method="post">
- <fieldset>
- <legend>Personal information:</legend>
- Id:<br>
- <input type="text" name="id" placeholder="Your id for updating or deleting" ><br>
- First name:<br>
- <input type="text" name="first_name" placeholder="Your name"><br>
- Last name:<br>
- <input type="text" name="last_name" placeholder="Your last name"><br>
- E-mail:<br>
- <input type="email" name="email" placeholder="Your E-mail adress"><br>
- Message:<br>
- <textarea name="message" placeholder="Your message" rows="5" cols="20" ></textarea>
- <br><br>
- <input type="submit" value="Submit" name="submit">
- <input type="submit" value="Update" name="update">
- <input type="submit" value="Delete" name="delete">
- <input type="submit" value="Find" name="find">
- </fieldset>
- </form>
- <?php
- $servername = "";
- $username = "" ;
- $password = "" ;
- $dbname = "" ;
- $first_name = $_POST ['first_name'];
- $last_name = $_POST ['last_name'];
- $email = $_POST ['email'];
- $message = $_POST ['message'];
- $ip = $_SERVER ['SERVER_ADDR'];
- $id = $_POST ['id'];
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- if(isset($_POST["submit"])){
- $submit = "INSERT INTO forms (ip, first_name, last_name, email, message)
- VALUES ('$ip', '$first_name', '$last_name', '$email', '$message')";
- if ($conn->query($submit) === TRUE) {
- echo "New record created succsessfully";
- } else {
- echo "Error: " . $submit . "<br>" . $conn->error;
- }
- $conn->close();
- }
- if(isset($_POST["delete"])){
- // sql to delete a record
- $delete = "DELETE FROM `forms` WHERE id='$id'";
- if ($conn->query($delete) === TRUE) {
- echo "Record deleted successfully";
- } else {
- echo "Error deleting record: " . $conn->error;
- }
- $conn->close();
- }
- if(isset($_POST["update"])){
- $update = "UPDATE `forms` SET `first_name`= '$first_name',`last_name`='$last_name',`email`='$email',`message`='$message' WHERE id='$id'";
- if ($conn->query($update) === TRUE) {
- echo "Record updated successfully";
- } else {
- echo "Error updating record: " . $conn->error;
- }
- $conn->close();
- }
- if(isset($_POST["find"])){
- $find = "";
- else if(!empty($first_name)){
- $find = "SELECT * FROM forms WHERE 'first_name'='$first_name'";
- } else {
- $find = "SELECT * FROM forms";
- }
- $result = $conn->query($find);
- $no = 0;
- if($result->num_rows > 0){
- echo "<p><p><table align=\"left\" border=\"1\" cellspacing=\"3\">\n";
- echo "<tr><th>No.</th>
- <th>ID</th>
- <th>Date</th>
- <th>IP</th>
- <th>First name</th>
- <th>Last name</th>
- <th>E-mail</th>
- <th>Message</th>";
- while($row = $result->fetch_assoc()){
- $no = $no+1;
- echo "<tr><td> $no </td>
- <td>". $row["id"] ."</td>
- <td>". $row["date"] ."</td>
- <td>". $row["ip"] ."</td>
- <td>". $row["first_name"] ."</td>
- <td>". $row["last_name"] ."</td>
- <td>". $row["email"] ."</td>
- <td>". $row["message"] ."</td>
- </tr>";
- }
- echo "</table>";
- }
- else {
- echo "Success";
- }
- $conn->close();
- }
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement