2012-10-15 70 views
1

我有一個PHP腳本,我寫過更新用戶是MySQL數據庫,但它不會更新。但它確實說成功這裏是腳本。用PHP格式更新MySQL

//This is the list script 
//Index.php script 

<?php 
$host="localhost"; // Host name 
$username="username"; // Mysql username 
$password="password"; // Mysql password 
$db_name="database"; // Database name 
$tbl_name="users"; // Table name 

// Connect to server and select database. 
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); 
?> 

<table width="400" border="0" cellspacing="1" cellpadding="0"> 
<tr> 
<td> 
<table width="400" border="1" cellspacing="0" cellpadding="3"> 
<tr> 
<td colspan="4"><strong>Users</strong> </td> 
</tr> 

<tr> 
<td align="center"><strong>Username</strong></td> 
<td align="center"><strong>Role</strong></td> 
<td align="center"><strong>Channels</strong></td> 
<td align="center"><strong>EMail</strong></td> 
<td align="center"><strong>Update</strong></td> 
<td align="center"><strong>Delete</strong></td> 
</tr> 

<?php 
while($rows=mysql_fetch_array($result)){ 
?> 

<tr> 
<td><?php echo $rows['Username']; ?></td> 
<td><?php echo $rows['Role']; ?></td> 
<td><?php echo $rows['Channels']; ?></td> 
<td><?php echo $rows['EMail']; ?></td> 


<td align="center"><a href="update.php?id=<?php echo $rows['ID']; ?>">Update</a></td> 
<td align="center"><a href="delete_ac.php?id=<?php echo $rows['ID']; ?>">Delete</a> 
</td> 
</tr> 

<?php 
} 
?> 

</table> 
</td> 
</tr> 
</table> 

<?php 
mysql_close(); 
?> 

這是更新用戶腳本獲取的用戶ID,並且允許用於更新
用戶。 update.php腳本

<?php 
$host="localhost"; // Host name 
$username="username"; // Mysql username 
$password="password"; // Mysql password 
$db_name="database"; // Database name 
$tbl_name="users"; // Table name 
// Connect to server and select database. 
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB"); 

// get value of id that sent from address bar 
$id=$_GET['id']; 

// Retrieve data from database 
$sql="SELECT * FROM $tbl_name WHERE id='$id'"; 
$result=mysql_query($sql); 

$rows=mysql_fetch_array($result); 
?> 

<table width="400" border="0" cellspacing="10" cellpadding="0"> 
<tr> 
<form name="form1" method="post" action="update_ac.php"> 
<td> 
<table width="100%" border="10" cellspacing="1" cellpadding="10"> 

<tr> 
<td colspan="3"><strong>Update User</strong> </td> 
</tr> 
<center> 
<tr> 
<td align="center"><strong>Username</strong></td> 
<td align="center"><strong>Password</strong></td> 
<td align="center"><strong>Role</strong></td> 
<td align="center"><strong>Channels</strong></td> 
<td align="center"><strong>EMail</strong></td> 
</tr> 
</center> 
<tr> 
<td align="center"> 
<input name="username" type="text" id="Username" value="<?php echo 
$rows['Username']; 
?>" size="15"> 
</td> 

<td align="center"> 
<input name="password" type="Password" id="Password" value="<?php echo  
$rows['Password']; ?>" size="15"> 
</td> 

<td> 
<input name="role" type="text" id="Role" value="<?php echo $rows['Role']; ?>" size="1"> 
</td> 

<td> 
<input name="channels" type="text" id="Channels" value="<?php echo $rows['Channels']; 
?>" size="10"> 
</td> 


<td> 
<input name="EMail" type="text" id="EMail" value="<?php echo $rows['EMail']; ?>" 
size="25"> 
</td> 

<tr> 
<td> 
<input name="id" type="hidden" id="ID" value="<?php echo $rows['ID']; ?>"> 
</td> 
<td align="center"> 
<input type="submit" name="Submit" value="Submit"> 
</td> 

</tr> 
</table> 
</td> 
</form> 
</tr> 
</table> 

<?php 
// close connection 
mysql_close(); 
?> 

這是同名但我覺得我的問題是,這個腳本
在這裏,但不知道在哪裏的更新腳本。我相信我可能會錯過一段時間循環。

update_ac.php腳本

<?php 
$host="localhost"; // Host name 
$username="username"; // Mysql username 
$password="password"; // Mysql password 
$db_name="database"; // Database name 
$tbl_name="users"; // Table name 
// Connect to server and select database. 
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB"); 


$id = $_POST['ID']; 
$Username = $_POST['Username']; 
$Password = $_POST['Password']; 
$Role = $_POST['Role']; 
$Channels = $_POST['Channels']; 
$EMail = $_POST['EMail']; 


// update data in mysql database 
$sql = "UPDATE $tbl_name SET Username='$Username', Password='$Password',  
Role='$Role', Channels='$Channels', EMail='$EMail' WHERE id='$id'"; 
$result = mysql_query($sql); 

// if successfully updated. 
if($result) 
{ 

echo "Successful"; 
echo "<BR>"; 
echo "<a href='index.php'>View result</a>"; 

} 

else 
{ 
echo "ERROR"; 
} 

?> 
+2

請不要使用'mysql_'函數,因爲它們在被棄用的過程中;而不是熟悉'mysqli'或'PDO'。 –

+0

另外,請確保你從'$ _POST'淨化了用戶輸入,否則你將自己打開[SQL Injection](http://en.wikipedia.org/wiki/SQL_injection) – Bogdan

+0

'

'不能成爲孩子''。閱讀[這篇文章](http://stackoverflow.com/questions/1984599/is-a-form-valid-over-a-tr) – Bogdan

回答

3

做更新,加上什麼的評註談到的mysqli或PDO之前,您應該清理您的數據,但如果你改變了查詢行$result = mysql_query($sql) or die(mysql_error());它會告訴你這是怎麼回事錯誤。

+0

如果OP使用PDO,它會照顧你的衛生(不知道關於mysqli因爲我從來沒有用過它)。 –

3
從其他的答案已經做出,現在大概在腳本中實際的錯誤,看來你 update.php腳本中的所有重要的考慮因素

除此之外,你有一個提交按鈕:

<input type="submit" name="Submit" value="Submit"> 

但我不請參閱<form>元素,您需要將所有<input>元素包含在表單標記中,以便將它們發送到目標腳本。類似這樣的:

<form method="POST" action="update_ac.php"> 
// HERE ALL YOUR INPUT ELEMENTS 
</form> 
0

只有當查詢失敗時,mysql_query纔會返回FALSE。在你的情況下,查詢可能沒有失敗,但只更改了0行,請檢查mysql_affected_rows()(在這種情況下,腳本不應報告成功)。請參閱文檔http://php.net/manual/en/function.mysql-query.php