1

Ok..I know how to get a data record from a MySql table...and I want to change data in that record and update the table.

My question is...can you actually manipulate that data from the result row, and subsequently use those in the update statement?

For example.

Let's say the table rows have 2 fields: Name, YearlyEarn.

And once a month I want to add that month's income to the YearlyEarn field for each person.

Assume we already did the Select statement for someone who's name is in $CurrentName. And we then get their record.

$DataRow = mysql_fetch_array($result):

Can you do this:

$DataRow["YearlyEarn"] = $DataRow["YearlyEarn"] + $MonthEarn;

$query = "UPDATE EarnTable SET YearlyEarn = '$DataRow["YearlyEarn"]'    
`WHERE Name = '$CurrentName'" ;

$UpdResult = mysql_query($query) or die(mysql_error());

OR.....should I put the data into intermediate fields, manipulate it..and then use those fields in the update statement?

3 Answers 3

1

You should use prepared statements, like PDO. The mysql_* is outdated. But if not doing so, you should consider changing your query from:

$query = "UPDATE EarnTable SET YearlyEarn = '$DataRow["YearlyEarn"]'`WHERE Name = '$CurrentName'" ;

to:

$query = "UPDATE EarnTable SET YearlyEarn = `" . $DataRow['YearlyEarn'] . "` WHERE Name = `$CurrentName`" ;
Sign up to request clarification or add additional context in comments.

6 Comments

Ahhh...use the double quotes so the variable is evaluated?
I'm thinking that I'll use intermediate variables....I think the parser will get confused with using array elements from a previous query....at any rate..MySql is obtuse to use....I'm actually converting a Pascal program to PHP....and the Pascal program uses a regular Database which lets you manipulate the record data itself and then post the record.........z
Also.....while PDO looks interesting....I'm not prepared for a steep learningn curve using something like that.......does that help save overhead processing?...And if so how much?....z
You could also use mysqli, which is more like mysql, but supports prepared statements. There's a little less overhead when you use prepared statements, but that's not the reason to use them, safety is.
It has a great portability. PDO provides single interface across multiple database types, like mysql, MS sql etc, in an object oriented way. You can easily use prepared statements, which help protecting you from MySQL injections. It also has excellent error trapping methods.
|
1

Yes, you can:

UPDATE EarnTable
SET    YearlyEarn = YearlyEarn + 123
WHERE  Name = 'abc'

3 Comments

Unfortunately I am doing much more complex data manipulation..I just provided a simple example to show what I wanted to do....If all I wanted to do is add a number to it, of course I would have done that.....z
@Zardiw: Then it's not at all clear (to me) from your question exactly what manipulation you are seeking to perform. Can you clarify?
Like I said...it's complex....issue is moot now anyway...using the KISS method now......z
0

You can use:

$query = "UPDATE EarnTable SET YearlyEarn = '$DataRow[YearlyEarn]' WHERE Name = '$CurrentName'" ;

When you're interpolating an array reference, the key is automatically quoted.

or:

$query = "UPDATE EarnTable SET YearlyEarn = '{$DataRow["YearlyEarn"]}' WHERE Name = '$CurrentName'" ;

Inside {...}, you can put any variable expression and it will be evaluated and interpolated.

4 Comments

Yes but can you actually add something to the $DataRow['YearlyEarn'] before you use it to update the row?
Not sure I understand the question. It's just a variable being interpolated into the string. Whatever its value is, it will be inserted.
The question was if you could treat that variable like any other variable....Could you add a value to it?....and then use it in an update statement........z
It is just like any other variable. What makes you think it's special?

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.