Is there an alternative code to this?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Bbob
Forum Commoner
Posts: 57
Joined: Sat Aug 07, 2010 4:46 am

Is there an alternative code to this?

Post by Bbob »

Hi

I have a form with 6 textfields that will be used to edit data in the database.

When a value is entered in one of the text field I want that value to be only updated in the database.

Ex.

[]id
[]name
[]age
[]address
[]number
[]gender

if name was edited, only the name field would be updated in the database.


As of now heres the code of what Im currently thinking

Code: Select all

if($name)
{
   UPDATE customer
   SET name=value
   WHERE id=id
}

elseif ($age)
{
   UPDATE age
   SET age = age
   WHERE id = id
}
....and so on


Is there an easier way to do this? Or am I stuck with these codes?
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Is there an alternative code to this?

Post by oscardog »

I tried this out (minus the query part) and it works. Might need to fiddle with the query slightly as I just used echos for testing.

Code: Select all

foreach( $_POST as $key => $value){
   if($value != "" && $key != "submit") { //Checks the field is not empty and also ignores the submit button in the array
      mysql_query("UPDATE customer SET $key = '".$value."' WHERE id = id");
   }
}
Bbob
Forum Commoner
Posts: 57
Joined: Sat Aug 07, 2010 4:46 am

Re: Is there an alternative code to this?

Post by Bbob »

Hi thanks for the reply

What does "=>" and $key mean?
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Is there an alternative code to this?

Post by oscardog »

Bbob wrote:Hi thanks for the reply

What does "=>" and $key mean?
It makes an associative array. Essentially assigns each value in the array with a key. So basically it just uses the field names as the $key in the array and the value in the fields as the $value.

You can think of it as a person with characteristics.

Ashley is 18 years old
Harry is 21 years old
Nick is 70 years old

If you were to display that as an associative array it would look like:

Code: Select all

$characteristics['Ashley'] = "18";
$characteristics['Harry'] = "21";
$characteristics['Nick'] = "70"; 
So basically instead of accessing an array value using [0][1] etc you use a name($key) and each one has a value($value).

Oh and the => is just a way of assigning the value to the key.

Hope you understand it better now :)
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: Is there an alternative code to this?

Post by greyhoundcode »

Close, but will create an extra comma and invalidate the query. A quick fix:

Code: Select all

// We need the ID to be set as a minimum
if (!isset($_POST['id']))
    die('No ID value set');
else 
    // Do not forget to sanitize!!!
    $id = (int)$_POST['id'];

// What fields are we expecting?
$acceptableKeys = array(
    'name',
    'age',
    'address',
    'number',
    'gender',
    );

// Create a new array to hold our column/values
$columnData = array();

// Iterate through the post data
foreach ($_POST as $key => $value)
{
    // Are we expecting this key?
    if (in_array($key, $acceptableKeys)) {
        // Do not forget to sanitize!!
        $columnData[] = "`$column`='". mysql_real_escape_string($value) ."'"; 
    }
}

$query = "UPDATE `customer` SET ". implode(', ', $columnData) ." WHERE `id`='$id'";
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Is there an alternative code to this?

Post by John Cartwright »

My apologies, Greyhound. I accidentally edited your post instead of creating a reply :oops:
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: Is there an alternative code to this?

Post by greyhoundcode »

No problem, much neater!
Post Reply