Skip to content

Commit a3738c5

Browse files
committed
added con
1 parent 39d873c commit a3738c5

File tree

6 files changed

+34
-4
lines changed

6 files changed

+34
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea/

adddata.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
global $conn;
3-
require_once "con.php"; // Assuming this file contains the PDO connection
3+
require_once "conn.php"; // Assuming this file contains the PDO connection
44

55
if (isset($_POST['submit'])) {
66
$name = $_POST['name'];

conn.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
// Define database credentials (consider using environment variables for sensitive data)
4+
define('DB_SERVER', 'mysql'); // Database server (e.g., 'localhost')
5+
define('DB_USERNAME', 'youruser'); // Your database username
6+
define('DB_PASSWORD', 'yourpassword'); // Your database password
7+
define('DB_NAME', 'yourdatabase'); // Your database name
8+
9+
// Set the Data Source Name (DSN)
10+
$dsn = "mysql:host=" . DB_SERVER . ";dbname=" . DB_NAME . ";charset=utf8mb4";
11+
12+
// PDO options to handle errors and set fetch mode
13+
$options = [
14+
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // Error handling as exceptions
15+
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, // Fetch data as associative arrays
16+
PDO::ATTR_EMULATE_PREPARES => false, // Disable emulation of prepared statements for better security
17+
];
18+
19+
try {
20+
// Create a new PDO instance
21+
$conn = new PDO($dsn, DB_USERNAME, DB_PASSWORD, $options);
22+
} catch (PDOException $e) {
23+
// Handle connection error (consider logging the error instead)
24+
error_log("Database connection error: " . $e->getMessage());
25+
die("ERROR: Could not connect to the database.");
26+
}
27+
28+
// Optional: Close the connection when done
29+
// $conn = null;

delete.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
// Include the database connection file
33
global $conn;
4-
require_once "con.php"; // Ensure this file establishes the PDO connection and defines $conn
4+
require_once "conn.php"; // Ensure this file establishes the PDO connection and defines $conn
55

66
// Check if 'id' is present in the GET request
77
if (isset($_GET["id"])) {

index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
<tbody>
6565
<?php
6666
global $conn;
67-
require_once "con.php";
67+
require_once "conn.php";
6868

6969
$sql_query = "SELECT * FROM results";
7070

updatedata.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
// Start PHP code before any HTML output
66
global $conn;
7-
require_once "con.php";
7+
require_once "conn.php";
88

99
$id = $_GET['id'] ?? null; // Get the ID from the URL, validate as necessary
1010

0 commit comments

Comments
 (0)