来自表单的数据未使用 php 添加到数据库表中
Posted
技术标签:
【中文标题】来自表单的数据未使用 php 添加到数据库表中【英文标题】:Data from form not getting added into the database table using php 【发布时间】:2020-02-22 10:29:03 【问题描述】:我正在使用 Xampp 服务器并希望将数据添加到数据库表中。我不知道我在这里做错了什么。服务器已启动并正在运行。选择框中的数据未插入到数据库中。我是 php 新手。
编辑:我已经编辑了我的代码。我收到此错误:
在此服务器上找不到请求的 URL。引用页面上的链接似乎是错误的或过时的。请将该错误告知该页面的作者。
Test.php
<form name="form2" action= "Sub.php" method="post">
<table style="width:100%">
<thead>
<tr>
<th style="width:25px;">Section</th>
<th style="width:250px;">Definition</th>
<th style="width:30px;">Sub Process</th>
<th style="width:35px;">Response</th>
<th style="width:30px;">Area</th>
<th style="width:30px;">Rating</th>
<th style="width:40px;">Comments</th>
<th style="width:40px;">Improvement Plan</th>
</tr>
</thead>
<tbody>
<tr class="data">
<td>1.</td>
<td style="width:500px;">Does Problem Management adheres to incident analysis and trending to identify the problem?</td>
<td>Problem Detection</td>
<td>
<select name="Response[]">
<option >--Select Function--</option>
<option value="Defined">Defined</option>
<option value="Defined and Followed Completely without Automation">
Defined and Followed Completely without Automation</option>
<option value="Defined and Followed Partially without Automation">
Defined and Followed Partially without Automation</option>
<option value="Not Defined">
Not Defined</option>
<option value="Process Present but not followed">
Process Present but not followed</option>
<option value="Yes">Yes</option>
<option value="Not Applicable">Not Applicable</option>
</select>
</td>
<td>
<select name="Area[]">
<option >--Select Area--</option>
<option value="Process">Process</option>
<option value="Tool">Tool</option>
</select>
</td>
<td>
<select name="Rating[]">
<option value="0">--Select Rating--</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</td>
<td>
<textarea style="width:80px;" name = "Comments[]" placeholder="Please Provide Comments"></textarea>
</td>
<td>
<textarea style="width:80px;" name = "ImpPlan[]" placeholder="Please suggest Improvements"></textarea>
</td>
</tr>
<tr class="data">
<td>2.</td>
<td style="width:500px;">Are reactive and proactive problem management understood by teams?</td>
<td>Problem Detection</td>
<td>
<select name="Response[]">
<option>--Select Function--</option>
<option value="Defined">Defined</option>
<option value="Defined and Followed Completely without Automation">
Defined and Followed Completely without Automation</option>
<option value="Defined and Followed Partially without Automation">
Defined and Followed Partially without Automation</option>
<option value="Not Defined">
Not Defined</option>
<option value="Process Present but not followed">
Process Present but not followed</option>
<option value="Yes">Yes</option>
<option value="Not Applicable">Not Applicable</option>
</select>
</td>
<td>
<select name="Area[]">
<option>--Select Area--</option>
<option value="Process">Process</option>
<option value="Tool">Tool</option>
</select>
</td>
<td>
<select name="Rating[]">
<option value="0">--Select Rating--</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</td>
<td>
<textarea style="width:80px;" name = "Comments[]" placeholder="Please Provide Comments"></textarea>
</td>
<td>
<textarea style="width:80px;" name = "ImpPlan[]" placeholder="Please suggest Improvements"></textarea>
</td>
</tr>
</table>
<br><br><br>
<input type="submit" value="Submit">
</form>
子.php
<?php
$Response1=$_POST['Response'][0];
$Area1=$_POST['Area'][0];
$Rating1=$_POST['Rating'][0];
$Response2=$_POST['Response'][1];
$Area2=$_POST['Area'][1];
$Rating2=$_POST['Rating'][1];
$Comments=$_POST['Comments'];
$ImpPlan=$_POST['ImpPlan'];
$servername= 'localhost';
$username = 'root';
$password = '';
$dbname = 'db1';
$port = 3306;
$conn = new mysqli($servername, $username, $password, $dbname, $port);
echo "Connection successful!" . "<bc>";
$conn->query('CREATE TABLE IF NOT EXISTS `problemdet` (Response VARCHAR(100) NOT NULL, Area VARCHAR(100) NOT NULL, Rating VARCHAR(15) NOT NULL, Comments VARCHAR(100) NOT NULL, ImprovementPlan VARCHAR(100) NOT NULL);');
$sql = "INSERT INTO problemdet (Response, Area, Rating, Comments, ImprovementPlan)
VALUES ('$Response1', '$Response2', '$Area1', '$Area2', '$Rating1','$Rating2','$Comments', '$ImpPlan')";
if($conn->query($sql) === TRUE)
echo "New record created successfully";
else
echo "<bc> Error: " . $sql . "<br>" . $conn->error;
$conn->close();
?>
【问题讨论】:
【参考方案1】:在 Sub.php 中,您在 $Comments 的这一行中错过了引号 '
$sql = "INSERT INTO problemdet (Response, Area, Rating, Comments, ImprovementPlan) VALUES ('$Response', '$Area', '$Rating',
'$Comments', '$ImpPlan')";
在 Test.php 中,您错过了 select 的 name 属性并将其放在选项标签中,因此将其更改为
<select name="Response[]">
<option >--Select Function--</option> ...
来自
<select>
<option name="Response">--Select Function--</option> ...
在 Test.php 中,您有多个具有相同名称属性的输入,而在 Sub.php 中,您只会得到一个值,因此将 name 属性中的名称更改为数组
<select name="Response[]"> ...
<textarea name = "Comments[]"> ...
然后:
$_POST['Response'][0]
$_POST['Response'][1]
$_POST['Comments'][0]
$_POST['Comments'][1]
//and so on if you have to many you can write it in a loop
编辑说明
您应该指定名称属性(在您的情况下为响应)是一个数组的浏览器(因为有多个具有相同名称属性的选择输入 - 响应,因此它应该是响应 [])
比在 php 中你应该从数组中一一获取值 (比如 $_POST['Response'][0],$_POST['Response'][1],.. upto count($_POST['response']))
你的例子
<select name="Response[]">
<option value="first_0">first</option>
<option value="second_0">second</option>
<option value="third_0">third</option>
</select>
<select name="Response[]">
<option value="first_1">first</option>
<option value="second_2">second</option>
<option value="third_3">third</option>
</select>
如果您从第一个选择中选择第三个_0,从第二个选择中选择第二个_2,那么当您在 php 中获取值时
echo $get_select_value_1 = $_POST['Response'][0];
echo $get_select_value_2 = $_POST['Response'][1];
//output will be
third_0 //--$get_select_value_1
second_2 //--$get_select_value_2
编辑 2 - 为同一列插入多个值
$Response1 = $_POST['Response'][0];
$Response2 = $_POST['Response'][1];
$sql = "INSERT INTO problemdet (Response) VALUES ('$Response1'),('$Response2')";//upto so on
编辑 3 - 测试代码
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<form name="form2" action= "register.php" method="post">
<select name="Response[]">
<option value="first_0">first</option>
<option value="second_0">second</option>
<option value="third_0">third</option>
</select>
<select name="Response[]">
<option value="first_1">first</option>
<option value="second_2">second</option>
<option value="third_3">third</option>
</select>
<input type="submit" name="submit">
</form>
</body>
</html>
sub.php
<?php
if(isset($_POST['submit']))
echo $Response1 = $_POST['Response'][0];
echo $Response2 = $_POST['Response'][1];
?>
【讨论】:
谢谢@PavanKumar 我一定会试试你的代码。 非常感谢@PavanKumar,您的更正就像一个魅力。您能否用我将用于选择属性的代码详细说明 Array 的事情。提前致谢! hai 我已经用我的话解释了希望您理解..如果您有任何疑问,您可以发表评论,以便我可以从一些链接中搜索参考 好吧,我只是想知道如何使用 php 在 mysql 数据库的不同行中发布这个数组值 我已经编辑了我的答案是你要求什么或者你要求动态循环?以上是关于来自表单的数据未使用 php 添加到数据库表中的主要内容,如果未能解决你的问题,请参考以下文章
我想将表单中的数据插入到表中,然后从另一个表中选择另一个数据并使用 PHP 插入到第三个表中