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";
}
?>
請不要使用'mysql_'函數,因爲它們在被棄用的過程中;而不是熟悉'mysqli'或'PDO'。 –
另外,請確保你從'$ _POST'淨化了用戶輸入,否則你將自己打開[SQL Injection](http://en.wikipedia.org/wiki/SQL_injection) – Bogdan
'