但是,当我通过表运行 ph-6ren">
gpt4 book ai didi

php - 编辑表中的链接未连接到mysql

转载 作者:行者123 更新时间:2023-11-29 16:14:56 25 4
gpt4 key购买 nike

我在通过 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 文件。

索引.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>

编辑.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>

但这也不起作用。

任何帮助将不胜感激。

提前谢谢您。

最佳答案

我发现你需要将链接放在 php.ini 中。

<td><?php echo "<a href='edit.php?edit=$row[id]'>edit</a>"?></td>

最终成功了。

关于php - 编辑表中的链接未连接到mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54907613/

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