如何使用一个按钮基于id更新SQL表中的多行

时间:2017-07-11 11:12:40

标签: php html sql html-table

table.php

<?php

include('../connections/conn.php');
include('../php/login.php');

$sql = "SELECT * FROM person";

$records = mysqli_query($conn, $sql)

?>

<html>
<head>
    <title>Table</title>
</head>
<body>
    <table>
<tr>

<th>Name</th>
<th>Age</th>
<th>Salary</th>
</tr>
<?php
while($row = mysqli_fetch_array($records)){
$name = $row['Name'];
$age = $row['Age'];
$salary = $row['Salary'];
$id = $row['id'];

echo "<tr><form action=update.php method=post>";
echo "<td><input type=text name=pname value='$name'></td>";
echo "<td><input type=text name=age value='$age'></td>";  
echo "<td><input type=text name=salary value='$salary'></td>";
echo "<input type=hidden name=id value='$id'></td>";
echo "<td><input type=submit>";
echo "</form></tr>";
}
?>
</table>

</body>
</html>

(代码的这一部分显示表及其值)

Update.php

<?php

include('../connections/conn.php');
include('../php/login.php');

$sql = "UPDATE person SET 
Name='$_POST[pname]',Age='$_POST[age]',Salary='$_POST[salary]' WHERE 
id='$_POST[id]'";

if(mysqli_query($conn, $sql)){
header("refresh:1 url=table.php");

}
else{
echo"Not Update";
}
$records = mysqli_query($conn, $sql)

?>

(这部分用于更新表格)

我有代码使用按钮更新表的内容但是我想只有一个按钮来更新整个表。目前我每行使用一个按钮来更新该特定行。

1 个答案:

答案 0 :(得分:-1)

请使用此

$sql = "UPDATE person SET 
Name='$_POST[pname]',Age='$_POST[age]',Salary='$_POST[salary]' WHERE 
id in('$_POST[id]')";