dtjo51649 2017-08-14 17:21
浏览 24
已采纳

html注册表单不保存mysql中的数据

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:

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

<?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?

  • 写回答

3条回答 默认 最新

  • dou5454954610 2017-08-15 09:52
    关注

    Introduction

    If the <?php tags and actual website code are being displayed on the page, then there are only two scenarios:

    1. XAMPP is not working, or;
    2. You are not navigating to a file at the web address 127.0.0.1.

    Since you have confirmed that XAMPP is running, I will cover scenario two, which is the most likely.

    You are not navigating to a file at the web address 127.0.0.1

    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.

    Summary

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式