Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $servername = "localhost";
- //these should be changed to the values set up when building the lamp server
- //the username will be 'root' unless you changed it
- //the password will be whatever you chose
- $username = "username";
- $password = "password";
- //this is the name of the database
- $dbname = "db";
- // Create conn
- $conn = new mysqli($servername, $username, $password);
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- // Create database
- $sql = "CREATE DATABASE " . $dbname;
- if ($conn->query($sql) === TRUE) {
- } else {
- echo "<script>console.log('error creating db, probably exists')</script>";
- }
- $conn->close();
- //create table
- $conn = new mysqli($servername, $username, $password, $dbname);
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- $sqltable = "CREATE TABLE users (name VARCHAR(100) NOT NULL, id VARCHAR(50), info VARCHAR(50), dob VARCHAR(50) )";
- if ($conn->query($sqltable) === TRUE) {
- } else {
- echo "<script>console.log('error creating table, probably exists')</script>";
- }
- $conn->close();
- function test_input($data) {
- $data = trim($data);
- $data = stripslashes($data);
- $data = htmlspecialchars($data);
- return $data;
- }
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- $name = test_input($_POST["name"]);
- $id = test_input($_POST["id"]);
- $info = test_input($_POST["info"]);
- $dob = test_input($_POST["dob"]);
- //insert from form
- $conninsert = new mysqli($servername, $username, $password, $dbname);
- if ($conninsert->connect_error) {
- die("Connection failed: " . $conninsert->connect_error);
- }
- $sqlinsert = "INSERT INTO users (name, id, info, dob) VALUES ('" . $name . "', '" . $id . "', '" . $info . "', '" . $dob ."')";
- if ($conninsert->query($sqlinsert) === TRUE) {
- echo "New record created successfully";
- } else {
- echo "Error: " . $sql . "<br>" . $conninsert->error;
- }
- $conninsert->close();
- }
- ?>
- <h2>db info</h2>
- <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
- Name: <input type="text" name="name">
- <br><br>
- id <input type="text" name="id">
- <br><br>
- info <input type="text" name="info">
- <br><br>
- dob <input type="text" name="dob">
- <br><br>
- <input type="submit" name="submit" value="Submit">
- </form>
- <?php
- //select rows and display them
- $conn = new mysqli($servername, $username, $password, $dbname);
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- $sql = "SELECT name, id, info, dob FROM users";
- $result = $conn->query($sql);
- if ($result->num_rows > 0) {
- while($row = $result->fetch_assoc()) {
- echo "name: " . $row["name"]. " - id: " . $row["id"]. " - info: " . $row["info"]. " - dob: " . $row["dob"]."<br>";
- }
- } else {
- echo "0 results";
- }
- $conn->close();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement