razorsese Posted March 31, 2012 Share Posted March 31, 2012 Need some help whit the following code: The problem is when i try to insert into database it appears only the first letter in the name column and in the text column it appears exact letter like in the name column. newArticle(); function newArticle() { $article = new Article; $article->storeFormValues("SOMENAME","SOMETEXT"); $article->insert(); echo $article->id." ".$article->name." "; } class Article{ public $id = null; public $name = null; public $text = null; public function __construct($data=array() ) { if( isset($data['id']) ) $this->id= $data['id']; if( isset($data['name']) ) $this->name= preg_replace ( "/[^\.\,\-\_\'\"\@\?\!\:\$ a-zA-Z0-9()]/", "", $data['name'] ); if( isset($data['text']) ) $this->text= preg_replace ( "/[^\.\,\-\_\'\"\@\?\!\:\$ a-zA-Z0-9()]/", "", $data['text'] ); } public function storeFormValues ( $params ) { // Store all the parameters $this->__construct( $params ); } public function insert() { $con = new PDO(DBN,DB_USER,DB_PASS); $sql = "INSERT INTO articles (name,text) VALUES (:name, :text) "; $st = $con->prepare($sql); $st->bindValue(":name", $this->name, PDO::PARAM_STR); $st->bindValue(":text", $this->text, PDO::PARAM_STR); $st->execute(); $this->id = $con->lastInsertId(); $con=null; } } Quote Link to comment https://forums.phpfreaks.com/topic/260063-php-class/ Share on other sites More sharing options...
requinix Posted March 31, 2012 Share Posted March 31, 2012 You're calling storeFormValues() wrong. It (more specifically __construct) wants an associative array. Quote Link to comment https://forums.phpfreaks.com/topic/260063-php-class/#findComment-1332971 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.