Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.60 KB | None | 0 0
  1. <?php
  2.  
  3. $servername = "localhost";
  4. $username = "admin_rq";
  5. $password = "rdOhu8GuZV";
  6. $database = "admin_rq";
  7.  
  8. // load the post data
  9. $postdata = file_get_contents("php://input");
  10. $data = json_decode($postdata, true);
  11.  
  12. $table = $data['table'];
  13. $name = $data['name'];
  14. $email = $data['email'];
  15. $points = $data['points'];
  16. $percentage = $data['percentage'];
  17. $userAnswers = $data['userAnswers'];
  18.  
  19. function connect_DB($servername, $username, $password, $database) {
  20.     $db_conn  = new mysqli($servername, $username, $password, $database);
  21.     $char = $db_conn->query("SET NAMES 'utf8'");
  22.  
  23.     if ($db_conn->connect_error) {
  24.         header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
  25.         die("Connection failed: " . $db_conn->connect_error);
  26.     }
  27.     else {
  28.         echo "Connected successfully \r\n";
  29.     }
  30.     return $db_conn;
  31. }
  32.  
  33. function create_DB_table($db, $table) {
  34.     $sql = "CREATE TABLE IF NOT EXISTS $table (
  35.        id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  36.        reg_date TIMESTAMP
  37.    )";
  38.  
  39.     if ($db->query($sql) == TRUE) {
  40.          echo "Table successfully created \r\n";
  41.      }
  42.      else {
  43.          header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
  44.          die("Table creation failed: " . $db->error);
  45.  
  46.      }
  47. }
  48.  
  49. function addColumnVarchar($conn_obj, $table, $column) {
  50.     $sql = $conn_obj->prepare("SHOW COLUMNS FROM $table LIKE '%$column%'"); // add wildcard
  51.     $sql->execute();
  52.     if($sql->num_rows <= 0) {
  53.         $sql->store_result();
  54.         $sql1 = $conn_obj->prepare("ALTER TABLE $table ADD COLUMN $column VARCHAR(255)");
  55.         $sql1->execute();
  56.     }
  57. }
  58.  
  59. function addColumnFloat($conn_obj, $table, $column) {
  60.     $sql = $conn_obj->prepare("SHOW COLUMNS FROM $table LIKE '%$column%'"); // add wildcard
  61.     $sql->execute();
  62.     if($sql->num_rows <= 0) {
  63.         $sql->store_result();
  64.         $sql1 = $conn_obj->prepare("ALTER TABLE $table ADD COLUMN $column FLOAT");
  65.         $sql1->execute();
  66.     }
  67. }
  68.  
  69. function insert_DB($db, $table, $column, $value) {
  70.     $sql = "INSERT INTO $table ($column)
  71.        VALUES ($value)";
  72.  
  73.     if( $db->query($sql) == TRUE) {
  74.         echo "Records inserted successfully!";
  75.     }
  76.  
  77.     else {
  78.         header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
  79.         die("Records insertion failed: " . $db->error);
  80.     }
  81. }
  82.  
  83. //connect to the database and create table
  84. $conn_obj = connect_DB($servername, $username, $password, $database);
  85. create_DB_table($conn_obj, $table);
  86.  
  87. $columnArr = array();
  88. $valueArr = array();
  89.  
  90. if (!is_null($name)){
  91.     addColumnVarchar($conn_obj, $table, 'name');
  92.     array_push($columnArr, "name");
  93.     array_push($valueArr, $name);
  94. }
  95. if (!is_null($email)){
  96.     addColumnVarchar($conn_obj, $table, 'email');
  97.     array_push($columnArr, "email");
  98.     array_push($valueArr, $email);
  99. }
  100. if (!is_null($points)){
  101.     addColumnFloat($conn_obj, $table, 'points');
  102.     array_push($columnArr, "points");
  103.     array_push($valueArr, $points);
  104. }
  105. if (!is_null($percentage)){
  106.     addColumnFloat($conn_obj, $table, 'percentage');
  107.     array_push($columnArr, "percentage");
  108.     array_push($valueArr, $percentage);
  109. }
  110.  
  111. if (!is_null($userAnswers)){
  112.     foreach ($userAnswers as $ua) {
  113.         addColumnVarchar($conn_obj, $table, $ua['qID']);
  114.         array_push($columnArr, $ua['qID']);
  115.         array_push($valueArr, wordwrap($ua['answer'], 60, "\n", false));
  116.     }
  117. }
  118.  
  119.  
  120. $column = implode(",", $columnArr);
  121. $value = "'".implode("','", $valueArr)."'";
  122.  
  123. insert_DB($conn_obj, $table, $column, $value);
  124.  
  125. $conn_obj->close();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement