I am creating my CV website and am creating a contact me form at the bottom of my website. So I am using HTML, CSS, PHP and Java to help me with this. Basically, I want to create a database in myPHP within Hostinger as my server to view and download people’s entries. I am having trouble getting the HTML to communicate with PHP to make it work properly.
I’ve tried using formsubmit but it times out so frequently so I am opting to learn PHP and use this as a learning tool. My issue is I get a HTTP 405 or 500 error after I hit the submit button. Can someone help me with my PHP code please? I will post both HTML and PHP. The HTML code is in a file called form.html and the PHP code is inside the file dbnew.php. Thanks in advance
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="dbnew.php" method="POST">
<div class="input-box">
<div class="input-field">
<label for="fullName">
<input type="text" name="fullName" placeholder="Full Name" required>
<span class="focus"></span>
</label>
</div>
<div class="input-field">
<label for="email">
<input name="email" type="email" id="email" pattern="/^[a-zA-Z0-9.!#$%&*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:.[a-zA-Z0-9-]+)*$/" placeholder="Email Address" required>
<span class="focus"></span>
</label>
</div>
</div>
<div class="input-box">
<div class="input-field">
<label for="phone">
<input type="tel" id="phone" name="phone" placeholder="Phone Number" required>
<span class="focus"></span>
</label>
</div>
<div class="input-field">
<label for="emailSubject">
<input type="text" name="emailSubject" placeholder="Email Subject" required>
<span class="focus"></span>
</label>
</div>
</div>
<div class="textarea-field">
<textarea name="emailMessage" id="" cols="30" rows="10" name="message" placeholder="Your Message" required></textarea>
<span class="focus"></span>
</div>
<div class="btn-box btns">
<button type="submit" class="btn">Submit</button>
</div>
</form>
</body>
</html>
<?php
$fullName = $_POST['fullName'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$emailSubject = $_POST['emailSubject'];
$emailMessage = $_POST['emailMessage'];
if (!empty($fullName) || !empty($email) || !empty($phone) || !empty($emailSubject) || !empty($emailMessage)){
$servername = "https://mywebsite.com";
$database = "u292472425_mywebsite";
$username = "u42424823_username";
$password = "MyPassword#1234";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
if (mysqli_connect_error()) {
die('Connection error('. mysqli_connect_errno().')'. mysqli_connect_error());
}else{
$SELECT = "SELECT email From register Where email = ? Limit = 1";
$INSERT = "INSERT Into register (fullName, email, phone, emailSubject, emailMessage) values(?, ?, ?, ?, ?)";
$stmt = $conn->prepare($SELECT);
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->bind_result($email);
$stmt->store_result();
$rnum = $stmt->num_rows;
if ($rnum==0) {
$stmt->close();
$stmt = $conn->prepare($INSERT);
$stmt->bind_param("ssiss", $fullName, $email, $phone, $emailSubject, $emailMessage);
$stmt->execute();
echo "New record inserted successfully";
} else {
echo "Someone already registered using this email";
}
$stmt->close();
$conn->close();
}
} else{
echo "All fields are required";
die();
}
?>