Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking an HTML form to a SQLite database

Tags:

html

php

Hello and thank you for taking the time to go through my issue.

I am trying to make a working newsletter form for my website which should store a person's name and email address in a database. I do not expect there to be much data to store, so I have - somewhat blindly - chosen SQLite.

My folder contains an HTML file, a PHP file and a SQLite database:

This is my index.html file:

<h1>Newsletter Signup</h1>
<form method="POST" action="./submit.php">
    <label for="name">Name:</label>
    <input type="text" name="name" id="name" required>
    <label for="email">Email:</label>
    <input type="email" name="email" id="email" required>
    <input type="submit" value="Subscribe">
</form>

This is my submit.php file:

<?php

$db = new SQLite3('mydb.db');

if (!$db) {
    die("Error connecting to database.");
}

$name = $_POST['name'] ?? '';
$email = $_POST['email'] ?? '';

if (!$name || !$email) {
    die("Name and email are required.");
}

$stmt = $db->prepare('INSERT INTO subscribers (name, email) VALUES (:name, :email)');

if (!$stmt) {
    die("Error preparing statement.");
}

$stmt->bindValue(':name', $name);
$stmt->bindValue(':email', $email);

$result = $stmt->execute();

if (!$result) {
    die("Error executing statement.");
}

$db->close();

exit();

?>

The mydb.db SQLite database was created with the following lines of code:

CREATE TABLE subscribers (
    id INTEGER PRIMARY KEY,
    name TEXT,
    email TEXT
);

Whenever the 'Subscribe' button is clicked on, the following messages appear via DevTools in the console:

(In red) Failed to load resource: the server responded with a status of 405 () chrome-error://chromewebdata/:1
(In yellow) VM491:161 crbug/1173575, non-JS module files deprecated.
(anonymous) @ VM491:161

and network > headers tabs:

Status: (failed)net::ERR_HTTP_RESPONSE_CODE_FAILURE
Request Method: POST 
Status Code: 405 Method Not Allowed 
Referrer Policy: strict-origin-when-cross-origin
Allow: GET, HEAD, OPTIONS

I have not been able to find a way to fix this problem. I am, of course, a newbie, and would sincerely appreciate your help, so please do go into details! Thank you once again.

like image 521
NPM Avatar asked Oct 20 '25 12:10

NPM


1 Answers

Check for enable_post_data_reading variable is set to on in php.ini to verify is need to accept POST

If didn't work please comment in this answer and I'll glade to update my answer.

like image 66
Leandro Bardelli Avatar answered Oct 23 '25 01:10

Leandro Bardelli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!