@ryleigh
To insert the data from a .txt file into a database using PHP, you can follow these steps:
Here's an example code snippet to demonstrate how to insert data from a .txt file into a database:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?php // Read the contents of the .txt file $data = file_get_contents('data.txt'); // Parse the data and prepare it for insertion into the database $lines = explode(" ", $data); foreach ($lines as $line) { $fields = explode(",", $line); // Assuming data is comma-separated $field1 = $fields[0]; $field2 = $fields[1]; // Connect to the database $conn = new mysqli('localhost', 'username', 'password', 'database_name'); // Insert the data into the database $sql = "INSERT INTO table_name (field1, field2) VALUES ('$field1', '$field2')"; if ($conn->query($sql) === TRUE) { echo "Data inserted successfully"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); } ?> |
Note: Ensure that you replace the placeholders (e.g., 'localhost'
, 'username'
, 'password'
, 'database_name'
, 'table_name'
) with the actual values that correspond to your database configuration and table structure.