Jump to content

Bravat

Members
  • Posts

    69
  • Joined

  • Last visited

    Never

Everything posted by Bravat

  1. Ooops, my bad . AyKay47 it is working, but I forgot to tell you. Thank you for helping me
  2. Nodral I want to be able to update table products when new images is not selected. If I write query this way: $sql = mysql_query("UPDATE products SET product_name = '$productName', image = '$productImage', price = '$productPrice', description = '$productDetails', category_id = '$producteCagegory', subcategory_id = '$productSubcategory' WHERE id_product = '{$_POST[idProduct]}'") then table is updated and new value for image, if nothing is selected, is NULL (works fine when new image is selected). There is no error, simply nothing happens.
  3. I have this block of code: if(isset ($_POST['submit'])) { $productName = mysql_real_escape_string($_POST['productName']); $productPrice = mysql_real_escape_string($_POST['productPrice']); $producteCagegory = mysql_real_escape_string($_POST['productCategory']); $productSubcategory = mysql_real_escape_string($_POST['productSubcategory']); $productDetails = mysql_real_escape_string($_POST['productDetails']); $productImage = $_FILES['productImage']['name']; $sql = mysql_query("UPDATE products SET product_name = '$productName', "); if($productImage != ""){ $sql .= (" image = '$productImage' " ); } $sql .= ( " , price = '$productPrice', description = '$productDetails', category_id = '$producteCagegory', subcategory_id = '$productSubcategory' WHERE id_product = '$_POST[idProduct]' ") or die(mysql_error()); if(!empty($productImage)) { move_uploaded_file($_FILES["productImage"]["tmp_name"], IMG_UPLOAD . $_FILES["productImage"]["name"]); } redirect_to("inventory_edit.php?pid=$_POST[idProduct]"); } Idea is that if no new image is selected, query does not update image field. What am I doing wrong, and how to do this?
  4. Ok, I will go that way. Thanks for advice
  5. I am standing confused I don't want to have situation where I have a 100 rows for one item (id1 is related to id2(one row), id2-id3 (second row) and so on). In that case i will have one big table (for example if I have 1000 items, that are related with 100 other items that will be 100,000 rows). I want to put 100 related items in one row, and that is puzzling me.
  6. That is the way I made table. I want to put more then one item_id in single row, and latter to be able to use stored item_ids for query. For example: item_id:1 is related with item_id2, item_id3, item_id4... (I don't know how to do this). Latter I want to do something like this: SELECT * FROM item WHERE item_id = (and here I insert values for related_id)
  7. I have table related where i need to store all items that are related to the specific one. I know how to create multiple rows (for example item:id1 is related to item:id2, item:id1 is related to item:id3, item:id1 is related to item:id4 and so one ). How can store all that in one row (item:id1 is related to item:id2,item:id3....) and later be able to use this for query?
  8. Well I am creating site for internal use, so I thought that I don't need to use mysql_real_escape_string(). In order to work I need to delete htmlspecialchars()?
  9. This is the page associated with JS: <div class="articlesQuote" id="article"> <div class="heading"> <h1 id="newTitle">Get a Free Quote</h1> </div> <div class="articletxt"> <p>Please fill out the form below. When you are finished our automated system will calculate the price for your project. Take note that the price is orientative and will vary depending on active discounts and promotions!</p> <div class="quote"><form id="forma" action="actions/quote.php" method="post" name="forma"> <div class="form_para"><fieldset id="legborder"><legend id="legend">Contact Information</legend> <div id="fieldset"><label class="labela" for="ime">Your Name *: </label><input id="ime" type="text" name="ime" size="30" /><br /> <div id="forma_ime_errorloc" class="error_strings"> </div> <script type="text/javascript">// <![CDATA[ $(document).ready(function() { if (!("autofocus" in document.createElement("input"))) { $("#ime").focus(); } }); // ]]></script> <label class="labela" for="email">Your Email *: </label><input id="email" type="email" name="email" size="30" /><br /> <div id="forma_email_errorloc" class="error_strings"> </div> <label class="labela" for="kompanija">Company: </label><input id="kompanija" type="text" name="kompanija" size="30" /></div> </fieldset></div> <div class="form_para"><fieldset id="legborder"><legend id="legend">Project Information</legend> <div id="fieldset"><label class="labela" for="usluga">Type of website services *: </label> <select id="usluga" name="usluga"><option value="Select One...">Select One...</option><option value="Design of an entirely new website">Design of an entirely new website</option><option value="Complete redesign of a website">Complete redesign of a website</option><option value="Maintenance or upgrade to an existing website">Maintenance or upgrade to an existing website</option><option value="eCommerce Website">eCommerce Website</option><option value="Search Engine Optimization">Search Engine Optimization</option><option value="Other">Other</option></select><br /> <label class="labela" for="sajt">If you have a website. please indicate the URL: </label><input id="sajt" type="url" name="sajt" value="http://" size="30" /><br /> <br /> <label class="labela" for="logo">Do you have a logo for your new site?: </label> <select id="logo" name="logo"><option value="Yes">Yes</option><option value="No">No</option></select><br /> <label class="labela" for="budzet">Project Budget *: </label><select id="budzet" name="budzet"><option value="200$-500$">200$-500$</option><option value="500$-1000$">500$-1000$</option><option value="1000$-1500$">1000$-1500$</option><option value="1500$-2000$">1500$-2000$</option><option value="2000$-2500$">2000$-2500$</option><option value="2500$-5000$">2500$-5000$</option><option value="5000$ or more">5000$ or more</option></select><br /> <label class="labela" for="domen">Domain Name Registration: </label> <select id="domen" name="domen"><option value="Select One...">Select One...</option><option value="I need a Domain Name registered">I need a Domain Name registered</option><option value="I already have a Domain Name">I already have a Domain Name</option><option value="Not Sure Yet, Please Contact Me">Not Sure Yet, Please Contact Me</option></select><br /> <label class="labela" for="hosting">Website Hosting: </label> <select id="hosting" name="hosting"><option value="Select One...">Select One...</option><option value="I need Website Hosting">I need Website Hosting</option><option value="I already have Website Hosting">I already have Website Hosting</option><option value="Not Sure Yet, Please Contact Me">Not Sure Yet, Please Contact Me</option></select><br /> <label class="labela" for="opis">Which description best represents your desired e-store?</label> <select id="opis" name="opis"><option value="We do not intend to sell products on our site right now.">We do not intend to sell products on our site right now.</option><option value="Small store on a new site with a fewer than 40 products; paypal integration should do the trick.">Small store on a new site with a fewer than 40 products; paypal integration.</option><option value="We need a merchant account and have up to several hundred products; frequent updates.">We need a merchant account, up to several hundred products; we will update the store often.</option><option value="We are going to offer subscriptions and/or downloadable files.">We are going to offer subscriptions and/or downloadable files.</option></select><br /> <br /> <label class="labela" for="start">Project Start: </label> <select id="start" name="start"><option value="Select One....">Select One....</option><option value="ASAP">ASAP</option><option value="1-2 weeks">1-2 weeks</option><option value="3-4 weeks">3-4 weeks</option><option value="4+ weeks">4+ weeks</option></select><br /> <label class="labela" for="slicnost">Other websites that match the design and/or functionality you want for your website: </label> <input id="slicnost" type="url" name="slicnost" value="http://" size="30" /><br /> <input id="slicnost1" type="url" name="slicnost1" value="http://" size="30" /><br /> <label class="labela" for="poruka">Please describe your website project. </label><textarea id="poruka" class="txtarea" name="poruka" rows="9" cols="40"></textarea></div> </fieldset></div> <div class="form_para"><fieldset id="legborder"><legend id="legend">Other Important Functions Required</legend> <div class="radio_butt"><input id="cms" type="checkbox" name="cms" value="CMS and Website Editing" /> CMS - Website Editing<br /> <input id="blog" type="checkbox" name="blog" value="Blog" /> Blog<br /> <input id="feedback_forms" type="checkbox" name="feedback_forms" value="Feedback Forms" /> Feedback Forms<br /> <input id="newsletter" type="checkbox" name="newsletter" value="Newsletter Management" /> Newsletter Management</div> <div class="radio_butt"><input id="application_forms" type="checkbox" name="application_forms" value="Application Forms" /> Application Forms<br /> <input id="surveys" type="checkbox" name="surveys" value="Polls & Surveys" /> Polls & Surveys<br /> <input id="community_forum" type="checkbox" name="community_forum" value="Community Forum" /> Community Forum<br /> <input id="gallery_quote" type="checkbox" name="gallery" value="Photo Gallery" /> Photo Gallery</div> <div class="radio_butt"><input id="flash_headers" type="checkbox" name="flash_headers" value="Flash Headers" /> Headers<br /> <input id="tell_a_friend" type="checkbox" name="tell_a_friend" value="Tell_A_Friend Feature" /> Tell-A-Friend Feature<br /> <input id="website_search" type="checkbox" name="website_search" value="Website Search" /> Website Search<br /> <input id="registration" type="checkbox" name="registration" value="registration" /> Registration/Membership</div> <div class="radio_butt"><input id="call_back_forms" type="checkbox" name="call_back_forms" value="Call Back Forms" /> Call Back Forms<br /> <input id="file_uploading" type="checkbox" name="file_uploading" value="File Uploading" /> File Uploading<br /> <input id="social_media" type="checkbox" name="social_media" value="social_media" /> Social Media Integration<br /> <input id="logins" type="checkbox" name="logins" value="User/Password Logins" /> User/Password Logins</div> <div class="radio_butt"><input id="live_support" type="checkbox" name="live_support" value="Live Support" /> Live Support<br /> <input id="chat_room" type="checkbox" name="chat_room" value="Chat Room" /> Chat Room<br /> <input id="bilingual_text" type="checkbox" name="bilingual_text" value="Bilingual Text" /> Bilingual Text</div> </fieldset></div> <input class="buttons" type="submit" name="submit" value="submit" /> </form></div> </div> </div> --------This is JS which controls form----- <script type="text/javascript">// <![CDATA[ var frmvalidator = new Validator("forma"); frmvalidator.EnableOnPageErrorDisplay(); frmvalidator.EnableMsgsTogether(); frmvalidator.addValidation("ime","req","Please enter your First Name"); frmvalidator.addValidation("ime","maxlen=20", "Max length for FirstName is 20"); frmvalidator.addValidation("email","maxlen=50"); frmvalidator.addValidation("email","req","Please enter your Email"); // ]]></script> This is the way I use to store page into DB: if(isset($_POST['submit_no_roller'])){ $body = htmlspecialchars($_POST['body1']); $naslov = $_POST['naslov']; $lang_short = $_POST['lang_short']; $pozicija = $_POST['pozicija']; $query = "INSERT INTO pages VALUES (NULL, '$body', 0, '$naslov', '$pozicija', '$lang_short')"; $result = mysql_query($query); }
  10. Well I am new to web, PHP and MySQL and it seems good idea to store hole page into DB for easier accesses . Into DB is stored body of the page without header and footer. There is no error when I put page into DB. Everything is putted fine, but JS isn't loaded. However, when I put JS in footer.php it is loaded but I get this error: Error: couldn't get Form object forma. Error is on every other page, except on the page where form is located.
  11. I have html page inserted into table, and I need to insert JS that is specific for that page. When I insert it in DB nothing happens, but when I put it into footer.php it works just fine, but I keep getting error until I go to the specific page (JS is searching for specific form to load). I use function "htmlspecialchars" to insert page into DB. Does this have something to do with this. This is JS code: <script type="text/javascript">// <![CDATA[ var frmvalidator = new Validator("forma"); frmvalidator.EnableOnPageErrorDisplay(); frmvalidator.EnableMsgsTogether(); frmvalidator.addValidation("ime","req","Please enter your First Name"); frmvalidator.addValidation("ime","maxlen=20", "Max length for FirstName is 20"); frmvalidator.addValidation("email","maxlen=50"); frmvalidator.addValidation("email","req","Please enter your Email"); // ]]></script>
  12. Oops, my bad. This is line that is giving error: $jezik = getLanguage($languages, $lang , '2'); Thank you for detail explanation , it is very helpful .
  13. It is working , but I have a bit of a problem. Language selection is working, but when I go to some link within that language I get this error: Notice: Undefined index: lang in C:\wamp\www\ads\layout\head.php on line 4 and this is line 4: $languages = array ('Serbian'=>'1', 'English'=>'2'); And one more question, how I get language list from DB and put it into array (First go name of the language and then id of the language from DB)?
  14. Thank you . I will give a try .
  15. I want to create multilingual website using php script. I have MySQL table with language (with this fields: id_lang, lang, image, pozicija). So far I did this: if(isset($_GET['language'])){ $_SESSION['jezik'] = $_GET['language']; } elseif(!isset($_GET['language'])){ $_SESSION['jezik'] = 2; } When I choose new language it is working, but when I choose another link inside that language it is back to the default one. Here is the code for menu: $query_meni = "SELECT * FROM pages WHERE lang_id = '$jezik' ORDER BY pozicija"; $result = mysql_query($query_meni); $count = mysql_affected_rows(); if($count >0 ){ while($row_meni = mysql_fetch_array($result)){ $link = $row_meni['id_pages']; $show = $row_meni['naslov']; echo "<li><a href=\"index.php?page=$link\">$show</a>"; $query_subject = "SELECT * FROM subjects WHERE pages_id = '$row_meni[id_pages]' AND lang_id = '$jezik' ORDER BY pozicija ASC"; $result1 = mysql_query($query_subject); $count1 = mysql_affected_rows(); if($count1 == 0){ echo "</li>"; } else { echo "<ul class=\"subnav\">"; while($row_subjects = mysql_fetch_array($result1)){ $link1 = $row_subjects['subject_id']; $show1 = $row_subjects['naslov']; echo "<li> <a href=\"index.php?subject=$link1\">$show1</a></li>"; } echo "</ul>"; } echo "</li>"; } echo "</li>"; } echo "</li>"; ?> What am I doing wrong?
  16. It is working perfectly . Thank you for help.
  17. I have two sessions on one page: Session #1: if(isset($_POST['sezonaSubmit'])){ $_SESSION['sezona'] = $_POST['sezona']; } else { $_SESSION['sezona'] = "Letnja Guma"; } $sezona = $_SESSION['sezona']; Session #2: if(isset($_POST['poredak'])){ $_SESSION['poredak'] = $_POST['red']; } else { $_SESSION['poredak'] = " ORDER BY price ASC"; } $order = $_SESSION['poredak']; And they are used to change order on the list. Problem is when I change one, other session is changed to the default value. How to fix this?
  18. How can I insert PHP code inside MySQL DB? I have mixed HTML and PHP code like this: <div class="rightpanel"> <div class="hltred"> <div class="hlthead"> <p><?php echo $pages->naslov; ?></p> </div> <div class="hltcontent"> <p> ovde treba da ide loop sa svi podlinkovi za taj glavni koji je aktivan</p> </div> </div> </div>, but when i insert it into DB i get this code (I use htmlspecialchars function to store): <div class="rightpanel"> <div class="hltred"> <div class="hlthead"> <p> <!--?php echo $pages---> naslov; ?></p> </div> <div class="hltcontent"> <p> ovde treba da ide loop sa svi podlinkovi za taj glavni koji je aktivan</p> </div> </div> </div>
  19. It is working perfectly . Thank you a lot for helping me .
  20. I think that I am doing something wrong here, but I cannot see error. I managed to create submenu for specific menu, but it only shows one item in subemnu. Here is the code: <?php $query_meni =mysql_query("SELECT * FROM meni WHERE lang_id = '$jezik'"); if(mysql_affected_rows() >0 ){ while($row_meni = mysql_fetch_array($query_meni)){ echo "<li>"; echo "<a href=\"index.php?page="; echo $row_meni['id_meni']; echo "\">"; echo $row_meni['naslov']; echo "</a>"; $query_subject =mysql_query("SELECT * FROM subjects WHERE meni_id = '$row_meni[id_meni]' AND lang_id = '$jezik' ORDER BY pozicija ASC"); if(mysql_affected_rows() == 0){ echo "<li>";} else { while($row_subjects = mysql_fetch_array($query_subject)){ echo "<ul class=\"subnav\">"; echo "<li>"; echo "<a href=\"index.php?subject="; echo $row_subjects['subject_id']; echo "\">"; echo $row_subjects['naslov']; echo "</li>"; echo "</a>"; echo "</ul>"; } } } echo "</li>"; } ?>
  21. I am trying to make menu with submenu. I have two tables: meni(id_meni, naslov, pozicija, lang_id) and subjects (subjecte_id, naslov, text, meni_id, lang_id, pozicija). Into subjects(meni_id) is written meni(id_meni) value in order to determinate what menu items are having summenu options. How can i do this? I tried following code, but it only display items from meni table: <?php $query_meni =mysql_query("SELECT * FROM meni WHERE lang_id = '$jezik'"); $query_subject =mysql_query("SELECT * FROM subjects WHERE lang_id = '$jezik'"); while($row_meni = mysql_fetch_array($query_meni)){ echo "<li>"; echo "<a href=\"index.php?page="; echo $row_meni['id_meni']; echo "\">"; echo $row_meni['naslov']; echo "</a>"; while($row_subjects = mysql_fetch_array($query_subject)){ if($row_meni['id_meni'] == $row_subjects['meni_id']){ echo "<ul class=\"subnav\">"; echo "<li>"; echo "<a href=\"index.php?subject="; echo $row_subjects['subject_id']; echo "\">"; echo $row_subjects['naslov']; echo "</li>"; echo "</a>"; echo "</ul>"; } } echo "</li>"; } ?>
  22. I want to create menu with submenu. Here is HTML code: <!--<li><a href="#">Home</a></li> <li><a href="#">About Us</a></li> <li><a href="#">Web Design</a> <ul class="subnav"> <li><a href="#">Corporate</a></li> <li><a href="#">Small Business</a></li> <li><a href="#">HTML Templates</a></li> <li><a href="#">Joomla Templates</a></li> </ul> </li> <li><a href="#">Web Development</a> <ul class="subnav"> <li><a href="#">eCommerce</a></li> <li><a href="#">CMS</a></li> <li><a href="#">Custom Features</a></li> <li><a href="#">Web Maintenance</a></li> </ul> </li> <li><a href="#">Internet Marketing</a> <ul class="subnav"> <li><a href="#">SEO</a></li> <li><a href="#">Pay Per Click</a></li> <li><a href="#">Social Media Marketing</a></li> <li><a href="#">Link Building</a></li> <li><a href="#">Content Marketing</a></li> <li><a href="#">Reputation Management</a></li> </ul> </li> <li><a href="#">Sound</a></li> <li><a href="#">3D</a></li> <li><a href="#">Portfolio</a></li> <li><a href="#">Quote</a></li> <li><a href="#">Contact</a></li>--> and this is php code which is trying to imitate HTML: <?php $query_meni =mysql_query("SELECT * FROM meni WHERE lang_id = '$jezik'"); $query_subject =mysql_query("SELECT * FROM subjects WHERE lang_id = '$jezik'"); while($row_meni = mysql_fetch_array($query_meni)){ echo "<li>"; echo "<a href=\"index.php?page="; echo $row_meni['id_meni']; echo "\">"; echo $row_meni['naslov']; echo "</a>"; while($row_subjects = mysql_fetch_array($query_subject)){ if($row_meni['id_meni'] == $row_subjects['meni_id']){ echo "<ul class=\"subnav\">"; echo "<li>"; echo "<a href=\"index.php?subject="; echo $row_subjects['subject_id']; echo "\">"; echo $row_subjects['naslov']; echo "</li>"; echo "</a>"; echo "</ul>"; } } echo "</li>"; } ?> Any idea why this is not working?
  23. I am trying to create insert function when there is no picture. Here is the code: if(isset($_POST['submit'])) { $pages = new Pages(); $pages->body = htmlspecialchars($_POST['body']); $pages->meni_id = $_POST['meni_id']; $pages->lang_id = $_POST['lang_id']; if(!empty($_FILES['image'])){$pages->attach_file($_FILES['image']); } else { $body = htmlspecialchars($_POST['body']); $meni_id = $_POST['meni_id']; $lang_id = $_POST['lang_id']; $roller = 0; $query = "INSERT INTO pages VALUES (NULL, '$body', '$roller', '$meni_id', '$lang_id')"; $result = mysql_query($query); $session->message("Strana je uspesno uneta."); } if($pages->save()) { // Success $session->message("Strana je uspesno uneta."); redirect_to('index.php?page=pages'); } else { // Failure $message = join("<br />", $pages->errors); } } It works fine when picture is selected, but it does not work when one is selected. How to make it work?
  24. This helps a lot . Thank you.
  25. This is the hole class: class Jezik { protected static $table_name=" language"; protected static $db_fields=array('id_lang', 'lang', 'image', 'pozicija'); public $id_lang; public $lang; public $image; public $pozicija; private $temp_path; protected $upload_dir="images"; public $errors=array(); protected $upload_errors = array( // http://www.php.net/manual/en/features.file-upload.errors.php UPLOAD_ERR_OK => "No errors.", UPLOAD_ERR_INI_SIZE => "Larger than upload_max_filesize.", UPLOAD_ERR_FORM_SIZE => "Larger than form MAX_FILE_SIZE.", UPLOAD_ERR_PARTIAL => "Partial upload.", UPLOAD_ERR_NO_FILE => "No file.", UPLOAD_ERR_NO_TMP_DIR => "No temporary directory.", UPLOAD_ERR_CANT_WRITE => "Can't write to disk.", UPLOAD_ERR_EXTENSION => "File upload stopped by extension." ); // Pass in $_FILE(['uploaded_file']) as an argument public function attach_file($file) { // Perform error checking on the form parameters if(!$file || empty($file) || !is_array($file)) { // error: nothing uploaded or wrong argument usage $this->errors[] = "No file was uploaded."; return false; } elseif($file['error'] != 0) { // error: report what PHP says went wrong $this->errors[] = $this->upload_errors[$file['error']]; return false; } else { // Set object attributes to the form parameters. $this->temp_path = $file['tmp_name']; $this->image = basename($file['name']); // Don't worry about saving anything to the database yet. return true; } } public function save() { // A new record won't have an id yet. if(isset($this->id)) { // Really just to update the caption $this->update(); } else { // Make sure there are no errors // Can't save if there are pre-existing errors if(!empty($this->errors)) { return false; } // Can't save without filename and temp location if(empty($this->image) || empty($this->temp_path)) { $this->errors[] = "The file location was not available."; return false; } // Determine the target_path $target_path = SITE_ROOT .DS.$this->upload_dir .DS. $this->image; // Attempt to move the file if(move_uploaded_file($this->temp_path, $target_path)) { // Success // Save a corresponding entry to the database if($this->create()) { // We are done with temp_path, the file isn't there anymore unset($this->temp_path); return true; } } else { // File was not moved. $this->errors[] = "The file upload failed, possibly due to incorrect permissions on the upload folder."; return false; } } } public function destroy() { // First remove the database entry if($this->delete()) { // then remove the file // Note that even though the database entry is gone, this object // is still around (which lets us use $this->image_path()). $target_path = SITE_ROOT.DS.$this->image_path(); return unlink($target_path) ? true : false; } else { // database delete failed return false; } } public function image_path() { return $this->upload_dir.DS.$this->filename; } public function size_as_text() { if($this->size < 1024) { return "{$this->size} bytes"; } elseif($this->size < 1048576) { $size_kb = round($this->size/1024); return "{$size_kb} KB"; } else { $size_mb = round($this->size/1048576, 1); return "{$size_mb} MB"; } } // Common Database Methods public static function find_all() { return self::find_by_sql("SELECT * FROM ".self::$table_name); } public static function find_by_id($id=0) { global $database; $result_array = self::find_by_sql("SELECT * FROM ".self::$table_name." WHERE product_id=".$database->escape_value($id)." LIMIT 1"); return !empty($result_array) ? array_shift($result_array) : false; } public static function find_by_sql($sql="") { global $database; $result_set = $database->query($sql); $object_array = array(); while ($row = $database->fetch_array($result_set)) { $object_array[] = self::instantiate($row); } return $object_array; } public static function count_all() { global $database; $sql = "SELECT COUNT(*) FROM ".self::$table_name; $result_set = $database->query($sql); $row = $database->fetch_array($result_set); return array_shift($row); } private static function instantiate($record) { // Could check that $record exists and is an array $object = new self; // Simple, long-form approach: // $object->id = $record['id']; // $object->username = $record['username']; // $object->password = $record['password']; // $object->first_name = $record['first_name']; // $object->last_name = $record['last_name']; // More dynamic, short-form approach: foreach($record as $attribute=>$value){ if($object->has_attribute($attribute)) { $object->$attribute = $value; } } return $object; } private function has_attribute($attribute) { // We don't care about the value, we just want to know if the key exists // Will return true or false return array_key_exists($attribute, $this->attributes()); } protected function attributes() { // return an array of attribute names and their values $attributes = array(); foreach(self::$db_fields as $field) { if(property_exists($this, $field)) { $attributes[$field] = $this->$field; } } return $attributes; } protected function sanitized_attributes() { global $database; $clean_attributes = array(); // sanitize the values before submitting // Note: does not alter the actual value of each attribute foreach($this->attributes() as $key => $value){ $clean_attributes[$key] = $database->escape_value($value); } return $clean_attributes; } // replaced with a custom save() // public function save() { // // A new record won't have an id yet. // return isset($this->id) ? $this->update() : $this->create(); // } public function create() { global $database; // Don't forget your SQL syntax and good habits: // - INSERT INTO table (key, key) VALUES ('value', 'value') // - single-quotes around all values // - escape all values to prevent SQL injection $attributes = $this->sanitized_attributes(); $sql = "INSERT INTO ".self::$table_name." ("; $sql .= join(", ", array_keys($attributes)); $sql .= ") VALUES ('"; $sql .= join("', '", array_values($attributes)); $sql .= "')"; if($database->query($sql)) { $this->product_id = $database->insert_id(); return true; } else { return false; } } public function update() { global $database; // Don't forget your SQL syntax and good habits: // - UPDATE table SET key='value', key='value' WHERE condition // - single-quotes around all values // - escape all values to prevent SQL injection $attributes = $this->sanitized_attributes(); $attribute_pairs = array(); foreach($attributes as $key => $value) { $attribute_pairs[] = "{$key}='{$value}'"; } $sql = "UPDATE ".self::$table_name." SET "; $sql .= join(", ", $attribute_pairs); $sql .= " WHERE id=". $database->escape_value($this->id); $database->query($sql); return ($database->affected_rows() == 1) ? true : false; } public function delete() { global $database; // Don't forget your SQL syntax and good habits: // - DELETE FROM table WHERE condition LIMIT 1 // - escape all values to prevent SQL injection // - use LIMIT 1 $sql = "DELETE FROM ".self::$table_name; $sql .= " WHERE id=". $database->escape_value($this->id); $sql .= " LIMIT 1"; $database->query($sql); return ($database->affected_rows() == 1) ? true : false; // NB: After deleting, the instance of User still // exists, even though the database entry does not. // This can be useful, as in: // echo $user->first_name . " was deleted"; // but, for example, we can't call $user->update() // after calling $user->delete(). } } ?>
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.