ArloGrant
ArloGrant

Reputation: 249

Updating certain columns in a mySql table?

Hello I am new into PHP and Mysql - well I am developing an android app that will retrieve some user data from a server mysql db table - data like username, firstname, last name, addresse etc..and the user has the option to edit that all or some of them...and lastly click the update button to update the data back to the mysql database.

So the problem is - should I return all the values (edited and those that are not) with the POST['username'], POST['fname'], .... etc - and then update the database with data that are changed and not changed - or should I RETURN ONLY THE EDITED DATA and then somehow update these fields in the table (BUT i am not sure how to implement this - to make a SWITCH statement with cases for every type of POST['fname'] and there update every single column with the POST value)

OR DO YOU KNOW SOME BETTER LOGIC/PATTERN FOR THIS - paste some links with examples/tuts if you know?

THANKS

Upvotes: 1

Views: 151

Answers (2)

Lawson
Lawson

Reputation: 634

This works in PHP, although prepared statements are better:

$sql = "
UPDATE userData
    SET
";

if(isset($_POST['firstname'])
    $sql .= "`firstname` = '" . $_POST['firstname'] . "'";
if(isset($_POST['lastname'])
    $sql .= "`lastname` = '" . $_POST['lastname'] . "'";
// You get the picture

$sql .= "
WHERE userid = $id"; 

Upvotes: 1

Hoan Nguyen
Hoan Nguyen

Reputation: 18151

You should send a flag indicate the edited fields and depending on the flag write your update statement, for example if username and fname are changed, send a flag USERNAME_FNAME_CHANGED and then expected 2 fields username and fname being sent from your device. Then in the server implements a switch statement to write the update statement accordingly.

Upvotes: 0

Related Questions