简体   繁体   English

HTML注册表格不保存数据在MySQL

[英]html sign up form not saving data in mysql

I have written a very basic HTML sign-up form: 我写了一个非常基本的HTML注册表格:

<!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. 我正在使用PHP将HTML页面连接到本地安装/配置的XAMPP服务器上的MySQL数据库。

I have divided my connection, and data storage logic across 2 different .php files: 我将连接和数据存储逻辑划分为2个不同的.php文件:

connection.php connection.php

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

code_exec.php code_exec.php

<?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. 在页面中插入数据后,当我单击“提交”按钮时,它将向我显示code_exec.php文件的代码,而不是将数据存储在MySQL中。

I did see couple of StackOverflow posts about this, but none of them seem to offer a working solution for my problem. 我确实看到了几个有关此的StackOverflow帖子,但是似乎没有一个可以为我的问题提供有效的解决方案。

Why could this be happening? 为什么会这样呢?

Introduction 介绍

If the <?php tags and actual website code are being displayed on the page, then there are only two scenarios: 如果<?php标签和实际的网站代码显示在页面上,则只有两种情况:

  1. XAMPP is not working, or; XAMPP无法正常工作,或者;
  2. You are not navigating to a file at the web address 127.0.0.1 . 您没有导航到网址127.0.0.1的文件。

Since you have confirmed that XAMPP is running, I will cover scenario two, which is the most likely. 由于您已经确认XAMPP正在运行,因此我将介绍最有可能的情况二。

You are not navigating to a file at the web address 127.0.0.1 您没有导航到网址为127.0.0.1的文件

Inside of your XAMPP installation, you should find a folder called htdocs . 在XAMPP安装的内部,您应该找到一个名为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/ ). 该文件夹应包含您创建的所有网站及其所有 PHP代码-它充当Web服务器的根目录(如果使用专用服务器,则它等效于/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. 转到URL 127.0.0.1localhost来访问本地托管的网站。 This is only accessible to you, on your computer, while XAMPP is running, and it displays the documents in the htdocs folder. 仅当XAMPP运行时,您才能在计算机上访问此文件,并且它会在htdocs文件夹中显示文档。 You can choose a file in that list to visit it, and it should now run on your web server. 您可以在该列表中选择一个文件来访问它,该文件现在应该在您的Web服务器上运行。

Summary 摘要

From now on, it should be fairly easy for you to work with files on your local web server. 从现在开始,使用本地Web服务器上的文件应该相当容易。 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! 当然,您的代码中仍然可能存在PHP错误(我迅速检查了它-一切似乎都很好,但是我可能错过了一些东西)-如果是这种情况,并且您陷入调试的困境,您总是可以询问另一个关于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. 另外,您应该能够运行XAMPP来访问127.0.0.1/phpmyadmin/ ,以通过Web界面管理MySQL表和数据库。

note that you can not use php builtin server in this case. 请注意,在这种情况下,您不能使用php内置服务器。 because your index.php file not is a front controller. 因为您的index.php文件不是前端控制器。 copy your code in XAMPP htdocs directory and access your code with localhost/your_directory_name url. 将您的代码复制到XAMPP htdocs目录中,并使用localhost / your_directory_name url访问您的代码。

and note that you have an error in your connection.php file. 并注意您的connection.php文件有错误。

in mysqli_select_db() function first parameter is mysqli link and second parameter is dbname. 在mysqli_select_db()函数中,第一个参数是mysqli链接,第二个参数是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");

Try to build your connection like this: 尝试像这样建立连接:

Mysqli_connect($mysql_hostname, $mysql_user, $mysql_password, $mysql_database) or die(mysqli_error) Mysqli_connect($ mysql_hostname,$ mysql_user,$ mysql_password,$ mysql_database)或die(mysqli_error)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM