Jump to content

just want some little help more from you seniors


Ehsan_collboy

Recommended Posts


        $sql = "INSERT INTO comments(name,datetime,email,comments,approvedby,status,post_id)";
        $sql .= "VALUES(:name,:email,:datetime,:comments,'Pending','OFF',:post_id)";
        $stmt = $cn->prepare($sql);
        $stmt->bindValue(":name",$name);
        $stmt->bindValue(":email",$email);
        $stmt->bindValue(":comments",$comments);
error in this line 
        $stmt->bindValue(":post_id",$searchqueryparameters);
    
        $sucess = $stmt->execute();
        if($sucess) {
            $_SESSION["successMessage"] = "Comments has been post and Waiting for Aprovel";
    
            redirect("fullpost.php?id=$searchqueryparameters");
        }else {
            $_SESSION["ErrorMessage"] = "System Error Try Again...!";
            redirect("fullpost.php?id=searchqueryparameters");
        }

hi seniors my code is working file till when i didn't add the post_id when i add my post_id variable in the code thy show me the error and also i join the two tables in the database post_id is coming from the post tabel

 


Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in C:\xampp\htdocs\CMS\Fullpost.php:49 Stack trace: #0 C:\xampp\htdocs\CMS\Fullpost.php(49): PDOStatement->execute() #1 {main} thrown in C:\xampp\htdocs\CMS\Fullpost.php on line 49

Link to comment
Share on other sites

Looks to me like you have 5 placeholders and 4 bound values for them, hence the error message (They usually tell you what the problem is - you just have to read them)

7 minutes ago, Ehsan_collboy said:

number of bound variables does not match number of tokens

 

What does your table structure look like?

If datetime is the current timestamp and post_id is an auto_incremented primary key then they don't need to be in the insert statement.

Link to comment
Share on other sites

You don't want to create a field called 'datetime' as varchar.  It s/b a DateTime type.

 $sql = "INSERT INTO comments
			(name, datetime, email, comments, approvedby, status, post_id) 
        VALUES(:name, :email, :datetime, :comments, 'Pending', 'OFF', :post_id)";

Looking at your query statement do you see that you have mis-aligned fieldnames and values?  You're trying to load an email into a datetime field and the reverse

PS - I recommend switching to the array method of assigning the values instead of the tedious bind function as in this:

 $sql = "INSERT INTO comments(name,datetime,email,comments,approvedby,status,post_id)";
        $sql .= "VALUES(:name,:email,:datetime,:comments,'Pending','OFF',:post_id)";

$parms = array(
		':name'=>$name,
		':datetime'=>$dt,
		':email'=>$email,
		'comments'=>$comments,
		':post_id'=>$postid);
if($sucess = $stmt->execute($parms))
{

 

Edited by ginerjm
  • Great Answer 1
Link to comment
Share on other sites

I'd define it something like this

CREATE TABLE `comments` (
  `post_id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  `datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `email` varchar(50) DEFAULT NULL,
  `comments` text,
  `approvedby` varchar(50) DEFAULT NULL,
  `status` tinyint(4) DEFAULT NULL,
  PRIMARY KEY (`post_id`)
)

then, as stated earlier, datetime and post_id are generated automatically and can be excluded from the INSERT.

 

As @ginerjm said, don't use varchar for dates. They should be DATE, DATETIME or TIMESTAMP and the format should be yyyy-mm-dd. Store them for functionality, not prettiness. Format them as required on output or in the query.

  • Great Answer 1
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.