0

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);

?>
Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
  • The issue apparently is not that you cannot connect to the database, but that you code does not get interpreted. So please add more information about what file(s) is/are responsible for processing the data. – arkascha Jul 04 '18 at 18:34
  • @arkascha the file I'm showing in the picture(Code of PHP instead of Alert Window) supposedly sends the data to the database – Myrtle Winchester Jul 04 '18 at 18:38
  • No, that picture shows some code, but not what file that comes from and how your setup is. – arkascha Jul 04 '18 at 18:51
  • Check the extension of the file containing the php code. It must be `.php` – Prince Brucex Jul 04 '18 at 18:55
  • @PrinceBrucex It is .php – Myrtle Winchester Jul 04 '18 at 18:58
  • Possible duplicate of [PHP code is not being executed, instead code shows on the page](https://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-instead-code-shows-on-the-page) – Nigel Ren Jul 04 '18 at 19:02
  • You are probably trying to run the code from your code editor. Go to the browser directly and type URL of the file to see if it shows same problem e.g localhost/index.php – Prince Brucex Jul 04 '18 at 19:03
  • Once ran through the url it performs well, but it needs to be accessed via the webpage. Localhost/index.php works fine, also Localhost/reg.php works fine as well but not when accessed via the form @PrinceBrucex – Myrtle Winchester Jul 04 '18 at 19:15
  • Your form does not have the closing tag `` . is it the same as on your HTML file? That could be a culprit – Prince Brucex Jul 04 '18 at 19:28
  • @PrinceBrucex no no it does have it – Myrtle Winchester Jul 04 '18 at 19:32
  • Your `database/reg.php` should produce error when your access it directly from the browser. It should give you `undefined index`error. If you are not getting error, then you are not accessing that file in the form `action` attribute. Make sure the `action` attribute is referring to the file which you said worked well when you accessed it directly via the browser. To fix the `undefined index` error, do this `if($_SERVER['REQUEST_METHOD'] == "POST"){//COPY ALL THE CODE HERE}` – Prince Brucex Jul 04 '18 at 19:48

2 Answers2

1

You must use mysqli_query instead of mysql_query:

if(mysqli_query($con, $query)) {

because you've connected through mysqli.

MrSmile
  • 1,217
  • 2
  • 12
  • 20
0

I think your query for inserting data is syntactically wrong.It should be like this...

You have to enclose the column name inside backtick symbol i.e (``)