Thursday, 9 November 2023

XDEBUG -- php.ini file


Xdebug: Support — Tailored Installation Instructions

zend_extension = xdebug

xdebug.mode=debug

xdebug.default_enable=1

xdebug.start_start_with_request=yes


Enables Xdebug and Step Debugger in VS Code



Sunday, 5 November 2023

csv upload in php

 



<!DOCTYPE html>
<html>
<head>
    <title>CSV Upload and Database Insert</title>
</head>
<body>
    <form action="<?php echo htmlspecialchars ($_SERVER['PHP_SELF']) ?>" method="post" enctype="multipart/form-data">
        <input type="file" name="csv_file">
        <input type="submit" name="submit" value="Upload CSV">
    </form>
</body>
</html>


<?php
error_reporting(0);
if ($_POST['submit']) {    
    if (isset($_FILES['csv_file']) && is_uploaded_file($_FILES['csv_file']['tmp_name'])) {

        // Database connection
        $db_host = '127.0.0.1';
        $db_user = 'test';
        $db_pass = 'test123';
        $db_name = 'test';
        $db_port = '3306';

        $conn = new mysqli($db_host, $db_user, $db_pass, $db_name, $db_port);

        // $conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name, $db_port);

        if ($conn->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
            exit();

        }

        echo "<br>database connection successful";
        // Get the uploaded file
        $csv_file = $_FILES['csv_file']['tmp_name'];

        // Read the CSV file
        $csv_data = file_get_contents($csv_file);
        $csv_rows = explode("\n", $csv_data);

        // Loop through the CSV data and insert into the database
        $sql = "TRUNCATE TABLE csv_table;";
        $conn->query($sql);
        foreach ($csv_rows as $row) {
            $row_data = str_getcsv($row);

            // Assuming the CSV has two columns (change as needed)
            $column1 = $conn->real_escape_string($row_data[0]);
            $column2 = $conn->real_escape_string($row_data[1]);

            // Insert data into the database table (change "your_table_name" to your table name)
            $sql = "INSERT INTO csv_table (value1, value2) VALUES ('$column1', '$column2');";
            $conn->query($sql);
        }

        // Close the database connection
        $conn->close();
        echo "<br> CSV data has been successfully uploaded and inserted into the database.";
    } else {
        echo "Please select a valid CSV file.";
    }
}

?>