Guest User

Untitled

a guest
Apr 8th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.58 KB | None | 0 0
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2.     <?php
  3.     error_reporting(E_ALL);
  4. ini_set('display_errors', '1');
  5.     include("Table.php");
  6.  
  7. $user = 'zpmm6';
  8. $pass = 'coldshot1';
  9. $sql = 'select * from MAJOR';
  10. //seperate array for the header data
  11. $headers = array();
  12. // the string for the prepare statment
  13. $headerPrepString = "select COLUMN_NAME from information_schema.COLUMNS where TABLE_NAME = 'MAJOR'";
  14.  
  15.  
  16. //create the PDO object with the dbconnection info
  17. try{
  18. $dbConnect = new PDO("mysql:host=localhost;dbname=zpmm6", $user, $pass);
  19. }
  20. catch(PDOException $e){
  21.     print "error!: " .$e->getMessage() . "<br>";
  22. }
  23. //call prepare to get the column info and execute the string
  24. $headPrep = $dbConnect->prepare($headerPrepString);
  25. $headPrep->execute();
  26.  
  27.  
  28. //take the headPrep arrays and pass the first array in the column to new oen dimensional headers array
  29. foreach($headPrep as $colName)
  30. {
  31.     $headers[] = $colName[0];
  32. }
  33.  
  34. //create the table which all the data with be put in and instatiate it with headers at top array
  35. $table  =   new Table($headers);
  36. //$table2 = new Table();
  37.  
  38. //numerical and tuple data is returned without the PDO::fetch_num param
  39. $data = $dbConnect->query($sql , PDO::FETCH_ASSOC);
  40.  
  41. //final loop to populate the table with remaining rows.
  42. foreach($data as $row)
  43. {
  44.     $table->addRow($row);
  45. }
  46.     $content = $table->render();
  47.    
  48.    
  49.     //$content .= $table->renderNew();
  50.    
  51.     include('index.php');
  52.    
  53.    
  54.     ?>
  55.    
  56.     <?php
  57.    
  58.     $my_key = '';
  59.     if(isset($_POST['changeValue']))  {
  60.         //loop through post looking for element that isnt change value
  61.         //to get the row and column to update in the DB
  62.         foreach ($_POST as $key => $val)
  63.         {
  64.             if ($key != 'changeValue')
  65.             {
  66.                 $my_key = $key;
  67.             }
  68.         }
  69.        
  70.         $tmp = explode("_", $my_key);
  71.         $col= $tmp[2];
  72.         $Primary_Key = $tmp[1];
  73.         //$newName = 'newText.' . $Primary_Key . '.' . $col;
  74.        
  75.         //test this crazy stuff
  76.         //$post = $_POST;
  77.         //unset($post['changeValue']);
  78.         $data = htmlspecialchars(next($_POST));
  79.        
  80.         //now get the data contained in the text box and send it to the SQL query
  81.             //NOTES: cant use explode for all columns since it delimits on post data which uses "_"
  82.             //cant get updates to work on immediate reload, happen on next reload of page
  83.             //should work for all columns other than Student_cap but doesnt work at all if I use param for column name in query
  84.            
  85.            
  86.            
  87.             if($col=='DEPARTMENT')
  88.             {
  89.                 $sql = "update MAJOR set DEPARTMENT = :data where MAJOR.TITLE = :id ";
  90.                 $Update_T = $dbConnect->prepare($sql);
  91.                 $Update_T->bindParam(':data',$data,PDO::PARAM_STR);
  92.                 $Update_T->bindParam(':id',$Primary_Key,PDO::PARAM_STR);
  93.                 $Update_T->execute();
  94.                
  95.             }
  96.            
  97.             //this is student as oopsed to student_cap since I delimit on underscores.
  98.             elseif($col=='STUDENT')
  99.             {
  100.                 $sql = "update MAJOR set STUDENT_CAP = :data where MAJOR.TITLE = :id ";
  101.                 $Update_T = $dbConnect->prepare($sql);
  102.                 $Update_T->bindParam(':data',$data,PDO::PARAM_STR);
  103.                 $Update_T->bindParam(':id',$Primary_Key,PDO::PARAM_STR);
  104.                 $Update_T->execute();
  105.                
  106.  
  107.             }
  108.            
  109.             elseif($col=='NAME')
  110.             {
  111.                 $sql = "update MAJOR set NAME = :data where MAJOR.TITLE = :id ";
  112.                 $Update_T = $dbConnect->prepare($sql);
  113.                 $Update_T->bindParam(':data',$data,PDO::PARAM_STR);
  114.                 $Update_T->bindParam(':id',$Primary_Key,PDO::PARAM_STR);
  115.                 $Update_T->execute();
  116.                        
  117.             }
  118.  
  119.    
  120.     }
  121.  
  122.    
  123.    
  124.     if(isset($_POST['changeValue']))
  125.     {
  126.    
  127.         //$post = $_POST;
  128.         //unset($post['changeValue']);
  129.         //$keys = array_keys($post);
  130.         //$key = $keys[0];0
  131.    
  132.            
  133.         //$data = htmlspecialchars(next($_POST));
  134.         /*
  135.         $pattern = '/^newText.[a-zA-Z0-9]*$/';
  136.         preg_match($pattern, substr($data,12), $matches);
  137.         $loc = $matches[0];
  138.         */
  139.         echo 'Primary Key: ' . $Primary_Key;
  140.         echo '</br> Column: ' . $col;
  141.         //echo '</br> Text Box Name ' . $newName;
  142.         echo '</br> SQL statemnt: ' . $sql;
  143.         echo '</br> DATA: ';
  144.         print_r($data);
  145.         //echo '</br>' . $flag;
  146.         //echo '</br>' . $key;
  147.         //'<pre>'; print_r($data); die;
  148.         //$updateSql = "update MAJOR set newText = :
  149.             //where MAJOR.ID = :";
  150.             //$results = $dbConnect->prepare($sql);
  151.             //unset($_POST);
  152.     }
  153.    
  154.     //check to see if any ID are in super global arrays from being set by clicking submit button
  155.     //if so you need to query the DB to check and see if the ID exsists
  156.     // if it does, decline to update, because it is primary key
  157.     //else if it does not, then update DB with new infromation from form
  158.     //TODO: make creation fields check here
  159.  
  160.  
  161.     //checking to see if submit button was clicked
  162.     //if so we update the DB
  163.    
  164.     ?>
  165.    
  166.     <script type="text/javascript">
  167.        
  168.    
  169.     </script>
Add Comment
Please, Sign In to add comment