Jump to content

PHP form post and database insert


razorsese

Recommended Posts

Ok .I tried to look for the problem for about 1 hour but I can't see what's wrong whit the following code:

In firebug the post values appear correctly like so:id=116&usern=asdawe&com=qweqw&page=116&submit=submit

and the inserc() function works correctly because i tried it whit a array of data created by my;


<?php
if(isset($_POST['submit']))
{
$comment = new comment;
$comment->storeform($_POST);
$comment->insertc();
}
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="hidden" name="id" value="<?php echo $result['article']->id ?>"/>

<ul>
<li>
  <input type="text" name="usern" id="usern" />
</li>

<li>
<textarea name="com" id="com" COLS=40 ROWS=6></textarea>
</li>

<input type="hidden" name="page" value="<?php echo $result['article']->id ?>" />
<input type="submit" name="submit" value="submit" />


</ul>

</form>

 


<?php

class comment{

public $id = null;
public $usern = null;
public $com = null;
public $page = null;

public function __construct($data=array() )
{
	if( isset($data['id']) ) $this->id= (int)$data['id'];
	if( isset($data['usern']) ) $this->usern= preg_replace ( "/[^\.\,\-\_\'\"\@\?\!\:\$ a-zA-Z0-9()]/", "", $data['usern'] );
	if( isset($data['com']) ) $this->com= preg_replace ( "/[^\.\,\-\_\'\"\@\?\!\:\$ a-zA-Z0-9()]/", "", $data['com'] );
	if( isset($data['page']) ) $this->page= (int)$data['page'];

}

public function storeform ( $params ) {

    // Store all the parameters
    $this->__construct( $params );
}

public function insertc()
{
	$con = new PDO(DBN,DB_USER,DB_PASS);
    $sql = "INSERT INTO comments (usern,com,page) VALUES (:usern,:com,:page)";
	$st =$con->prepare($sql);	
	$st->bindValue( ":usern", $this->usern, PDO::PARAM_STR );
        $st->bindValue( ":com", $this->com, PDO::PARAM_STR );
	$st->bindValue( ":page", $this->page, PDO::PARAM_INT );
        $st->execute();
	$this->id = $con->lastInsertId();
        $con = null;
}
}

?>

Link to comment
https://forums.phpfreaks.com/topic/260108-php-form-post-and-database-insert/
Share on other sites

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.