I am having problem in inserting using PDO statement. I am moving from mysql to PDO statement. So your opinions are welcome. It is not a duplicate question at all. I have checked other questions but could not get the result. So, please don't mark it as a duplicate question.
<?php
class Stock{
const DB_HOST = 'localhost';
const DB_NAME = 'stock';
const DB_USER = 'root';
const DB_PASSWORD = '';
private $conn = null;
/**
* Open the database connection
*/
public function __construct(){
// open database connection
$connectionString = sprintf("mysql:host=%s;dbname=%s",
Stock::DB_HOST,
Stock::DB_NAME);
try{
$this->conn = new PDO($connectionString,
Stock::DB_USER,
Stock::DB_PASSWORD);
}
catch(PDOException $e){
die($e->getMessage());
}
}//end of constructor
public function insert_stock($stock_symbol,$stock_name,$no_of_trans,$max_price,$min_price,$closing_price,$total_shares,$amount,$previous_close,$difference){
$data = array(
':stock_symbol' => $stock_symbol,
':stock_name' => $stock_name,
':no_of_trans' => $no_of_trans,
':max_price' => $max_price,
':min_price' => $min_price,
':closing_price' => $closing_price,
':total_shares' => $total_shares,
':amount' => $amount,
':previous_close' => $previous_close,
':difference' => $difference
);
$sql = 'INSERT INTO tbl_stock(stock_symbol,stock_name,no_of_trans,max_price,min_price.closing_price,total_shares,amount,previous_close,difference)
VALUES(:stock_symbol,:stock_name,:no_of_trans,:max_price,:min_price,:closing_price,:total_shares,:amount,:previous_close,:difference)';
$q = $this->conn->prepare($sql);
return $q->execute($data);
}
}//end of class
$obj = new Stock;
if($obj->insert_stock('TES','Testing',5,500,600,200,2000,200000,600,10)!= false){
echo 'One row added sucessfully';
}
else{
echo 'Error adding the task';
}
?>
I have also got the id in my tbl_stock
table which has auto_increment
in it.