2017-07-11 70 views
0

table.php更新基於ID SQL表的多個行如何使用一個按鈕

<?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) 

?> 

(這部分是用於更新表)

我已經得到了代碼更新表全光照的內容g按鈕,但我想只有一個按鈕,將更新整個表。此刻,我每行使用一個按鈕來更新該特定行。

+1

你的腳本是[SQL注入攻擊]的風險(http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in -php) 看看發生了什麼事[Little Bobby Tables](http://bobby-tables.com/)即使是 [如果你是逃避投入,它不安全!](http://stackoverflow.com/questions/5741187/sql -injection-that-around-mysql-real-escape-string) 使用[prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements .php) – RiggsFolly

回答

-1

只要使用這個

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

非常容易出現SQL注入攻擊.. :) –

+0

這樣做n爲我的代碼工作,但謝謝你 –