Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 致命错误:未捕获PDOException:SQLSTATE[HY093]:无效参数编号:绑定变量的数量与令牌的数量不匹配_Php_Mysql - Fatal编程技术网

Php 致命错误:未捕获PDOException:SQLSTATE[HY093]:无效参数编号:绑定变量的数量与令牌的数量不匹配

Php 致命错误:未捕获PDOException:SQLSTATE[HY093]:无效参数编号:绑定变量的数量与令牌的数量不匹配,php,mysql,Php,Mysql,大家好,我试图解决这个问题好几天,但没有结果,你能帮我吗? 问题是: 致命错误:未捕获PDO异常:SQLSTATE[HY093]:无效参数编号:绑定变量的数量与C:\xampp\htdocs\dashboard\sn\classes\DB.php:12堆栈跟踪:0 C:\xampp\htdocs\dashboard\sn\classes\DB.php12:PDOStatement->executeArray 1 C:\xampp\htdocs\dashboard\sn\classes\Post.

大家好,我试图解决这个问题好几天,但没有结果,你能帮我吗? 问题是:

致命错误:未捕获PDO异常:SQLSTATE[HY093]:无效参数编号:绑定变量的数量与C:\xampp\htdocs\dashboard\sn\classes\DB.php:12堆栈跟踪:0 C:\xampp\htdocs\dashboard\sn\classes\DB.php12:PDOStatement->executeArray 1 C:\xampp\htdocs\dashboard\sn\classes\Post.php24:DB::query“插入pos…”,数组2 C:\xampp\htdocs\dashboard\sn\profile.php54:Post::createImgPost,'1',1'3{main}在第12行的C:\xampp\htdocs\dashboard\sn\classes\DB.php中抛出

下面是Post.php文件:

<?php

class Post {
        public static function createPost($postbody, $loggedInUserId, $profileUserId) {

                if (strlen($postbody) > 160 || (strlen($postbody) < 1)) {
                        die('Incorrect lenght!');
                }

                $topics = self::getTopics($postbody);

                if ($loggedInUserId == $profileUserId) {
                        DB::query('INSERT INTO posts VALUES (:id, :postbody, NOW(), :userid, 0, \'\', :topics)', array(':id'=>0, ':postbody'=>$postbody, ':userid'=>$profileUserId, ':topics'=>$topics));
                } else {
                        die('Incorrect user!');
                }
        }

        public static function createImgPost($postbody, $loggedInUserId, $profileUserId) {
                if (strlen($postbody) > 160) {
                        die('Incorrect length!');
                }
                $topics = self::getTopics($postbody);
                if ($loggedInUserId == $profileUserId) {
                        DB::query('INSERT INTO posts VALUES (\'\', :postbody, NOW(), :userid, 0, \'\', \'\')', array(':postbody'=>$postbody, ':userid'=>$profileUserId, ':topics'=>$topics));
                        $postid = DB::query('SELECT id FROM posts WHERE user_id=:userid ORDER BY ID DESC LIMIT 1;', array(':userid'=>$loggedInUserId))[0]['id'];
                        return $postid;
                } else {
                  die('Incorrect user!');
                }
          }

        public static function likePost($postId, $likerId) {
                if (!DB::query('SELECT user_id FROM post_likes WHERE post_id=:postid and user_id=:userid', array(':postid'=>$postId, ':userid'=>$likerId))) {
                        DB::query('UPDATE posts SET likes=likes+1 WHERE id=:postid', array(':postid'=>$postId));
                        DB::query('INSERT INTO post_likes VALUES(:id, :postid, :userid)', array(':id'=>0, ':postid'=>$postId, ':userid'=>$likerId));
                } else {
                        DB::query('UPDATE posts SET likes=likes-1 WHERE id=:postid', array(':postid'=>$postId));
                        DB::query('DELETE FROM post_likes WHERE post_id=:postid AND user_id=:userid', array(':postid'=>$postId, ':userid'=>$likerId));
                }
        }

