0

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>

(this part of the code displays the table and its values)

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)

?>

(this part is for updating the table)

I have got the code to update the contents of a table using buttons however I would like to just have one button that will update the whole table. At the moment I use a button per row to update that particular row.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 1
    Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Jul 11 '17 at 11:14

1 Answers1

-1

Just use this

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