Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //$commands = $_GET['commands'];
- $commands = ["UPDAtE users SET (age = 30) WHERE (user_id = 1)", "INSERT INTO users (login, age, gender) VALUES (yana, 20, female)", "UPDATE users SET (age = 30) WHERE (user_id = 1)", "INSERT INTO users (login, age) VALUES (yana, 20, female)", "INSERT INTO users (login, age, gender) VALUES (yana, 20, female)", "INSERT INTO users (user_id, login, gender) VALUES (12, yana, female)", "INSERT INTO users (login, age, gender) VALUES (yana, 20, female)", "INSrRT INTO users (login, age, gender) VALUES (yana, 20, female)", "UPDATE users SET (age = 30) WHERE (user_id = 1)", "UPDATE users SET (age = 30) WHERE (age = undefined)", "DELETE FROM users WHERE (age = 30)"];
- $insertRegex = '/INSERT INTO (\w+) \((.+)\) VALUES \((.+)\)/';
- $updateRegex = '/UPDATE (\w+) SET \((\w+) = (\w+)\) WHERE \((\w+) = (\w+)\)/';
- $deleteRegex = '/DELETE FROM (\w+) WHERE \((\w+) = (\w+)\)/';
- $errors = 0;
- $lastID = -1;
- foreach ($commands as $command) {
- if (preg_match($insertRegex, $command, $insertMatch)) {
- $fields = explode(', ', $insertMatch[2]);
- $values = explode(', ', $insertMatch[3]);
- if (array_search('login', $fields) === false) {
- $errors++;
- continue;
- } else if (count($fields) != count($values)) {
- $errors++;
- continue;
- }
- if (array_search('user_id', $fields) !== false) {
- $id = array_search('user_id', $fields);
- $lastID = $values[$id];
- } else {
- $lastID += 1;
- }
- $login = $values[array_search('login', $fields)];
- if (array_search('gender', $fields) !== false) {
- $gender = $values[array_search('gender', $fields)];
- } else {
- $gender = 'undefined';
- }
- if (array_search('age', $fields) !== false) {
- $age = $values[array_search('age', $fields)];
- } else {
- $age = 'undefined';
- }
- $table[$lastID]['login'] = $login;
- $table[$lastID]['gender'] = $gender;
- $table[$lastID]['age'] = $age;
- } else if (preg_match($updateRegex, $command, $updateMatch)) {
- // $updateField = $updateMatch[2];
- // $existingField = false;
- // if ($updateMatch[4] == 'user_id') {
- // if (isset($table[$updateMatch[5]])) {
- // $existingField = true;
- // }
- // } else {
- // foreach ($table as $key => $value) {
- // foreach ($table[$key] as $field => $fieldValue) {
- // if ($field == $updateMatch[4]) {
- // $existingField = true;
- // }
- // }
- // }
- // if ($existingField) {
- // if ($updateMatch[4] == 'user_id') {
- // $table[$updateMatch[5]][$updateMatch[2]] = $updateMatch[3];
- // } else {
- //
- // }
- // } else {
- // $errors++;
- // continue;
- // }
- //
- // }
- } else if (preg_match($deleteRegex, $command, $deleteMatch)) {
- } else {
- $errors++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement