从mysql表中删除密码?

时间:2013-02-06 18:52:21

标签: php mysql

我需要从mysql的members表中的密码字段中删除密码。

我有这些代码,如果我将$ password和密码更改为$ id和id但是它会删除整个用户帐户。我只需要删除/删除用户密码。

delete.php

<?php

$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name="members"; // Table name

// Connect to server and select databse.
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'];

// Delete data in mysql from row that has this id
$sql="DELETE FROM $tbl_name WHERE password='$password'"; <<<< if i change this to id=$id the script will work but it will remove the entire account!! 
$result=mysql_query($sql);

// if successfully deleted
if($result){
echo "Deleted Successfully";
echo "<BR>";
echo "<a href='suspend.php'>Back to main page</a>";
}

else {
echo "ERROR";
}
?>

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

这是我的List.php

<?php

$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name="members"; // 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");

// select record from mysql
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
?>

<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td colspan="5" bgcolor="#FFFFFF"><strong>Delete data in mysql</strong> </td>
</tr>

<tr>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Username</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Pass</strong></td>
<td align="center" bgcolor="#FFFFFF">&nbsp;</td>
</tr>

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

<tr>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['username']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['password']; ?></td>
<td bgcolor="#FFFFFF"><a href="delete_ac.php?id=<? echo $rows['id']; ?>">Ban and Remove</a></td>
</tr>

<?php
// close while loop
}
?>

</table>

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

如何删除/删除成员表中的密码字段?

3 个答案:

答案 0 :(得分:3)

RDBMS'不适用于列,它们适用于行。 INSERT,SELECT,DELETE和UPDATE都适用于的上下文,而不是

这意味着,如果您想要“删除”用户的密码,您真正想做的是UPDATE该用户的密码为NULL {{1} }})。

如果您发出WHERE user_id = 12345,无论您的DELETE条件如何,都告诉数据库完全删除该行。这意味着数据库将不再包含该用户。

答案 1 :(得分:0)

您要做的是UPDATE记录,而不是DELETE

$sql="UPDATE $tbl_name SET password = '' WHERE id='$id'";

答案 2 :(得分:0)

你不能移除一个字段。您只需将其更新为NULL或为空:

$sql="UPDATE $table_name SET password='' where id=$id"; <<<< if i change this to id=$id the script will work but it will remove the entire account!! 
$result=mysql_query($sql);  

OR

$sql="UPDATE $table_name SET password=NULL where id=$id"; <<<< if i change this to id=$id the script will work but it will remove the entire account!! 
$result=mysql_query($sql);