gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:39:44 27 4
gpt4 key购买 nike

我有一个简单的 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 表单的代码如下:EDIT - 将 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 脚本正常工作的帮助!

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

最佳答案

这意味着$_POST 数组中没有这样的键。如果您想稍后将它们用作 $_POST 中的键,您应该为 form 元素使用 name 属性(而不是 id) 数组。此外,您还有一个错字:mehtod 应该是 form 元素中的 method 属性。

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com