douwuli4512 2015-01-21 20:02
浏览 62
已采纳

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

I have a simple web form, which I want the user to fill out and when they click the submit button the information from the form should be inserted into the correct tables in the mySQL database for later use. I have a database called foundation_fitness with a table called Client.

The problem I am facing is this error message upon clicking submit:

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

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

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

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

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

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

Below is my code for new_client.php. EDIT: Changed Foundation_fitness to Client for the INSERT INTO

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

And the code for my html form is as follows: EDIT - Changed method mistake and id to 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>

Any help with the error and getting the php script working would be much appreciated!

EDIT: I am now not getting the error message as I changed the method mistake and id to name in the HTML form. I am still not able to get the information into the table on the mySQL database even with the additional change of the table name which should have been client as I do not have a table called foundation_fitness that is the database name.

  • 写回答

3条回答 默认 最新

  • douxian1892 2015-01-21 20:03
    关注

    It means that there are no such keys in the $_POST array. You should use name attribute (not id) for the form elements if you want to use them later as keys in the $_POST array. Also, you have a typo : mehtod should be method attribute in your form element.

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

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退