php - MySQL/PHP 数据库连接以从 Web 表单输入文本

标签 php html mysql sql arrays

我有一个简单的 Web 表单,我希望用户填写该表单,当他们单击提交按钮时,表单中的信息应插入到 mySQL 数据库中的正确表中以供以后使用。我有一个名为 foundation_fitness 的数据库,其中有一个名为 Client 的表。

我面临的问题是单击提交时出现此错误消息:

Notice: Undefined index: exampleInputEmail1 in C:\xampp\htdocs\ffitness\dist\new_client.php on line 19

Notice: Undefined index: forname1 in C:\xampp\htdocs\ffitness\dist\new_client.php on line 20

Notice: Undefined index: surname1 in C:\xampp\htdocs\ffitness\dist\new_client.php on line 21

Notice: Undefined index: height1 in C:\xampp\htdocs\ffitness\dist\new_client.php on line 22

Notice: Undefined index: weight1 in C:\xampp\htdocs\ffitness\dist\new_client.php on line 23

Notice: Undefined index: bodyfat1 in C:\xampp\htdocs\ffitness\dist\new_client.php on line 24

下面是我的 new_client.php 代码。 编辑:INSERT INTO 的 Foundation_fitness 更改为 Client

<?php

define('DB_NAME', 'foundation_fitness');
define('DB_USER', 'root');
define('DB_HOST', 'localhost');

$link = mysql_connect(DB_HOST, DB_USER);

if (!$link) {
     die('Could not connect: ' . mysql_error());
     }

     $db_selected = mysql_select_db(DB_NAME, $link);

     if (!$db_selected) {
     die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
     }

     $value2 = $_POST['exampleInputEmail1'];
     $value3 = $_POST['forname1'];
     $value4 = $_POST['surname1'];
     $value5 = $_POST['height1'];
     $value6 = $_POST['weight1'];
     $value7 = $_POST['bodyfat1'];

     $sql = "INSERT INTO client (Client_email, Client_forename, Client_surname, Height, Weight, Body_fat) VALUES ('$value2', 
     '$value3', '$value4', '$value5', '$value6')";
    
    $result = mysql_query($sql);

     mysql_close();

我的 html 表单的代码如下:编辑 - 将 method 错误和 id 更改为 name

<form action="new_client.php" method="POST">
        <div class="form-group">
          <label for="exampleInputEmail1">Email address</label>
          <input type="email" class="form-control" name="exampleInputEmail1" placeholder="Enter email">
        </div>
        <div class="form-group">
          <label for="forename1">Forename</label>
          <input type="text" class="form-control" name="forname1" placeholder="Enter First Name">
        </div>
        <div class="form-group">
          <label for="surname1">Surname</label>
          <input type="text" class="form-control" name="surname1" placeholder="Enter Surname">
        </div>
        <div class="form-group">
          <label for="height1">Height (cm)</label>
          <input type="text" class="form-control" name="height1" placeholder="Enter Height">
        </div>
        <div class="form-group">
          <label for="weight1">Weight (kgs)</label>
          <input type="text" class="form-control" name="weight1" placeholder="Enter Weight">
        </div>
        <div class="form-group">
          <label for="bodyfat1">Body Fat %</label>
          <input type="text" class="form-control" name="bodyfat1" placeholder="Enter Body Fat Percentage">
        </div>
        <button type="submit" class="btn btn-default">Submit</button>
      </form>

任何有关错误和让 php 脚本正常工作的帮助将不胜感激!

编辑:我现在没有收到错误消息,因为我将方法错误和 ID 更改为 HTML 表单中的名称。即使额外更改了应该是客户端的表名称,我仍然无法将信息获取到 mySQL 数据库上的表中,因为我没有名为 Foundation_fitness 的表(即数据库名称)。

最佳答案

说明 $_POST 中没有这样的键大批。您应该使用name id 的属性(不是 form )元素(如果您想稍后将它们用作 $_POST 中的键)大批。另外,你有一个错字:mehtod应该是method form 中的属性元素。

关于php - MySQL/PHP 数据库连接以从 Web 表单输入文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28075848/

相关文章:

javascript - 将共享的 'code_block' 从 loc-A 移动到 loc-B,只有一个 'code_block' 的写入实例

mysql - 如何按范围对 MySQL 查询进行排序?

php - 如何识别网页的真实访问

php - 具有多个连接的分页查询

javascript - 从 HTML 表中的 JSON url 加载数据,不重复

mysql - 如何在 Rails 中测试或调试我的迁移脚本?

mysql - 为了最小化笛卡尔积,在 FROM 子句中使用子查询是否更好?

php - 帮助加入

php - ?使用表中的 COUNT 和具有相关数据的另一个表中的 SUM

javascript - 相对于屏幕宽度更改 div 的可见性