drago
drago

Reputation: 1303

Deleting password from mysql table?

I need to delete the password from the password field in the members table in the mysql.

I have these codes which will work if if I change the $password and password to $id and id but then it will remove the entire user account. I just need to remove/delete the users password.

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();
?>

and this is my 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();
?>

How can i just delete/remove the password field in the members table ?

Upvotes: 0

Views: 899

Answers (3)

Colin M
Colin M

Reputation: 13348

RDBMS' don't work on columns, they work on rows. INSERT, SELECT, DELETE and UPDATE are all intended to work in the context of a row, not of a column.

What this means is that, if you want to "remove" a password from a user, what you really want to do is UPDATE the password to be NULL for that user (WHERE user_id = 12345).

If you issue a DELETE, regardless of your WHERE criteria, you are telling the database to remove the row entirely. This means that the database will no longer contain that user at all.

Upvotes: 3

mallix
mallix

Reputation: 1429

You don t remove a field. You simply update it to NULL or empty:

$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);

Upvotes: 0

What you want to do is UPDATE the record, not DELETE it,

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

Upvotes: 0

Related Questions