使用PHP表单更新MySQL

使用PHP表单更新MySQL,php,mysql,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

我有一个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();
?>
//这是列表脚本
//Index.php脚本
用户
用户名
角色
频道
电子邮件
更新
删除
这是更新用户脚本,它获取用户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();
?>
<?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";
}

?> 

在进行更新之前,您应该清理您的数据,加上评论员对mysqli或PDO的评论,但是如果您将查询行更改为
$result=mysql\u query($sql)或die(mysql\u error())它会告诉你出了什么问题。

在进行更新之前,你应该清理你的数据,加上评论人对mysqli或PDO的评论,但是如果你将查询行更改为
$result=mysql\u query($sql)或die(mysql\u error())它会告诉您出了什么问题。

除了在其他答案中已经考虑的所有重要因素外,现在关于脚本中的实际错误,似乎在
更新.php
脚本中,您有一个提交按钮:

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

但是我看不出它包含在
元素中,您需要将所有
元素包含在表单标记中,以便将它们发送到目标脚本。大概是这样的:

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

//这里是所有的输入元素

除了在其他答案中已经考虑的所有重要因素外,现在关于脚本中的实际错误,在
update.php
脚本中,您似乎有一个提交按钮:

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

但是我看不出它包含在
元素中,您需要将所有
元素包含在表单标记中,以便将它们发送到目标脚本。大概是这样的:

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

//这里是所有的输入元素

mysql\u只有在查询失败时,查询才会返回FALSE。在您的情况下,查询可能没有失败,但只更改了0行,请检查mysql\u impacted\u rows()(在这种情况下,脚本不应报告成功)。请参阅mysql上的文档。只有在查询失败时,查询才会返回FALSE。在您的情况下,查询可能没有失败,但只更改了0行,请检查mysql\u impacted\u rows()(在这种情况下,脚本不应报告成功)。查看

上的文档,请不要使用
mysql
函数,因为它们正在被弃用;相反,要熟悉
mysqli
PDO
。还要确保您从
$\u POST
中清理了用户输入,否则您将面临另一个挑战
不能是
的孩子。Read请不要使用
mysql\uuz
函数,因为它们正在被弃用;相反,要熟悉
mysqli
PDO
。还要确保您从
$\u POST
中清理了用户输入,否则您将面临另一个挑战
不能是
的孩子。阅读如果OP使用PDO,它会为您提供卫生服务(我不确定mysqli,因为我从未使用过它)。如果OP使用PDO,它会为您提供卫生服务(不确定mysqli,因为我从未使用过它)。