colombian Posted May 22, 2008 Share Posted May 22, 2008 Below is an over-simplification of what I am trying to accomplish. I am having a hard time grasping some of this object oriented programming concepts. I want to know how to go from the class, to the insertion into MySQL. <?php class Stock { public $Symbol public $Price public function __construct ( $in_symbol $in_price ) { $this->Symbol = $in_symbol; $this->Price = $in_price; } } ?> I could just create the object with the data... So we have an html form letting the user pick the symbol and price. They click submit... How do I go from this to insert the data into MySQL OOP style? $stock1 = new Stock($in_pid, $in_price); Do I just insert it like typical procedural way? $sql = "INSERT INTO ... etc" If done this way, the class seems to be a waste, since it is not accomplishing anything. Any help would be appreciated. Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/106731-solved-stock-class-how-to-insert-to-database/ Share on other sites More sharing options...
bilis_money Posted May 22, 2008 Share Posted May 22, 2008 My advice is don't use OOP if you are solving small codes only. OOP is good for large projects only, or if you needed to create many objects. Ok, hold on while i'll formulate you some snippet codes for your problem. Link to comment https://forums.phpfreaks.com/topic/106731-solved-stock-class-how-to-insert-to-database/#findComment-547154 Share on other sites More sharing options...
bilis_money Posted May 22, 2008 Share Posted May 22, 2008 <?php class Stock { var $Symbol; var $Price; set_data($in_symbol, $in_price) { $this->Symbol = $in_symbol; $this->Price = $in_price; } get_data() { RETURN $this->$Symbol; RETURN $this->$Price; } function insert_data($in_symbol, $in_price) { $q = "INSERT INTO ".stock_table." VALUES ('$Symbol', $Price)"; return mysql_query($q, $this->connection); } } $Stock = new Stock(); $Stock->set_data('dasymbol', 100); $Stock->insert_data(); ?> notice i did not included the constructor because it is just a simple demo 4 u. Link to comment https://forums.phpfreaks.com/topic/106731-solved-stock-class-how-to-insert-to-database/#findComment-547163 Share on other sites More sharing options...
colombian Posted May 23, 2008 Author Share Posted May 23, 2008 Many thanks. This actually helps. I am not writing any large scale projects at the moment - but I want to be ready and learn OOP and start using it, even on small scale ones. Besides, I will likely start working on larger projects very soon. I will play around with this code to see what I can do - thanks again. I will use the constructor though, not sure why you didn't. I understand the use of the function inside the class better, many thanks for taking the time to show me that snippet. Link to comment https://forums.phpfreaks.com/topic/106731-solved-stock-class-how-to-insert-to-database/#findComment-547882 Share on other sites More sharing options...
colombian Posted May 23, 2008 Author Share Posted May 23, 2008 Additional question... I changed this to work with a constructor (just a sample), and it works just as intended - for a 2 column table. However, if I have an auto_num id column as the first one, it complains that the values do not match. My confusion is - typically in SQL queries you don't need to pass a value for the id auto_num column, but it is apparently asking for one - what should I do? Thank you. Code below: <?php class Name { public $Ages; public $Names; public function __construct ($name, $age) { $this->Ages = $age; $this->Names = $name; } public function add_name() { $query = "INSERT INTO name_age VALUES ('$this->Names', $this->Ages)"; $result = mysql_query($query); if (mysql_affected_rows() > 0) { echo "Success!"; } else { echo "<p> Failed</p>" . mysql_error() . "</p> <a href=\"staff.php\"> Return to main page </a>"; } } } $name1 = new Name('John', 28); $name1->add_name(); ?> Link to comment https://forums.phpfreaks.com/topic/106731-solved-stock-class-how-to-insert-to-database/#findComment-548597 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.