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条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?