【发布时间】:2019-07-21 07:13:54
【问题描述】:
我在通过 html 表编辑 mysql 数据库时遇到问题。
当我运行一个简单的 while 循环时,它可以工作,我可以拉入数据并更新数据库:
工作代码
<?php
while ($row = mysqli_fetch_array($res))
echo "$row[id]. $row[Key_Role] .$row[Incumbant] .$row[Attrition_Risk] .$row[Ready_Now] .$row[lowerYears] .$row[higherYears]<a href='edit.php?edit=$row[id]'>edit</a> <br />";
?>
但是,当我通过表运行 php 时,编辑链接不再从 mysql 中提取数据,并且我无法更新信息。我已将索引放在下面并编辑 php 文件。
Index.php
<?php
include_once('db.php');
if(ISSET($_POST['Key_Role']))
{
$Key_Role = $_POST['Key_Role'];
$Incumbant = $_POST['Incumbant'];
$Attrition_Risk = $_POST['Attrition_Risk'];
$Ready_Now = $_POST['Ready_Now'];
$lowerYears = $_POST['1-2_Years'];
$higherYears = $_POST['3-5_Years'];
$sql = "INSERT INTO tmdata VALUES('','$Key_Role','$Incumbant','$Attrition_Risk','$Ready_Now','$lowerYears','$higherYears')";
$res = mysqli_query($conn, $sql);
echo "<meta http-equiv='refresh' content='0;url=index.php'>";
if ($res)
echo "<meta http-equiv='refresh' content='0;url=index.php'>";
else
echo "Failed";
} else {
echo "please enter a key Role";
}
$res = mysqli_query($conn, "SELECT * FROM tmdata");
?>
<style>
<?php include 'style.css' ?>
</style>
<H1 class="Title">Talent Management System</H1>
<form action="." method="post">
Key Role:<input type="text" name="Key_Role">
Incumbant:<input type="text" name="Incumbant">
Attrition_Risk:<input type="text" name="Attrition_Risk">
Ready_Now:<input type="text" name="Ready_Now">
1-2_Years:<input type="text" name="1-2_Years">
3-5_Years:<input type="text" name="3-5_Years">
<input type ="submit" value="Enter">
</form>
<h1> List Of Key Roles</h1>
<?php
/*
while ($row = mysqli_fetch_array($res))
echo "$row[id]. $row[Key_Role] .$row[Incumbant] .$row[Attrition_Risk] .$row[Ready_Now] .$row[lowerYears] .$row[higherYears]<a href='edit.php?edit=$row[id]'>edit</a> <br />";
*/
?>
<table>
<tr>
<th>id</th>
<th>Key_Role</th>
<th>Incumbant</th>
<th>Attrition_Risk</th>
<th>Ready_Now</th>
<th>1-2_Years</th>
<th>3-5_Years</th>
<th>Edit</th>
</tr>
<?php while ($row = mysqli_fetch_array($res)):;?>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['Key_Role'];?></td>
<td><?php echo $row['Incumbant'];?></td>
<td><?php echo $row['Attrition_Risk'];?></td>
<td><?php echo $row['Ready_Now'];?></td>
<td><?php echo $row['lowerYears'];?></td>
<td><?php echo $row['higherYears'];?></td>
<td><a href='edit.php?edit=$row["id"]'>edit</a></td>
</tr>
<?php endwhile;?>
</table>
Edit.php
<?php
include_once('db.php');
if (isset($_GET["edit"]))
{
$id = $_GET["edit"];
$res = mysqli_query($conn, "SELECT * FROM tmdata WHERE id='".$id."'");
$row = mysqli_fetch_array($res);
}
if( isset($_POST['newKey_Role']) || isset($_POST['newIncumbant']) || isset($_POST['newAttrition_Risk']) || isset($_POST['newReady_Now']) || isset($_POST['newLowerYears']) || isset($_POST['newHigherYears']) )
{
$newKey_Role = $_POST['newKey_Role'];
$newIncumbant = $_POST['newIncumbant'];
$newAttrition_Risk = $_POST['newAttrition_Risk'];
$newReady_Now = $_POST['newReady_Now'];
$newLowerYears = $_POST['newLowerYears'];
$newHigherYears = $_POST['newHigherYears'];
$id = $_POST['id'];
$sql = "UPDATE tmdata SET Key_Role='$newKey_Role', Incumbant='$newIncumbant', Attrition_Risk='$newAttrition_Risk', Ready_Now='$newReady_Now', lowerYears='$newLowerYears', higherYears='$newHigherYears' WHERE id='".$id."'";
$res = mysqli_query($conn, $sql) or die(mysqli_error($conn));
echo "<meta http-equiv='refresh' content='0;url=index.php'>";
}
?>
<form action="edit.php" method="post">
Key Role:<input type="text" name="newKey_Role" value ="<?php echo $row[1]; ?>"></input> <br />
Incumbant:<input type="text" name="newIncumbant" value ="<?php echo $row[2]; ?>"></input> <br />
Attrition_Risk:<input type="text" name="newAttrition_Risk" value ="<?php echo $row[3]; ?>"></input> <br />
Ready_Now:<input type="text" name="newReady_Now" value ="<?php echo $row[4]; ?>"></input> <br />
1-2 Years:<input type="text" name="newLowerYears" value ="<?php echo $row[5]; ?>"></input> <br />
3-5 Years:<input type="text" name="newHigherYears" value ="<?php echo $row[6]; ?>"></input> <br />
<input type="hidden" name="id" value ="<?php echo $row[0]; ?>">
<input type ="submit" value="Update">
</form>
<style>
<?php include 'style.css' ?>
</style>
看起来好像 ID 没有拉到编辑 php.ini 中。我也尝试在表格中手动输入 id:
<td><a href='edit.php?edit=$row[184]'>edit</a></td>
但这也没有用。
任何帮助将不胜感激。
提前谢谢你。
【问题讨论】:
-
在 where 行中有
.点,因为你想要输出中的点还是你认为你正在连接 -
这些点有助于显示所有输出是否都显示在工作示例中,因为如果 sql 表中没有数据,则字段不会显示。不起作用的部分是,当我单击编辑时,没有填充任何字段,并且当我更新字段时,它不会更新 sql 数据库。是的,表单用于添加新行,编辑按钮用于编辑表中已存在的数据。所以我在一行上单击编辑,当它应该显示该行的表中的信息时,表单字段是空白的。它在不显示为表格时确实有效。
-
当您单击一个锚点
<a href='edit.php?edit=$row[id]'>edit</a>时,传递给 PHP 的所有内容就是您放在查询字符串上的内容。在这种情况下,只会传递$_GET['edit']。它被传递给$_GET数组而不是$_POST数组 -
当您单击 FORM 中的提交按钮时,所有
<input>值(具有name="xxx"属性)都会传递到$_POST数组 -
是的,所以这应该传递该行的 id。然后在编辑中应该在 GET 中选择它。但是,它看起来好像没有拾取该 ID。但是,当我在一个简单的 while 循环中对链接执行相同操作时。它有效。