function save(Entity $entity) { $vars = get_object_vars($entity); //print_r($vars); if ($vars['id'] == 0) { $vars['id'] == 'NULL'; } $cols = array(); $values = array(); $updates = array(); foreach ($vars as $key => $value) { $cols[] = "`{$key}`"; $values[] = ' :' . $key . ' '; $updates[] = " `{$key}` = VALUES(`{$key}`) "; } $col = implode(",", $cols); $value = implode(",", $values); $update = implode(",", $updates); $sql = "INSERT INTO {$this->tableName()} ({$col}) VALUES ({$value}) ON DUPLICATE KEY UPDATE {$update}"; $res = $this->db->query($sql, $vars); if ($res === FALSE) { return FALSE; } if ($entity->id == 0) { $entity->id = $this->db->lastInsertId(); } return $entity; }
public function insert_comment($sid, $msg, $parent, $author_name, $author_email) { // Connect to database try { $handler = new Database(); // Insert comment to database if ($parent !== 'NULL') { $handler->beginTransaction(); // If comment has a parent begin transaction } $res = $handler->prepare('INSERT INTO `comment`(`sid`, `author_name`, `author_email`, `message`, `parent`) VALUES (:sid, :author_name, :author_email, :message, :parent)'); $res->execute(array(':sid' => $sid, ':author_name' => $author_name, ':author_email' => $author_email, ':message' => $msg, ':parent' => $parent)); if ($res->rowCount() !== 1) { return false; } // Get cid of last comment $cid = $handler->lastInsertId(); if ($parent !== 'NULL') { $res = $handler->prepare('UPDATE `comment` SET `children` = 1 WHERE `cid` = :parent'); $res->execute(array(':parent' => $parent)); $handler->commit(); // Commit only if both queries succeed } } catch (PDOException $e) { if ($parent !== 'NULL') { $handler->rollback(); } return false; } return $cid; }
function sendMessage($id_author, $to_users, $subject, $body, $time, $thread_id = false) { if (!is_array($to_users)) throw new Exception('$to_users must be an array'); Database::query('START TRANSACTION'); $query = 'INSERT INTO `users_messages` SET `id_author`=' . $id_author . ', `time`=' . $time . ', `subject`=' . Database::escape($subject) . ', `html`=' . Database::escape($body); Database::query($query); // если есть тред - пишем в тот же тред $lastId = Database::lastInsertId(); $thread_id = $thread_id ? $thread_id : $lastId; if ($thread_id) { $q = array(); foreach ($to_users as $receiver_id) { $is_new = ($receiver_id == $id_author) ? 0 : 1; $q[] = '(' . $lastId . ',' . $thread_id . ',' . $receiver_id . ',' . $is_new . ',0)'; } if (count($q)) { $query = 'INSERT INTO `users_messages_index`(message_id,thread_id,id_recipient,is_new,is_deleted) VALUES ' . implode(',', $q); Database::query($query); } } Database::query('COMMIT'); }
function edit_event() { $id = $_POST['id'] ? $_POST['id'] : 'NULL'; $_POST['template_id'] = max(1, (int) $_POST['template_id']); Database::query('INSERT INTO `lib_events` SET `id` = ' . $id . ', `title`=' . Database::escape($_POST['title']) . ', `male`=' . Database::escape($_POST['male']) . ', `age_start_days`=' . Database::escape($_POST['age_start_days']) . ', `age_end_days`=' . Database::escape($_POST['age_end_days']) . ', `description`=' . Database::escape($_POST['description']) . ', `template_id`=' . Database::escape($_POST['template_id']) . ' ON DUPLICATE KEY UPDATE `title`=' . Database::escape($_POST['title']) . ', `male`=' . Database::escape($_POST['male']) . ', `age_start_days`=' . Database::escape($_POST['age_start_days']) . ', `age_end_days`=' . Database::escape($_POST['age_end_days']) . ', `description`=' . Database::escape($_POST['description']) . ', `template_id`=' . Database::escape($_POST['template_id']) . ' '); $id = $id == 'NULL' ? Database::lastInsertId() : $id; header('Location: /admin/event/' . $id . '/edit'); }
public function CreateBankProcess($param) { parent::query('INSERT INTO dd_bank(bk_code,bk_account_branch,bk_account_name,bk_account_number) VALUE(:code,:branch,:name,:number)'); parent::bind(':code', $param['code']); parent::bind(':branch', $param['branch']); parent::bind(':name', $param['name']); parent::bind(':number', $param['number']); parent::execute(); return parent::lastInsertId(); }
public function CreateMatchingProcess($param) { parent::query('INSERT INTO dy_place_to_tag(pt_place_id,pt_tag_id,pt_create_time,pt_update_time) VALUE(:place_id,:tag_id,:create_time,:update_time)'); parent::bind(':place_id', $param['place_id']); parent::bind(':tag_id', $param['tag_id']); parent::bind(':create_time', date('Y-m-d H:i:s')); parent::bind(':update_time', date('Y-m-d H:i:s')); parent::execute(); return parent::lastInsertId(); }
public function persist() { $db = new Database(); if ($this->id == null) { $db->execute('INSERT INTO followers (user, follower) VALUES (?, ?)', array($this->user, $this->follower)); $this->id = $db->lastInsertId(); } else { $req = 'UPDATE followers SET user = ?, follower = ? WHERE id = ?'; $db->execute($req, array($this->user, $this->follower, $this->id)); } }
public function persist() { $db = new Database(); if ($this->id == null) { $req = 'INSERT INTO stream_email (server, account, password, port, firstUpdate) VALUES (?, ?, ?, ?, ?)'; $db->execute($req, array($this->server, $this->account, $this->password, $this->port, $this->firstUpdate)); $this->id = $db->lastInsertId(); } else { $req = 'UPDATE stream_email SET server = ?, account = ?, password = ?, port = ?, firstUpdate = ? WHERE id = ?'; $db->execute($req, array($this->server, $this->account, $this->password, $this->port, $this->firstUpdate, $this->id)); } }
public function persist() { $db = new Database(); if ($this->id == null) { $req = 'INSERT INTO stream_category (stream, category, streamType) VALUES (?, ?, ?)'; $db->execute($req, array($this->stream, $this->category, $this->streamType)); $this->id = $db->lastInsertId(); } else { $req = 'UPDATE stream_category SET stream = ?, category = ?, streamType = ? WHERE id = ?'; $db->execute($req, array($this->stream, $this->category, $this->streamType, $this->id)); } }
function _new() { $title = trim(Request::post('title')); if (!$title) { throw new Exception('title missed'); } $query = 'INSERT INTO `rightholders` SET `title`=' . Database::escape($title); Database::query($query); @ob_end_clean(); header('Location: /admin/rightholders/' . Database::lastInsertId()); exit; }
public function persist() { $db = new Database(); if ($this->id == null) { $req = 'INSERT INTO article (title, content, articleDate, streamType, url, stream_id) VALUES (?, ?, ?, ?, ?, ?)'; $db->execute($req, array($this->title, $this->content, $this->articleDate, $this->streamType, $this->url, $this->stream_id)); $this->id = $db->lastInsertId(); } else { $req = 'UPDATE article SET title = ?, content = ?, articleDate = ?, streamType = ?, url = ?, stream_id = ? WHERE id = ?'; $db->execute($req, array($this->title, $this->content, $this->articleDate, $this->streamType, $this->url, $this->stream_id, $this->id)); } }
public function persist() { $db = new Database(); if ($this->id == null) { $req = 'INSERT INTO blog (account, article) VALUES (?, ?)'; $db->execute($req, array($this->account, $this->article)); $this->id = $db->lastInsertId(); } else { $req = 'UPDATE blog SET account = ?, article = ? WHERE id = ?'; $db->execute($req, array($this->account, $this->article, $this->id)); } }
public function persist() { $db = new Database(); if ($this->id == null) { $req = 'INSERT INTO accounts (authentification, username, email, userKey, active, accountLevel, picture) VALUES (?, ?, ?, ?, ?, ?, ?)'; $db->execute($req, array($this->authentification, $this->username, $this->email, $this->userKey, $this->active, $this->accountLevel, $this->picture)); $this->id = $db->lastInsertId(); } else { $req = 'UPDATE accounts SET authentification = ?, username = ?, email = ?, userKey = ?, active = ?, accountLevel = ?, picture = ? WHERE id = ?'; $db->execute($req, array($this->authentification, $this->username, $this->email, $this->userKey, $this->active, $this->accountLevel, $this->picture, $this->id)); } }
function add_album_relation_link() { $album_id = $_POST['album_id']; $role = $_POST['role']; Database::query('INSERT INTO `album_invites` SET `album_id`=' . $album_id . ', `inviter_user_id `=' . CurrentUser::$id . ', `family_role`=' . $role); $uniqid = Database::lastInsertId(); $data = array(); $data['link'] = 'http://' . Config::need('www_domain') . '/invite/' . $album_id . '/' . $role . '/' . md5($uniqid); return $data; }
/** * Create a new DVD to the database * * @return mixed False if failed, admin url otherwise */ public function create() { $this->publicUrl = strtolower(urlencode($this->title)); $this->adminHash = substr(md5($this->title . time() . rand()), 0, 8); $dbh = new Database(); $sth = $dbh->prepare("INSERT INTO " . DB_PREFIX . "dvds (title, author, email, publish_date, deadline_date, status, show_frontpage, description, public_url, admin_hash) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); if (!$sth->execute(array($this->title, $this->author, $this->email, $this->publishDate, $this->deadlineDate, $this->status, $this->showFrontpage, $this->description, $this->publicUrl, $this->adminHash))) { return false; } $this->id = (int) $dbh->lastInsertId(); $adminUrl = SITE_URL . '/' . $this->id . '/' . $this->publicUrl . '/' . $this->adminHash; return $adminUrl; }
function _update($data, $tableName) { $q = array(); $this->dropCache(); foreach ($data as $field => $value) { if (isset($this->fieldsMap[$field])) { $q[] = '`' . $field . '`=' . Database::escape($value); }else throw new Exception('_create failed: illegal field #' . $field); } if (count($q)) { Database::query('UPDATE `' . $tableName . '` SET ' . implode(',', $q) . ' WHERE `id`=' . $this->id); return $lid = Database::lastInsertId(); } }
public function setSettings($store_id, $array) { if (!is_numeric($store_id)) { Database::query("INSERT INTO PREFIX_stores SET name = :name", array('name' => crc32(microtime(true)))); $store_id = Database::lastInsertId(); return $this->setSettings($store_id, $array); } if (!is_array($array)) { return; } foreach ($array as $key => $value) { $this->setSetting($store_id, $key, $value); } }
public function CreateMoneyTransferProcess($param) { parent::query('INSERT INTO dd_money_transfer(mf_order_id,mf_to_bank,mf_member_id,mf_total,mf_description,mf_create_time,mf_update_time,mf_type) VALUE(:order_id,:to_bank,:member_id,:total,:description,:create_time,:update_time,:type)'); parent::bind(':order_id', $param['order_id']); parent::bind(':to_bank', $param['to_bank']); parent::bind(':member_id', $param['member_id']); parent::bind(':total', $param['total']); parent::bind(':description', $param['description']); parent::bind(':create_time', date('Y-m-d H:i:s')); parent::bind(':update_time', date('Y-m-d H:i:s')); parent::bind(':type', $param['type']); parent::execute(); return parent::lastInsertId(); }
public function registerUser($name, $role_id, $email, $password, $phone, $lang_id, $birthday, $company = false) { // Accepts 5 arguments in the array: name, email, password, phone, lang_id, birthday (optional) // $settings = array(string $name, string $email, string $phone, int $lang_id [, date(YYYY-MM-DD) $birthday ] ); // setting values from settings array $date_registered = date("Y-m-d"); // checking if email exists if ($this->userEmailExist($email)) { throw new Exception(Translate::string("user.email_already_exists"), 1); } else { $db = new Database(); $db->beginTransaction(); // setting the user properties and validating $this->setName($name); $this->setRole($role_id); $this->setEmail($email); $this->setPassword($password); $this->setPhone($phone); $this->setDateRegistred($date_registered); $this->setLangID($lang_id); $this->setBirthday($birthday); $db->query('INSERT INTO users ( name, role_id, email, password, phone, date_registered, lang_id, birthday) VALUES (:name,:role_id,:email,:password,:phone,:date_registered,:lang_id,:birthday) '); $db->bind(':name', $this->name()); $db->bind(':role_id', $this->role()); $db->bind(':email', $this->email()); $db->bind(':password', $this->password()); $db->bind(':phone', $this->phone()); $db->bind(':date_registered', $this->dateRegistred()); $db->bind(':lang_id', $this->langID()); $db->bind(':birthday', $this->birthday()); $db->execute(); $newUserID = $db->lastInsertId(); if ($company) { $db->query('INSERT INTO companies ( user_id, company_name, company_number, company_address, company_zip, phone_2) VALUES (:user_id, :company_name,:company_number,:company_address,:company_zip,:phone_2) '); $db->bind(':user_id', $newUserID); $db->bind(':company_name', $this->company_name); $db->bind(':company_number', $this->company_number); $db->bind(':company_address', $this->company_address); $db->bind(':company_zip', $this->company_zip); $db->bind(':phone_2', $this->phone_2); $db->execute(); } $db->endTransaction(); $this->setID($newUserID); $this->checkCredentials($this->email(), $password, 1, $_SERVER["HTTP_USER_AGENT"], $_SERVER["REMOTE_ADDR"], session_id()); } }
public static function addCity($id_country, $name) { if (!$id_country) { return false; } $query = 'INSERT INTO `lib_city` SET `verified`=0, `name`=' . Database::escape($name) . ',`country_id`=' . (int) $id_country . ' ON DUPLICATE KEY UPDATE `country_id`=' . (int) $id_country; Database::query($query); $id = Database::lastInsertId(); if (!$id) { $id = Database::sql2single('SELECT id FROM `lib_city` WHERE `country_id`=' . (int) $id_country . ' AND `name`=' . Database::escape($name)); } return $id; }
public function CreateCommentProcess($param) { parent::query('INSERT INTO dy_comment(cm_parent_id,cm_token,cm_member_id,cm_post_id,cm_message,cm_create_time,cm_update_time,cm_ip,cm_type) VALUE(:parent_id,:token,:member_id,:post_id,:message,:create_time,:update_time,:ip,:type)'); parent::bind(':parent_id', $param['parent_id']); parent::bind(':token', $param['token']); parent::bind(':member_id', $param['member_id']); parent::bind(':post_id', $param['post_id']); parent::bind(':message', $param['message']); parent::bind(':create_time', date('Y-m-d H:i:s')); parent::bind(':update_time', date('Y-m-d H:i:s')); parent::bind(':ip', parent::GetIpAddress()); parent::bind(':type', $param['type']); parent::execute(); return parent::lastInsertId(); }
public function CreateImageProcess($param) { parent::query('INSERT INTO dd_image(im_product_id,im_member_id,im_caption,im_filename,im_format,im_create_time,im_update_time,im_type,im_status) VALUE(:product_id,:member_id,:caption,:filename,:format,:create_time,:update_time,:type,:status)'); parent::bind(':product_id', $param['product_id']); parent::bind(':member_id', $param['member_id']); parent::bind(':caption', $param['caption']); parent::bind(':filename', $param['filename']); parent::bind(':format', $param['format']); parent::bind(':create_time', date('Y-m-d H:i:s')); parent::bind(':update_time', date('Y-m-d H:i:s')); parent::bind(':type', $param['type']); parent::bind(':status', $param['status']); parent::execute(); return parent::lastInsertId(); }
/** * @covers Database::insert * @covers Database::selectOne */ public function testInsert() { $this->assertEquals(1, $this->db->insert('test', array('name' => 'Other test'))); $this->assertEquals(4, $this->db->lastInsertId('test', 'id')); $r1 = $this->db->selectOne('*', 'test', array('id' => 4)); $this->assertEquals('Other test', $r1['name']); $this->assertEquals(array('id', 'name'), array_keys($r1)); //Errors test $this->assertFalse(LoggerApp::hasError(), LoggerApp::getLastError()); $this->assertEquals(0, $this->db->insert('test', array('name' => 'Other test', 'id' => 1))); $this->assertTrue(LoggerApp::hasError(), 'Não gerou o erro esperado'); LoggerApp::clear(); $this->assertEquals(0, $this->db->insert('testinvalid', array('name' => 'Other test', 'xyz' => 1))); $this->assertTrue(LoggerApp::hasError(), 'Não gerou o erro esperado'); }
function write() { $id = Request::post('entry_id'); $title = Request::post('title'); $body = Request::post('body'); $id_parent = Request::post('answer_to'); global $current_user; $query = 'SELECT * FROM `blog_entries` WHERE `id`=' . $id; $data = Database::sql2row($query); $entry = new Entrie($data); if (!$current_user->authorized) { throw new Exception('must be autorized'); } if (!$body) { throw new Exception('body missed'); } if (!$title) { throw new Exception('title missed'); } if ($id_parent) { // answer $query = 'SELECT * FROM `blog_entries_comments` WHERE `id`=' . $id_parent; $parent_comment = Database::sql2row($query); if ($parent_comment['id_parent'] > 0) { $answer_to = $id_parent; $id_parent = $parent_comment['id_parent']; } else { $answer_to = $id_parent; $id_parent = $parent_comment['id']; } } else { $answer_to = 0; $id_parent = 0; } $query = 'INSERT INTO `blog_entries_comments` SET `id_entry`=' . $id . ', `id_user`=' . $current_user->id . ', `id_parent`=' . $id_parent . ', `time`=' . time() . ', `title`=' . Database::escape($title) . ', `comment`=' . Database::escape($body) . ', `answer_to`=' . $answer_to; Database::query($query); $comment_id = Database::lastInsertId(); $entry->updateCommentsCount(); header('Location: ' . '/blog/' . $entry->user->getNickName() . '/' . $entry->id . '#comment-' . $comment_id); exit(0); }
function register($nickname, $email, $password) { $hash = md5($email . $nickname . $password . time()); $query = 'INSERT INTO `users` SET `email`=\'' . $email . '\', `password`=\'' . md5($password) . '\', `nickname`=\'' . $nickname . '\', `hash` = \'' . $hash . '\''; if (Database::query($query)) { $this->id = Database::lastInsertId(); if ($this->id) { return $hash; } } return false; }
public function persist() { $db = new Database(); $firstUpdate = date(Database::DATE_FORMAT, strtotime($this->firstUpdate)); $lastUpdate = date(Database::DATE_FORMAT, strtotime($this->lastUpdate)); if ($this->id == null) { $req = 'INSERT INTO stream_twitter (channel, firstUpdate, lastUpdate) VALUES (?, ?, ?)'; $data = array($this->channel, $firstUpdate, $lastUpdate); $db->execute($req, $data); $this->id = $db->lastInsertId(); } else { $req = 'UPDATE stream_twitter SET channel = ?, firstUpdate = ?, lastUpdate = ? WHERE id = ?'; $data = array($this->channel, $firstUpdate, $lastUpdate, $this->id); $db->execute($req, $data); } }
public function CreateProcess($param) { parent::query('INSERT INTO base_image(im_page_id,im_people_id,im_caption,im_filename,im_format,im_create_time,im_update_time,im_ip,im_type,im_status) VALUE(:page_id,:people_id,:caption,:filename,:format,:create_time,:update_time,:ip,:type,:status)'); parent::bind(':page_id', $param['page_id']); parent::bind(':people_id', $param['people_id']); parent::bind(':caption', $param['caption']); parent::bind(':filename', $param['filename']); parent::bind(':format', $param['format']); parent::bind(':create_time', date('Y-m-d H:i:s')); parent::bind(':update_time', date('Y-m-d H:i:s')); parent::bind(':ip', parent::GetIpAddress()); parent::bind(':type', $param['type']); parent::bind(':status', $param['status']); parent::execute(); return parent::lastInsertId(); }
public function CreateTokenProcess($param) { parent::query('INSERT INTO base_token(tk_people_id,tk_token,tk_device,tk_model,tk_os,tk_browser,tk_user_agent,tk_ip,tk_create_time,tk_update_time,tk_expired) VALUE(:people_id,:token,:device,:model,:os,:browser,:user_agent,:ip,:create_time,:update_time,:expired)'); parent::bind(':people_id', $param['facebook_id']); parent::bind(':token', $param['new_token']); parent::bind(':device', $param['device']); parent::bind(':model', $param['model']); parent::bind(':os', $param['os']); parent::bind(':browser', $param['browser']); parent::bind(':user_agent', $param['user_agent']); parent::bind(':ip', parent::GetIpAddress()); parent::bind(':create_time', date('Y-m-d H:i:s')); parent::bind(':update_time', date('Y-m-d H:i:s')); parent::bind(':expired', $param['expired']); parent::execute(); return parent::lastInsertId(); }
function _new() { global $current_user; Request::$post['lang_code'] = Config::$langs[Request::$post['lang_code']]; $fields = array('title' => 'title', 'isbn' => 'ISBN', 'lang_code' => 'id_lang', 'annotation' => 'annotation'); $to_update = array(); foreach ($fields as $field => $magazinefield) { if (!isset(Request::$post[$field])) { throw new Exception('field missed #' . $field); } $to_update[$magazinefield] = Request::$post[$field]; } $q = array(); foreach ($to_update as $field => &$value) { if (in_array($field, array('ISBN', 'year'))) { $value = is_numeric($value) ? $value : 0; } $q[] = '`' . $field . '`=' . Database::escape($value) . ''; } if (count($q)) { $query = 'INSERT INTO `magazines` SET ' . implode(',', $q); Database::query($query); $lid = Database::lastInsertId(); MagazineLog::addLog($to_update, array(), $lid); MagazineLog::saveLog($lid, BookLog::TargetType_magazine, $current_user->id, BiberLog::BiberLogType_magazineNew); $search = Search::getInstance(); /* @var $search Search */ $search->setMagazineToFullUpdate($lid); } if (isset($_FILES['cover']) && $_FILES['cover']['tmp_name']) { $folder = Config::need('static_path') . '/upload/mcovers/' . ceil($lid / 5000); @mkdir($folder); chmod($folder, 755); $filename = $folder . '/' . $lid . '.jpg'; $upload = new UploadAvatar($_FILES['cover']['tmp_name'], 100, 100, "simple", $filename); if ($upload->out) { $to_update['is_cover'] = 1; } else { throw new Exception('cant copy file to ' . $filename, 100); } } ob_end_clean(); header('Location:' . Config::need('www_path') . '/m/' . $lid); exit; }
public function CreatePlaceProcess($param) { parent::query('INSERT INTO dy_place(pl_token,pl_member_id,pl_name,pl_description,pl_province_id,pl_city_id,pl_district_id,pl_register_time,pl_update_time,pl_visit_time,pl_ip) VALUE(:token_id,:member_id,:name,:description,:province_id,:city_id,:district_id,:register_time,:update_time,:visit_time,:ip)'); parent::bind(':token_id', $param['token_id']); parent::bind(':member_id', $param['member_id']); parent::bind(':name', $param['name']); parent::bind(':description', $param['description']); parent::bind(':province_id', $param['province_id']); parent::bind(':city_id', $param['city_id']); parent::bind(':district_id', $param['district_id']); parent::bind(':register_time', date('Y-m-d H:i:s')); parent::bind(':update_time', date('Y-m-d H:i:s')); parent::bind(':visit_time', date('Y-m-d H:i:s')); parent::bind(':ip', parent::GetIpAddress()); parent::execute(); return parent::lastInsertId(); }