Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $host = "localhost";
- $user = "root";
- $password ="";
- $database = "ntmadb";
- $id = "";
- $firstname = "";
- $lastname = "";
- $username = "";
- mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
- // connect to mysql database
- try{
- $connect = mysqli_connect($host, $user, $password, $database);
- } catch (mysqli_sql_exception $ex) {
- echo 'Error';
- }
- // get values from the form
- function getPosts()
- {
- $posts = array();
- $posts[0] = $_POST['id'];
- $posts[1] = $_POST['firstname'];
- $posts[2] = $_POST['lastname'];
- $posts[3] = $_POST['username'];
- return $posts;
- }
- // Search
- if(isset($_POST['search']))
- {
- $data = getPosts();
- $search_Query = "SELECT * FROM members WHERE id = $data[0]";
- $search_Result = mysqli_query($connect, $search_Query);
- if($search_Result)
- {
- if(mysqli_num_rows($search_Result))
- {
- while($row = mysqli_fetch_array($search_Result))
- {
- $id = $row['id'];
- $firstname = $row['firstname'];
- $lastname = $row['lastname'];
- $username = $row['username'];
- }
- }else{
- echo 'No Data For This Id';
- }
- }else{
- echo 'Result Error';
- }
- }
- // Edit
- if(isset($_POST['update']))
- {
- $data = getPosts();
- $update_Query = "UPDATE `members` SET `firstname`='$data[1]',`lastname`='$data[2]',`username`='$data[3]' WHERE `id` = $data[0]";
- try{
- $update_Result = mysqli_query($connect, $update_Query);
- if($update_Result)
- {
- if(mysqli_affected_rows($connect) > 0)
- {
- echo 'Data Updated';
- }else{
- echo 'Data Not Updated';
- }
- }
- } catch (Exception $ex) {
- echo 'Error Update '.$ex->getMessage();
- }
- }
- <!--UPLOADUPLOADUPLOADUPLOADUPLOADUPLOADUPLOADUPLOADUPLOADUPLOADUPLOADUPLOADUPLOAD -->
- // Check if a file has been uploaded
- if(isset($_FILES['uploaded_file'])) {
- // Make sure the file was sent without errors
- if($_FILES['uploaded_file']['error'] == 0) {
- // Connect to the database
- $dbLink = new mysqli('localhost', 'root', '', 'ntmadb');
- if(mysqli_connect_errno()) {
- die("MySQL connection failed: ". mysqli_connect_error());
- }
- // Gather all required data
- $data = $dbLink->real_escape_string(file_get_contents($_FILES ['uploaded_file']['tmp_name']));
- // Create the SQL query
- $query = "
- INSERT INTO `members` (
- `data`
- )
- VALUES (
- '{$data}' NOW()
- )";
- // Execute the query
- $result = $dbLink->query($query);
- // Check if it was successfull
- if($result) {
- echo 'Success! Your file was successfully added!';
- }
- else {
- echo 'Error! Failed to insert the file'
- . "<pre>{$dbLink->error}</pre>";
- }
- }
- else {
- echo 'An error accured while the file was being uploaded. '
- . 'Error code: '. intval($_FILES['uploaded_file']['error']);
- }
- // Close the mysql connection
- $dbLink->close();
- }
- else {
- echo 'Error! A file was not sent!';
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement