Page 1 of 1
Is there an alternative code to this?
Posted: Sat Aug 14, 2010 7:33 am
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?
Re: Is there an alternative code to this?
Posted: Sat Aug 14, 2010 9:09 am
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");
}
}
Re: Is there an alternative code to this?
Posted: Sat Aug 14, 2010 10:26 am
by Bbob
Hi thanks for the reply
What does "=>" and $key mean?
Re: Is there an alternative code to this?
Posted: Sat Aug 14, 2010 10:55 am
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

Re: Is there an alternative code to this?
Posted: Sat Aug 14, 2010 12:32 pm
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'";
Re: Is there an alternative code to this?
Posted: Sat Aug 14, 2010 12:42 pm
by John Cartwright
My apologies, Greyhound. I accidentally edited your post instead of creating a reply

Re: Is there an alternative code to this?
Posted: Mon Aug 16, 2010 1:25 am
by greyhoundcode
No problem, much neater!