1

I am trying to insert a new password into the password column of my users table. I am passing two variables to the SQL query, the new password and the user ID, but I cannot get it to work no matter what I do. Here is the SQL query:

$sql = "INSERT INTO cryptuser  WHERE userID ='" . $userid ."' (passwordnew)
                    VALUES ('$passwordnew')  "; 

Here is my form:

<fieldset>

<legend>Change Password</legend>

<form action="changepass.php" method="POST">
        <label>Current Password :</label>
        <input type="password" name="password" /><br />
        <label>New Password :</label>
        <input type="password" name="passwordnew" /><br/>
        <label>Repeat New Password :</label>
        <input type="password" name="passwordnewRepeat" /><br/>
        <input class="signbutt" type="submit" value="Save"/><br />

</form>

Once I can change the password stored in the table, then I will create further SQL that will only change the password to the new value if the current password entered matches the existing password, then JavaScript to make sure the re-entered password matches the new password.

1
  • 1
    You need to use UPDATE. Please post the DB schema of that table. Commented Feb 23, 2012 at 15:40

4 Answers 4

2

either you insert

$sql = "INSERT INTO cryptuser ( userID, password) VALUES ( 5, 's3cret')  "; 

or you update

$sql = "UPDATE cryptuser  SET password='s3cr3t' where userID =5"; 

AND as always: WORD OF WARNING! do not use the posted strings and insert them directly into the database, because you open yourself up to sql injection attacks. you need to sanitize the strings. look up the terms for further instruction.

edit because of comment:

$sql = sprintf("UPDATE cryptuser  SET password='%s' where userID =%d" , $newpassword, $userId); 
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, I am taking the users ID from a session variable, it will be different for every user, so I cannot add a user ID manually to the query.
@deucalion0 you are missing the point.. you can use the variable.. but within an UPDATE statement
You are right, I have been trying to do something completely wrong using INSERT instead of UPDATE. It now works, thanks you.
1

I think you should take a look at the sql manual.

First of all it sounds like you want to update a value but you are using INSERT. Look for the UPDATE statement

The second problem: INSERT with WHERE?

Comments

1

Try changing your SQL to this:

$sql = "INSERT INTO cryptuser(userID, passwordnew) 
        VALUES('$userid', '$passwordnew')";

Although, if this is an 'Update Password' form, it makes more sense to use a SQL UPDATE statement because the record in your table for the current user probably already exists...

$sql = "UPDATE cryptuser SET passwordnew = '$passwordnew' 
        WHERE userID = '$userid'";

I am assuming that you are already sanitizing the variables holding your values to prevent SQL Injection attacks. You should also look into using Prepared Statements

Comments

0

this is wrong SQL problem, not inserting variable problem
the right order of the UPDATE query would be

UPDATE cryptuser SET passwordnew='passwordnew'  WHERE userID=1; 

1 Comment

I implemented your suggestion and it worked perfectly. Thank you for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.