我有一个 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();
?>
这是执行更新的脚本我认为我的问题是这个脚本
在这里但不确定在哪里。我相信我可能会错过一个while循环。
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";
}
?>