Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $servername = "Localhost";
- $username = "root";
- $password = "";
- $dbname = "form";
- function TimsDump($v)
- {
- echo "<pre style='background-color:#fff;'>";
- var_dump($v);
- echo "</pre>";
- }
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- // check to see if form is submitted
- if(isset($_POST['save']))
- {
- // error messages
- $errorMessages = array();
- // assign posted form data to some new variables. Makes it easier.
- $lastname = $_POST["lastname"];
- $firstname = $_POST['firstname'];
- $email = $_POST['email'];
- $message = $_POST['message'];
- if($firstname == "")
- {
- $errorMessages[] = "Hey dude, please fill your first name!";
- }
- if($lastname == "")
- {
- $errorMessages[] = "Hey dude, please fill your last name!";
- }
- if($message == "")
- {
- $errorMessages[] = "Hey dude, please fill a message!";
- }
- // now count number of keys or indexes in $errorMessages
- // if zero, all's fine
- // else, dont run query
- if(count($errorMessages) == 0)
- {
- $sql = "INSERT INTO users (firstname, lastname, email, message)
- VALUES ('".$_POST["firstname"]."','".$_POST["lastname"]."','".$_POST["email"]."','".$_POST["message"]."')";
- $result = mysqli_query($conn,$sql);
- }
- }
- // the SQL string that fetches data....
- $selectSQL = "SELECT firstname, lastname, email, message FROM users";
- // This is a prepared statement, not necessary with this simple query with no variables, but anyway...
- // run query and store the resource into variable
- $result = $conn->query($selectSQL);
- // messages array to store data into
- $messages = array();
- while($row = $result->fetch_assoc())
- {
- $messages[] = $row;
- }
- TimsDump($errorMessages);
- ?>
- <!Doctype html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
- <meta name="description" content="$1">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <link rel="stylesheet" type="text/css" href="css/style-index.css">
- <title>test</title>
- </head>
- <body>
- <?php
- if(!empty($errorMessages))
- {
- echo "<h3>There are some errors:</h3>";
- echo "<ul>";
- // array is filled with ...
- foreach($errorMessages as $a => $b)
- {
- ?>
- <li><?php echo $b; ?></li>
- <?php
- }
- echo "</ul>";
- }
- ?>
- <form action="index.php" method="POST">
- <label id="first"> First name</label><br/>
- <input type="text" name="firstname"><br/>
- <label id="first">Last name</label><br/>
- <input type="text" name="lastname"><br/>
- <label id="first">Email</label><br/>
- <input type="text" name="email"><br/>
- <label id="first">Message</label><br/>
- <textarea name="message"></textarea>
- <button type="submit" name="save">save</button>
- </form>
- <table>
- <thead>
- <tr>
- <th>Firstname</th>
- <th>Lastname</th>
- <th>E-mail</th>
- <th>Message</th>
- </tr>
- </thead>
- <tbody>
- <!--Use a while loop to make a table row for every ìndex in $messages-->
- <?php
- foreach($messages as $k => $v)
- {
- ?>
- <tr>
- <!--Each table column is echoed in to a td cell-->
- <td><?php echo $v['lastname']; ?></td>
- <td><?php echo $v['lastname']; ?></td>
- <td><?php echo $v['email']; ?></td>
- <td><?php echo $v['message']; ?></td>
- </tr>
- <?php
- // end of loop
- }
- ?>
- </tbody>
- </table>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement