Ehsan_collboy Posted April 16, 2022 Share Posted April 16, 2022 $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 Quote Link to comment https://forums.phpfreaks.com/topic/314706-just-want-some-little-help-more-from-you-seniors/ Share on other sites More sharing options...
Barand Posted April 16, 2022 Share Posted April 16, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/314706-just-want-some-little-help-more-from-you-seniors/#findComment-1595408 Share on other sites More sharing options...
Ehsan_collboy Posted April 16, 2022 Author Share Posted April 16, 2022 i set my datetime in varcahar type and post_id is set as index type in database Quote Link to comment https://forums.phpfreaks.com/topic/314706-just-want-some-little-help-more-from-you-seniors/#findComment-1595409 Share on other sites More sharing options...
Ehsan_collboy Posted April 16, 2022 Author Share Posted April 16, 2022 Thanks Senior for open my eyes Quote Link to comment https://forums.phpfreaks.com/topic/314706-just-want-some-little-help-more-from-you-seniors/#findComment-1595410 Share on other sites More sharing options...
ginerjm Posted April 16, 2022 Share Posted April 16, 2022 (edited) 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 April 16, 2022 by ginerjm 1 Quote Link to comment https://forums.phpfreaks.com/topic/314706-just-want-some-little-help-more-from-you-seniors/#findComment-1595411 Share on other sites More sharing options...
Barand Posted April 16, 2022 Share Posted April 16, 2022 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/314706-just-want-some-little-help-more-from-you-seniors/#findComment-1595412 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.