Jump to content

[SOLVED] Stock class - how to insert to database?


colombian

Recommended Posts

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.

 

 

 

<?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.

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.

 

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();
?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.