0

I have a database in MySql with two columns, one where the user's name is stored and the user's corresponding value in the second column. How do I replace the values in the second column with new values? I am having trouble finding the answer in Google, because I am not so familiar with PHP. I know that both REPLACE and UPDATE functions are there, but I am having trouble getting them to work for only that specific user, not ALL the values in the column. The code I am unsuccessfully using is this:

$query1 = "UPDATE TextMeBabe SET '$username' = replace( '$username', '$existingcredits', '$newcredits')";
    echo "Values added";
    mysql_query($query1);
    mysql_close();

(I am able to log into the database with no issues, so I have not provided that code)

2 Answers 2

4
$query1 = "UPDATE TextMeBabe SET `credits`='$newcredits' WHERE (`username` ='$username')";

Assuming those are your field names.

Sign up to request clarification or add additional context in comments.

Comments

0

1) You need to use a 'where' clause to update specific rows. You can find some sample code here: http://www.w3schools.com/php/php_mysql_update.asp

The way you are doing it will update all of the records in the table.

2) The replace function does not work with replacing values in columns, It is purely a string operation for replacing parts of the string passed as first argument.

You are thinking of the repalce command(http://dev.mysql.com/doc/refman/5.0/en/replace.html) instead of the replace function(http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_replace)

In the example code from the question, it should be:

$query1 = "Update TextMeBabe Set credits = $newCredits where username = '$username'"; Assuming the column names are username and credits in the table.

Comments

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.