<?php

// This script interprets the data from the submitted form
// Write what could you change in the code and why


$operation = $_POST['option'];
$id_record_used_later_in_class_for_getting_data_from_the_database = $_GET['id'];


$adminIdList = array(1, 2, 3);
$key = 'form_test'; //Debug

echo '<table>
<tr>
<th>UserID</th>
<th>User Name</th>
</tr>';

switch ($operation) {

case 1:

DbEngine::Instance()->Query("SELECT * FROM table_users WHERE id = $id_record_used_later_in_class_for_getting_data_from_the_database");
$arrResult = DbEngine::Instance()
->FetchAll()
->Execute();

for ($i = 1; $i < count($arrResult); $i++) {
$row = &$arrResult[$i];
if (in_array($row['id'], $adminIdList)) {

//Skip admins

}

else{
echo '<tr>
<td>' . $row['id'] . '</td>
<td>' . $row['username'] . '</td>
</tr>';
}
}

case "2":
$arrToUpdate = array();
foreach ($_POST as $val) {
if (strpos($key, 'form_') !== false) {
$fieldName = substr($key, 4);
$arrToUpdate[$fieldName] = $val;
}
}

$result = DbEngine::Instance()
->Table('tablu_users')
->Update()
->Where(array('id' => $id_record_used_later_in_class_for_getting_data_from_the_database))
->Set($arrToUpdate)
->Execute();

echo 'Update executed successfully.';

case "3":
$arrToInsert = array();
foreach ($_POST as $val) {
if (strpos($key, 'form_') !== false) {
$fieldName = substr($key, 4);
$arrToInsert[$fieldName] = $val;
}
}

echo 'Insert executed successfully.';

}


echo '</div>';
?>