'Why is my form not submitting data into my database?
I have ensured this page is connecting to my database, but for some reason it is not submitting any data into the database and I'm not sure why.
I am fairly new to PHP and MYSQL, so any help would be appreciated.
If there is a better/more secure way, any information would help, as all the research I've found online/tutorials suggest different approaches.
I'm simply trying to create a register page, that submits data into my local MYSQL server.
<?php
require_once "connection.php";
session_start(); //intiate session for current user on site
if (isset($_SESSION["email"]) && isset($_SESSION["password"])) {
$email = filter_input(INPUT_POST, "email");
$password = filter_input(INPUT_POST, "password");
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
//database entry
$create_account = $pdo->prepare("INSERT INTO users (email,password) VALUES (:email, :password)");
$create_account->bindParam(':email', $email);
$create_account->bindParam(':password', $hashed_password);
$create_account->execute();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Food Roulette</title>
<link rel="stylesheet" href="style.css">
</head>
<body class="colorThis">
<div class="error"></div>
<div class="oneForm">
<form action="index.php" id="signupForm" method="POST">
<h1 class="register"> Register now </h1><br>
<label class="emailOne" for="email"> Email Address </label><br>
<input type="email" id="email" name="email" required><br>
<label class="passwordOne" for="password"> Password </label><br>
<input type="password" id="password" name="password" required><br>
<button class="registerBtn" name="register_button"> Login </button>
</form>
</div>
<script src="app.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$('#signUpForm').submit(function() {
$.post("register.php", $("#signupForm").serialize(), function(data) {});
return false;
});
</script>
</body>
</html>
connection.php
<?php
$host = 'localhost';
$db = 'getrecipes';
$user = 'root';
$pass = '';
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,//fetches row
PDO::ATTR_EMULATE_PREPARES => false,
];
$dsn = "mysql:host=$host;dbname=$db";
try {
$pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
?>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
