setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // $data = [ // 'filename' => 'testupload', // 'path' => 'uploads/', // 'extension' => 'png', // 'status' => 1, // 'active' => 1, // 'created_at' => date("Y-m-d h:i:s") // ]; // createData($pdo, 'files', $data); } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); } function createData($pdo, $table, $data){ try{ $columns = implode(", ", array_keys($data)); $placeholders = implode(", ", array_fill(0, count($data), '?')); $pdo->beginTransaction(); $sql = "INSERT INTO customer_upload_ltms ($columns) VALUES ($placeholders) "; $stmt = $pdo->prepare($sql); $stmt->execute(array_values($data)); $pdo->commit(); echo'Record inserted successfully'; // $conditions = ['active' => 1]; // $results = readData($pdo, 'files', $conditions); } catch(PDOException $e){ $pdo->rollback(); echo 'Failed to insert data : '.$e->getMessage(); } } function readData($pdo, $table, $conditions =[]){ try{ $sql = "SELECT * FROM $table"; if (!empty($conditions)) { $sql .= " WHERE " . implode(" AND ", array_map(function ($key) { return "$key = ?"; }, array_keys($conditions))); } // Prepare the statement $stmt = $pdo->prepare($sql); // Execute with condition values $stmt->execute(array_values($conditions)); // Fetch all the results $results = $stmt->fetchAll(PDO::FETCH_ASSOC); return $results; } catch(PDOException $e) { echo 'Read Failed: '. $e->getMessage(); return []; } } ?>