daily pastebin goal
0%
SHARE
TWEET

Example PHP

LukaSJ0 Apr 27th, 2018 (edited) 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.     // config variables
  3.     $servername = "127.0.0.1";
  4.     $username = "username";
  5.     $password = "password";
  6.     $dbname = "database_name";
  7.     // create connection
  8.     $conn = mysqli_connect($servername, $username, $password, $dbname);
  9.     // check connection
  10.     if (!$conn)
  11.         die("failed");
  12.     // fetches the type of post to process in logic later
  13.     $type = $_POST["type"];
  14.     // gets a string with all the read contents from table
  15.     if ($type == "getTestSubjects") {
  16.         // sends query
  17.         $query = mysqli_query($conn, "SELECT Name, Surname, Age FROM TestTable");
  18.         while ($row = mysqli_fetch_assoc($query)) {
  19.             // appends data to string and posts to client
  20.             echo($row["Name"]. "</s>". $row["Surname"]. "</s>". $row["Age"]. "\r\n");
  21.         }
  22.     }
  23.     // adds new row to table
  24.     elseif ($type == "addTestSubject") {
  25.         $name = $_POST["name"];
  26.         $surname = $_POST["surname"];
  27.         $age = $_POST["age"];
  28.         // sends query
  29.         mysqli_query($conn, "INSERT INTO TestTable (Name, Surname, Age) VALUES ('$name', '$surname', '$age')");
  30.     }
  31.     // modifies existing data in table by ID
  32.     elseif ($type == "modifyTestSubject") {
  33.         $name = $_POST["name"];
  34.         $id = $_POST["id"];
  35.         // sends query
  36.         mysqli_query($conn, "UPDATE TestTable SET Name='$name' WHERE ID='$id'");
  37.     }
  38.     // closes the connection
  39.     mysqli_close($conn);
  40. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top