Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?xml version="1.0" encoding="UTF-8" ?>
- <?php
- error_reporting(E_ALL);
- ini_set('display_errors', '1');
- include("Table.php");
- $user = 'zpmm6';
- $pass = 'coldshot1';
- $sql = 'select * from MAJOR';
- //seperate array for the header data
- $headers = array();
- // the string for the prepare statment
- $headerPrepString = "select COLUMN_NAME from information_schema.COLUMNS where TABLE_NAME = 'MAJOR'";
- //create the PDO object with the dbconnection info
- try{
- $dbConnect = new PDO("mysql:host=localhost;dbname=zpmm6", $user, $pass);
- }
- catch(PDOException $e){
- print "error!: " .$e->getMessage() . "<br>";
- }
- //call prepare to get the column info and execute the string
- $headPrep = $dbConnect->prepare($headerPrepString);
- $headPrep->execute();
- //take the headPrep arrays and pass the first array in the column to new oen dimensional headers array
- foreach($headPrep as $colName)
- {
- $headers[] = $colName[0];
- }
- //create the table which all the data with be put in and instatiate it with headers at top array
- $table = new Table($headers);
- $table2 = new Table();
- //numerical and tuple data is returned without the PDO::fetch_num param
- $data = $dbConnect->query($sql , PDO::FETCH_ASSOC);
- //final loop to populate the table with remaining rows.
- foreach($data as $row)
- {
- $table->addRow($row);
- }
- $content = $table->render();
- $content .= $table->renderNew();
- include('index.php');
- ?>
- <?php
- $my_key = '';
- if(isset($_POST['changeValue'])) {
- //loop through post looking for element that isnt change value
- //to get the row and column to update in the DB
- foreach ($_POST as $key => $val)
- {
- if ($key != 'changeValue')
- {
- $my_key = $key;
- }
- }
- $tmp = explode("_", $my_key);
- $col= $tmp[2];
- $Primary_Key = $tmp[1];
- $newName = 'newText.' . $Primary_Key . '.' . $col;
- //test this crazy stuff
- //$post = $_POST;
- //unset($post['changeValue']);
- $data = htmlspecialchars(next($_POST));
- //now get the data contained in the text box and send it to the SQL query
- //NOTES: cant use explode for all columns since it delimits on post data which uses "_"
- //cant get updates to work on immediate reload, happen on next reload of page
- //should work for all columns other than Student_cap but doesnt work at all if I use param for column name in query
- if($col=='DEPARTMENT')
- {
- $sql = "update MAJOR set DEPARTMENT = :data where MAJOR.TITLE = :id ";
- $Update_T = $dbConnect->prepare($sql);
- $Update_T->bindParam(':data',$data,PDO::PARAM_STR);
- $Update_T->bindParam(':id',$Primary_Key,PDO::PARAM_STR);
- $Update_T->execute();
- }
- //this is student as oopsed to student_cap since I delimit on underscores.
- elseif($col=='STUDENT')
- {
- $sql = "update MAJOR set STUDENT_CAP = :data where MAJOR.TITLE = :id ";
- $Update_T = $dbConnect->prepare($sql);
- $Update_T->bindParam(':data',$data,PDO::PARAM_STR);
- $Update_T->bindParam(':id',$Primary_Key,PDO::PARAM_STR);
- $Update_T->execute();
- }
- elseif($col=='NAME')
- {
- $sql = "update MAJOR set NAME = :data where MAJOR.TITLE = :id ";
- $Update_T = $dbConnect->prepare($sql);
- $Update_T->bindParam(':data',$data,PDO::PARAM_STR);
- $Update_T->bindParam(':id',$Primary_Key,PDO::PARAM_STR);
- $Update_T->execute();
- }
- }
- if(isset($_POST['changeValue']))
- {
- //$post = $_POST;
- //unset($post['changeValue']);
- //$keys = array_keys($post);
- //$key = $keys[0];0
- //$data = htmlspecialchars(next($_POST));
- /*
- $pattern = '/^newText.[a-zA-Z0-9]*$/';
- preg_match($pattern, substr($data,12), $matches);
- $loc = $matches[0];
- */
- echo $Primary_Key;
- echo '</br>' . $col;
- echo '</br>' . $newName;
- echo '</br>' . $sql;
- echo '</br>';
- print_r($data);
- //echo '</br>' . $flag;
- //echo '</br>' . $key;
- //'<pre>'; print_r($data); die;
- //$updateSql = "update MAJOR set newText = :
- //where MAJOR.ID = :";
- //$results = $dbConnect->prepare($sql);
- //unset($_POST);
- }
- //check to see if any ID are in super global arrays from being set by clicking submit button
- //if so you need to query the DB to check and see if the ID exsists
- // if it does, decline to update, because it is primary key
- //else if it does not, then update DB with new infromation from form
- //TODO: make creation fields check here
- //checking to see if submit button was clicked
- //if so we update the DB
- ?>
- <script type="text/javascript">
- </script>
Add Comment
Please, Sign In to add comment