Last active
          March 29, 2024 12:21 
        
      - 
      
 - 
        
Save arvindsvt/a1dfbbb922a3f0e56d5184b713fc9e04 to your computer and use it in GitHub Desktop.  
    IndertOrUpdate
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | function insertOrUpdate($table,$params=array()){ | |
| global $conn; | |
| $sqlQuery = 'CREATE TABLE IF NOT EXISTS '.$table.' (id INT NOT NULL AUTO_INCREMENT, '; | |
| foreach($params as $field=>$val){ | |
| if($field == 'id'){ continue ; } | |
| $sqlQuery .= ' '.$field.' VARCHAR( 250 ),'; | |
| } | |
| $sqlQuery .= ' PRIMARY KEY ( `id` ));'; | |
| $sqlQuery .='INSERT INTO `'.$table.'` (`'.implode('`, `',array_keys($params)).'`) VALUES ("' . implode('", "', $params) . '")'; | |
| $args=array(); | |
| foreach($params as $field=>$value){ | |
| $args[]= $field.'=VALUES('.$field.')'; | |
| } | |
| $sqlQuery .=' ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id) , '.implode(',',$args); | |
| if ( mysqli_query($conn, $sqlQuery) ) { | |
| return mysqli_insert_id($conn); | |
| } else { | |
| return false; | |
| } | |
| } | |
| ___________________________________________________________________________________ | |
| https://stackoverflow.com/questions/2866766/php-display-comma-after-each-element-except-the-last-using-for-statement-and | |
| <?php | |
| include_once('config.php'); | |
| $empsId = ( isset($_REQUEST['id'] ) && !empty($_REQUEST['id']) ) ? $_REQUEST['id'] : ''; | |
| $empId = ( isset($_REQUEST['emp_id'] ) && !empty($_REQUEST['emp_id']) ) ? $_REQUEST['emp_id'] : ''; | |
| // $input = json_decode(file_get_contents('php://input'), true); | |
| // // echo "<pre>"; | |
| // print_r($input ); | |
| if($_SERVER['REQUEST_METHOD'] == 'POST' ){ | |
| $first_name = $_REQUEST['fname']; | |
| $last_name = $_REQUEST['lname']; | |
| $email = $_REQUEST['email']; | |
| $phone = $_REQUEST['phone']; | |
| $post = $_REQUEST['post']; | |
| $fields = array( 'emp_id' , 'emp_avatar' , 'first_name', 'last_name', 'email' , 'phone' , 'post' ); | |
| $finalRequest = array_combine($fields, $_REQUEST); | |
| unset($finalRequest['emp_id']); | |
| unset($finalRequest['emp_avatar']); | |
| $finalRequest = array_merge(array('id'=> $empId ) , $finalRequest ); | |
| if( $empId ){ | |
| InsertOrUpdateRecord( $finalRequest , $table ='employees' , $empId); | |
| }else{ | |
| //InsertRecord( $finalRequest , $table ='employees' ); | |
| $sql = "INSERT INTO employees (id , first_name, last_name, email , phone , post) VALUES (null , '$first_name', '$last_name', '$email' , '$phone' , '$post' )"; | |
| if ($conn->query($sql) === TRUE) { | |
| echo "New record created successfully $conn->insert_id"; | |
| // header("Location: index.php"); | |
| } else { | |
| echo "Error: " . $sql . "<br>" . $conn->error; | |
| } | |
| } | |
| }else{ | |
| if( $empsId ){ | |
| $result = $conn->query("SELECT * FROM employees WHERE id = '$empsId' "); | |
| $emp = $result->fetch_assoc(); | |
| // echo "<pre>"; | |
| // print_r($emp ); | |
| header('Content-type: application/json; charset=utf-8'); | |
| echo json_encode($emp); | |
| }else{ | |
| $result = $conn->query("SELECT * FROM employees"); | |
| $emps = $result->fetch_all(MYSQLI_BOTH); | |
| $output = ''; | |
| if ( count($emps) > 0) { | |
| $output .= '<table class="table table-striped table-sm text-center align-middle"> | |
| <thead> | |
| <tr> | |
| <th>ID</th> | |
| <th>Avatar</th> | |
| <th>Name</th> | |
| <th>E-mail</th> | |
| <th>Post</th> | |
| <th>Phone</th> | |
| <th>Action</th> | |
| </tr> | |
| </thead> | |
| <tbody>'; | |
| foreach ($emps as $emp) { | |
| // $empavatar = ($emp['avatar']) ? 'storage/images/'. $emp['avatar'] : 'https://placehold.co/600x400'; | |
| $empavatar = 'https://placehold.co/600x400'; | |
| $output .= '<tr> | |
| <td>' . $emp['id'] . '</td> | |
| <td><img src=" ' . $empavatar . ' " width="50" class="img-thumbnail rounded-circle"></td> | |
| <td>' . $emp['first_name'] . ' ' . $emp['last_name'] . '</td> | |
| <td>' . $emp['email' ] . '</td> | |
| <td>' . $emp['post'] . '</td> | |
| <td>' . $emp['phone'] . '</td> | |
| <td> | |
| <a href="#" id="' . $emp['id'] . '" class="text-success mx-1 editIcon" data-bs-toggle="modal" data-bs-target="#editEmployeeModal"><i class="bi-pencil-square h4"></i></a> | |
| <a href="#" id="' . $emp['id'] . '" class="text-danger mx-1 deleteIcon"><i class="bi-trash h4"></i></a> | |
| </td> | |
| </tr>'; | |
| } | |
| $output .= '</tbody></table>'; | |
| echo $output; | |
| } else { | |
| echo '<h1 class="text-center text-secondary my-5">No record present in the database!</h1>'; | |
| } | |
| } | |
| } | |
| function UpdateRecord( $finalRequest , $table , $empId ){ | |
| global $conn; | |
| $sql = $coma = ""; | |
| foreach( $finalRequest as $key => $val){ | |
| $sql .= "$coma$key='$val'"; | |
| $coma = ","; } | |
| $sql = "UPDATE $table SET $sql WHERE id = '$empId' "; | |
| if ($conn->query($sql) === TRUE) { | |
| echo "Record updated successfully"; | |
| } else { | |
| echo "Error updating record: " . $conn->error; | |
| } | |
| // header('Content-type: application/json; charset=utf-8'); | |
| // echo json_encode($emp); | |
| // header("Location: index.php"); | |
| } | |
| function InsertRecord( $finalRequest , $table ='employees' ){ | |
| global $conn; | |
| $fields = implode(",", array_keys($finalRequest)); | |
| $newdata = implode(",", array_map(function($x) use ($conn) { | |
| return "'" . $conn->real_escape_string($x) . "'"; | |
| }, $finalRequest)); | |
| $sql = "INSERT INTO employees ($fields) VALUES ($newdata)"; | |
| if ($conn->query($sql) === TRUE) { | |
| echo "New record created successfully $conn->insert_id"; | |
| // header("Location: index.php"); | |
| } else { | |
| echo "Error: " . $sql . "<br>" . $conn->error; | |
| } | |
| // header('Content-type: application/json; charset=utf-8'); | |
| // echo json_encode($emp); | |
| // header("Location: index.php"); | |
| } | |
| function InsertOrUpdateRecord( $finalRequest , $table ='employees' ){ | |
| global $conn; | |
| $sqlfields = $sqlval = $sql2 = $sql = ""; | |
| foreach($finalRequest as $key => $val) | |
| { | |
| $sqlfields .= "$key,"; | |
| $all_fieldsval = $conn->real_escape_string( $val ); | |
| $sqlval .= " '$all_fieldsval' ,"; | |
| $sql2 .= ", $key=VALUES($key)"; | |
| } | |
| $sql = "INSERT INTO $table (" . trim($sqlfields, ",") . ") VALUES ( " . trim($sqlval, ",") . " )"; | |
| $sql .= " ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id) " . $sql2; | |
| //echo $sql; | |
| if ($conn->query($sql) === TRUE) { | |
| echo json_encode([ | |
| 'status' => 200, | |
| 'message' => 'inserted', | |
| 'lastinsertedId' => mysqli_insert_id($conn), | |
| ]); | |
| } else { | |
| echo json_encode([ | |
| 'status' => 201, | |
| ]); | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment