0

我有一个 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";
}

?> 
4

3 回答 3

3

You should sanitize your data before doing the update, plus what the commentor said about mysqli or PDO, but if you change the query line to $result = mysql_query($sql) or die(mysql_error()); it will tell you what's going wrong.

于 2012-10-15T22:09:07.320 回答
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>
于 2012-10-15T22:13:57.213 回答
0

mysql_query 只有在查询失败时才会返回 FALSE。在您的情况下,查询可能没有失败,但仅更改了 0 行,请检查 mysql_affected_rows() (在这种情况下,该脚本不应报告成功)。请参阅http://php.net/manual/en/function.mysql-query.php上的文档

于 2012-10-15T22:17:49.290 回答