I am trying to add data into a MySql Database using PHP.
The code:
<?php
session_start();
include('config.php');
if(isset($_POST['Submit'])) {
$city = $_POST['city'];
$from_station = $_POST['from_station'];
$from_date = $_POST['from_date'];
// Prepare and execute the SQL query
$sql = "INSERT INTO journeys (city, from_station, from_date) VALUES ('$city', '$from_station', '$from_date')";
$result = $conn->query($sql);
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
?>
<!DOCTYPE html>
<html lang="pt_PT">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>IR PLANNER</title>
<link rel="stylesheet" href="Stylesgeneral_styles.css">
<link rel="stylesheet" href="Stylescitymanager_styles.css">
</head>
<body>
<form class="modal" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<label for="city">City:</label>
<input type="text" name="city" id="city">
<label for="from_station">From Station:</label>
<input type="text" name="from_station" id="from_station">
<label for="from_date">From Date:</label>
<input type="date" name="from_date" id="from_date">
<input type="submit" value="Submit" id="Submit">
</form>
</body>
</html>
When the submit button is pressed no error is displayed. However, when the database table is checked nothing appears.
I have read articles and the sql connection (config.php) is working perfectly (in other pages and files is working) and the sql query is also good.
Is anyone facing this problem?