This is what I need;
- function to create $sql var with an SQL Query that updates a record in my table.
I dont know what fields are going to be changed so I have to dynamically create my Query. This is what I've done;
function editPC ($Type, $PC_model, $SerieNummer, $DHVNummer,
$AankoopDatum, $PC_ID) {
if (!$PC_ID) {
return FALSE;
}
$sql = "UPDATE pc SET "
if ($type) {
$sql .= "Type = '$type' ";
}
if ($PC_model) {
$sql .= "PC_Model = '$PC_Model' ";
}
if ($SerieNummer) {
$sql .= "SerieNummer = '$SerieNummer' ";
}
if ($DHVNummer) {
$sql .= "DHVNummer = '$DHVNummer' ";
}
if ($AankoopDatum) {
$sql .= "AankoopDatum = '$AankoopDatum' ";
}
$sql .= "WHERE PC_ID = '$PC_ID'";
$this->runQuery($sql);
}
I don't really like this code but it does work. I'm wondering if I can write a method that takes care of generating the mysql query that i can use for all editing functions. For example;
class edit {
function MagicQuery ($vars) {
//magic code
}
function editPC ($var1, $var2) {
$this->MagicQuery($var1, $var2);
}
}
Any tips or trick are very welcome. If more information is required I'll be happy to elaborate.