        public static function link_add($text) {

                        $text = explode(" ", $text);
                        $newstring = "";

                        foreach ($text as $word) {
                              if (substr($word, 0, 1) == "@") {
                                      $newstring .= "<a href='profile.php?username=".substr($word, 1)."'>".htmlspecialchars($word)."</a>";
                              } else if (substr($word, 0, 1) == "#") {
                                      $newstring .= "<a href='topics.php?topic=".substr($word, 1)."'>".htmlspecialchars($word)."</a>";
                              } else {
                                      $newstring .= htmlspecialchars($word)." ";
                              }
                        }

                        return $newstring;
        }
        public static function getTopics($text) {

                        $text = explode(" ", $text);
                        $topics = "";

                        foreach ($text as $word) {
                              if (substr($word, 0, 1) == "#") {
                                      $topics .= substr($word, 1).",";
                              }
                        }

                        return $topics;
        }

        public static function displayPosts($userid, $username, $loggedInUserId) {
                        $dbposts = DB::query('SELECT * FROM posts WHERE user_id=:userid ORDER BY id DESC', array(':userid'=>$userid));
                        $posts = "";
                        foreach($dbposts as $p) {

                                if (!DB::query('SELECT post_id FROM post_likes WHERE post_id=:postid AND user_id=:userid', array(':postid'=>$p['id'], ':userid'=>$loggedInUserId))) {

                                        $posts .= "<img src='".$p['postimg']."'>".self::link_add($p['body'])."
                                        <form action='profile.php?username=$username&postid=".$p['id']."' method='post'>
                                              <input type='submit' name='like' value='Like'>
                                              <span>".$p['likes']." likes</span>
                                        </form>
                                        <hr /></br />
                                        ";
                                } else {
                                        $posts .= "<img src='".$p['postimg']."'>".self::link_add($p['body'])."
                                        <form action='profile.php?username=$username&postid=".$p['id']."' method='post'>
                                              <input type='submit' name='unlike' value='Unlike'>
                                              <span>".$p['likes']." likes</span>
                                        </form>
                                        <hr /></br />
                                        ";
                                }
                        }
                        return $posts;
        }
   }

   ?>
谢谢你的时间和精力

    $postImg = ""; 
    $topics = self::getTopics($postbody); 
    if ($loggedInUserId == $profileUserId) { 
       DB::query('INSERT INTO posts VALUES (:id, :postbody, NOW(), :userid, 0, :postimg, :topics)', array(':id'=>0,':postbody'=>$postbody, ':userid'=>$profileUserId, ':postimg'=>$postImg,':topics'=>$topics)); 
谢谢你的回答,你是对的,但这不是问题,我不知道如何设置\'\'的值,因为我尝试了很多方法,但都没有效果,但最终我解决了问题,我必须设置值并创建新变量。。。
我感觉很好:DDD

没有$topics$postImg=的占位符$topics=self::getTopics$postbody;如果$loggedInUserId==$profileUserId{DB::query'INSERT-INTO-posts-VALUES:id,:postbody,NOW,:userid,0,:postmg,:topics',array':id'=>0':postbody'=>$postbody':userid'=>$profileUserId',:postmg'=>$postmg':topics'=>topics;谢谢你的回答,你是对的,但这不是问题,我不知道如何为\'设置值,因为我试过l很多事情都没有成功,但最终我解决了我觉得很完美:DDD
                        DB::query('INSERT INTO posts VALUES (\'\', :postbody, NOW(), :userid, 0, \'\', \'\')', array(':postbody'=>$postbody, ':userid'=>$profileUserId, ':topics'=>$topics));
    $postImg = ""; 
    $topics = self::getTopics($postbody); 
    if ($loggedInUserId == $profileUserId) { 
       DB::query('INSERT INTO posts VALUES (:id, :postbody, NOW(), :userid, 0, :postimg, :topics)', array(':id'=>0,':postbody'=>$postbody, ':userid'=>$profileUserId, ':postimg'=>$postImg,':topics'=>$topics));