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
Share on other sites

the reason no one has responded yet is probably because you never said what is going wrong.

 

so rather than us just looking to see why something is going wrong, we have to guess and look for what might be going on wrong and what might work to finish it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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