Reputation: 1130
I have written a very basic HTML sign-up form:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Registration</title>
</head>
<body>
<form name="reg" action="code_exec.php" onsubmit="return validateForm()" method="post">
<table width="274" border="0" align="center" cellpadding="2" cellspacing="0">
<tr>
<td colspan="2">
<div align="center">
<?php
// $remarks=$_GET['remarks'];
if (!isset($_GET['remarks']))
{
echo 'Register Here';
}
if (isset($_GET['remarks']) && $_GET['remarks']=='success')
{
echo 'Registration Success';
}
?>
</div></td>
</tr>
<tr>
<td width="95"><div align="right">First Name:</div></td>
<td width="171"><input type="text" name="fname" /></td>
</tr>
<tr>
<td><div align="right">Last Name:</div></td>
<td><input type="text" name="lname" /></td>
</tr>
<tr>
<td><div align="right">Gender:</div></td>
<td><input type="text" name="gender" /></td>
</tr>
<tr>
<td><div align="right">Address:</div></td>
<td><input type="text" name="address" /></td>
</tr>
<tr>
<td><div align="right">Contact No.:</div></td>
<td><input type="text" name="contact" /></td>
</tr>
<tr>
<td><div align="right">Username:</div></td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td><div align="right">Password:</div></td>
<td><input type="text" name="password" /></td>
</tr>
<tr>
<td><div align="right"></div></td>
<td><input name="submit" type="submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>
I am using PHP to connect my HTML page to a MySQL database on XAMPP server installed/configured locally.
I have divided my connection, and data storage logic across 2 different .php
files:
<?php
$mysql_hostname = "localhost";
$mysql_user = "amitesh";
$mysql_password = "amitesh";
$mysql_database = "sign_up_form";
$prefix = "";
$bd = mysqli_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysqli_select_db($mysql_database,$bd) or die("Could not select database");
?>
<?php
session_start();
include('connection.php');
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$mname=$_POST['mname'];
$address=$_POST['address'];
$contact=$_POST['contact'];
$username=$_POST['username'];
$password=$_POST['password'];
mysqli_query($bd, "INSERT INTO member(fname, lname, gender, address, contact, username, password)VALUES('$fname', '$lname', '$mname', '$address', '$contact','$username', '$password')");
header("location: index.php?remarks=success");
mysqli_close($bd);
After I insert data in the page, when I click on the "submit" button, it shows me the code of the code_exec.php
file instead of storing the data in MySQL.
I did see couple of StackOverflow posts about this, but none of them seem to offer a working solution for my problem.
Why could this be happening?
Upvotes: 0
Views: 281
Reputation: 8742
If the <?php
tags and actual website code are being displayed on the page, then there are only two scenarios:
Since you have confirmed that XAMPP is running, I will cover scenario two, which is the most likely.
Inside of your XAMPP installation, you should find a folder called htdocs
. This is the folder which should contain any websites that you make, and all of their PHP code - it acts as the root directory of your web server (if you were using a dedicated server, it is the equivalent of /var/www/html/
). The server only processes files which are contained within that folder.
If your files are not inside of that folder, move them into it (and delete / move to a different folder any files that where previously in that directory).
Next, you can't just open those files in your web browser once they are in the folder - you have to navigate to them via your locally hosted website.
Go to the URL 127.0.0.1 or localhost to visit your locally hosted website. This is only accessible to you, on your computer, while XAMPP is running, and it displays the documents in the htdocs
folder. You can choose a file in that list to visit it, and it should now run on your web server.
From now on, it should be fairly easy for you to work with files on your local web server. Of course, there could still be a PHP error in your code (I checked over it quickly - everything appears to be fine, but I could have missed something) - if that is the case and you get stuck with debugging, you can always ask another question here on StackOverflow!
As a bonus, you should be able to visit 127.0.0.1/phpmyadmin/ with XAMPP running to manage your MySQL tables and databases via a web interface.
Upvotes: 1
Reputation: 31
Try to build your connection like this:
Mysqli_connect($mysql_hostname, $mysql_user, $mysql_password, $mysql_database) or die(mysqli_error)
Upvotes: 0
Reputation: 606
note that you can not use php builtin server in this case. because your index.php file not is a front controller. copy your code in XAMPP htdocs directory and access your code with localhost/your_directory_name url.
and note that you have an error in your connection.php file.
in mysqli_select_db() function first parameter is mysqli link and second parameter is dbname.
replace
mysqli_select_db($mysql_database,$bd) or die("Could not select database");
with
mysqli_select_db($bd, $mysql_database) or die("Could not select database");
Upvotes: 0