Hello I've created a database in myPHPadmin and I'm trying to send registration data over there via an HTML form. I've created the connection files for the server, and have tested them. It says I'm connected to the database but when I'm using the form to send the data it doesn't work.
This is the connection file and the index one (the testing that I'm connected to the data base:
<?php
function OpenCon()
{
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '0000';
$db = 'mydatabase';
$conn = new mysqli($dbhost, $dbuser, $dbpass, $db) or die ("Connect failed: %s\n". $conn -> error);
return $conn;
}
function CloseCon($conn)
{
$conn -> close();
}
?>
This is the index file. It states Connected Successfully once I run it.
<?php
include 'connection.php';
$conn = OpenCon();
echo "Connected Successfully";
CloseCon($conn);
?>
I've written the registration form in HTML and combined it via the action method as stated below:
<form id='register-form' action="database/reg.php" method='post'>
<input type="text" maxlength="15" name="usr" placeholder="Username" required>
<input type="text" name="fname" placeholder="First Name" required>
<input type="text" name="lname" placeholder="Last Name" required>
<input type="email" name="mail" placeholder="Email" required>
<input type="password" name="psw" placeholder="Password" required>
<input type="password" placeholder="Re Password" required>
<button type='submit'> Εγγραφή</button>
I've payed attention on where to place the files (the directories etc.) But once I enter the data it displays this (Mainly the PHP file that's supposed to run) Code of PHP instead of Alert Window
Can someone please help me or point out what I'm missing?
This is the file handling the transaction:
<?php
include 'connection.php';
$a=$_POST["fname"];
$b=$_POST["lname"];
$c=$_POST["usr"];
$d=$_POST["psw"];
$e=$_POST["mail"];
$con = OpenCon();
$query="INSERT INTO users(firstname,lastname,username,password,email) VALUES ('$a','$b','$c','$d','$e')";
if(mysqli_query($con, $query)){
echo "<script>alert('Success') </script>";
}
CloseCon($con);
?>