public function testMultipleDeleteInJsonFormat() { // SQL Delete in JSON format $json = ' delete: [ { table: "books" , where: { id: 200 } } , { table: "users" , where: { id: 1 } } , { table: "users" , where: { id: 300 } } ] '; // Executes the SQL and stores the result $result = test::execute($json); $this->assertEquals(1, self::getNumRowsAffected($result), 'Test with Delete command'); }
static function delete($arr, $multi = '') { if ($arr) { if ($multi) { foreach ($arr as $i => $a) { $delete[] = array("table" => self::$table, "where" => array("id" => $i)); } } else { $delete[] = array("table" => self::$table, "where" => array("id" => $arr)); } $json_delete = json_encode($delete); return parent::execute('delete:' . $json_delete); } }
/** * Main method * * */ public function __construct() { // Validates the form input submitted, before writing to database if ($_POST) { $firstName = filter_input(INPUT_POST, 'firstname', FILTER_SANITIZE_STRING); $lastName = filter_input(INPUT_POST, 'lastname', FILTER_SANITIZE_STRING); $mail = filter_input(INPUT_POST, 'mail', FILTER_VALIDATE_EMAIL); // Performing validation if (empty($firstName) || empty($lastName)) { $error = 'Enter your name and last name'; } else { if (isset($_POST['mail']) && !empty($_POST['mail']) && $mail == FALSE) { $error = 'Enter a valid email address'; } } // Displays a message in case of errors if (isset($error)) { self::$message = '<i style="color: #f50;">ERROR: ' . $error . '</i><br /><br />'; } else { // SQL insertion in JSON format $json = ' insert : [ { table: "users" , values: { firstname: "' . $firstName . '", lastname: "' . $lastName . '", mail: "' . $mail . '" } } ] '; // Performs the new record and store the result list($total) = PDO4You::execute($json); // Displays a success message self::$message = 'Register #' . $total . ' added successfully!!<br /><br />'; } } // Capture records of all registered users self::$hasRecords = PDO4You::select('SELECT * FROM users ORDER BY id DESC'); }
/** * Multiple Delete * * */ public function multipleDelete() { // SQL Delete in JSON format $json = ' delete: [ { table: "users" , where: { id: 4 } },{ table: "users" , where: { id: 20 } },{ table: "users" , where: { id: 30 } },{ table: "books" , where: { id: 10 } } ] '; // Executes the SQL and stores the result $result = PDO4You::execute($json); echo '<div class="code title">Demo with Delete command</div>'; echo '<div class="code debug">PDO4You::execute(' . $this->getQuery($json) . '); ' . $this->getResult($result) . '</div>'; }