有人可以告訴我是我寫錯了嗎?此代碼顯示來自數據庫的數據,但當我按下提交它只是重新加載頁面,並沒有那些字段的更新數據庫... 我應該更新更新查詢中的變量,例如:$ _POST ['name']更新MySQL數據庫中的多行時遇到的問題
<?php
$host="localhost"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="db"; // Database name
$tbl_name="test"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
// Count table rows
$count=mysql_num_rows($result);
?>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="POST" action="">
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>mid</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
$id[]=$rows['id'];
?>
<tr>
<td align="center">
<input name="id[]" type="text" id="id" value="<? echo $rows['id']; ?>">
</td>
<td align="center">
<input name="name[]" type="text" id="name" value="<? echo $rows['name']; ?>">
</td>
<td align="center">
<input name="lastname[]" type="text" id="lastname" value="<? echo $rows['lastname']; ?>">
</td>
<td align="center">
<input name="midmark[]" type="text" id="midmark" value="<? echo $rows['midmark']; ?>">
</td>
</tr>
<?
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
if(isset($_POST['submit'])){
for($i=0;$i<$count;$i++){
$sql1=mysql_query("UPDATE $tbl_name SET name='$name[$i]', lastname='$lastname[$i]', midmark='$midmark[$i]' WHERE id='$id[$i]' ");
$result1=mysql_query($sql1);
}
}
if(isset($result1)){
header("location:multiple.php");
}
mysql_close();
?>
添加索引到輸入端米更容易更新,即name =「id [<? echo $ rows ['id']; '']' – Waygood 2013-03-04 12:34:29
'isset($ _ POST ['submit'])'it is'isset($ _ POST ['Submit'])'Next這裏是什麼名字? – 2013-03-04 12:36:51
相信你永遠不會在你的查詢中設置變量。此外,你的代碼是SQL注入,使用的mysqli或PDO敞開,而不是 – AmazingDreams 2013-03-04 12:37:58