gpt4 book ai didi

php - fatal error : Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

转载 作者:行者123 更新时间:2023-11-29 10:52:39 26 4
gpt4 key购买 nike

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

Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in C:\xampp\htdocs\dashboard\sn\classes\DB.php:12 Stack trace: #0 C:\xampp\htdocs\dashboard\sn\classes\DB.php(12): PDOStatement->execute(Array) #1 C:\xampp\htdocs\dashboard\sn\classes\Post.php(24): DB::query('INSERT INTO pos...', Array) #2 C:\xampp\htdocs\dashboard\sn\profile.php(54): Post::createImgPost('', '1', '1') #3 {main} thrown in C:\xampp\htdocs\dashboard\sn\classes\DB.php on line 12

这里是 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;
}
}

?>

这是 profile.php 文件:

<?php
include('./classes/DB.php');
include('./classes/Login.php');
include('./classes/Post.php');
include('./classes/Image.php');
$username = "";
$verified = False;
$isFollowing = False;
if (isset($_GET['username'])) {
if (DB::query('SELECT username FROM users WHERE username=:username', array(':username'=>$_GET['username']))) {
$username = DB::query('SELECT username FROM users WHERE username=:username', array(':username'=>$_GET['username']))[0]['username'];
$userid = DB::query('SELECT id FROM users WHERE username=:username', array(':username'=>$_GET['username']))[0]['id'];
$verified = DB::query('SELECT verified FROM users WHERE username=:username', array(':username'=>$_GET['username']))[0]['verified'];
$followerid = Login::isLoggedIn();
if (isset($_POST['follow'])) {
if ($userid != $followerid) {
if (!DB::query('SELECT follower_id FROM followers WHERE user_id=:userid AND follower_id=:followerid', array(':userid'=>$userid, ':followerid'=>$followerid))) {
if ($followerid == 6) {
DB::query('UPDATE users SET verified=1 WHERE id=:userid', array(':userid'=>$userid));
}
DB::query('INSERT INTO followers VALUES (:id, :userid, :followerid)', array(':id'=>0, ':userid'=>$userid, ':followerid'=>$followerid));
} else {
echo 'Already following!';
}
$isFollowing = True;
}
}
if (isset($_POST['unfollow'])) {
if ($userid != $followerid) {
if (DB::query('SELECT follower_id FROM followers WHERE user_id=:userid AND follower_id=:followerid', array(':userid'=>$userid, ':followerid'=>$followerid))) {
if ($followerid == 6) {
DB::query('UPDATE users SET verified=0 WHERE id=:userid', array(':userid'=>$userid));
}
DB::query('DELETE FROM followers WHERE user_id=:userid AND follower_id=:followerid', array(':userid'=>$userid, ':followerid'=>$followerid));
}
$isFollowing = False;
}
}
if (DB::query('SELECT follower_id FROM followers WHERE user_id=:userid AND follower_id=:followerid', array(':userid'=>$userid, ':followerid'=>$followerid))) {
//echo 'Already following!';
$isFollowing = True;
}
if (isset($_POST['deletepost'])) {
if (DB::query('SELECT id FROM posts WHERE id=:postid AND user_id=:userid', array(':postid'=>$_GET['postid'], ':userid'=>$followerid))) {
DB::query('DELETE FROM posts WHERE id=:postid and user_id=:userid', array(':postid'=>$_GET['postid'], ':userid'=>$followerid));
DB::query('DELETE FROM post_likes WHERE post_id=:postid', array(':postid'=>$_GET['postid']));
echo 'Post deleted!';
}
}
if (isset($_POST['post'])) {
if ($_FILES['postimg']['size'] == 0) {
Post::createPost($_POST['postbody'], Login::isLoggedIn(), $userid);
} else {
$postid = Post::createImgPost($_POST['postbody'], Login::isLoggedIn(), $userid);
Image::uploadImage('postimg', "UPDATE posts SET postimg=:postimg WHERE id=:postid", array(':postid'=>$postid));
}
}
if (isset($_GET['postid']) && !isset($_POST['deletepost'])) {
Post::likePost($_GET['postid'], $followerid);
}
$posts = Post::displayPosts($userid, $username, $followerid);
} else {
die('User not found!');
}
}
?>
<h1><?php echo $username; ?>'s Profile<?php if ($verified) { echo -
Verified';` } ?></h1>
<form action="profile.php?username=<?php echo $username; ?>"
method="post">
<?php
if ($userid != $followerid) {
if ($isFollowing) {
echo '<input type="submit" name="unfollow"
value="Unfollow">';
} else {
echo '<input type="submit" name="follow" value="Follow">';
}
}
?>
</form>

<form action="profile.php?username=<?php echo $username; ?>" method="post"
enctype="multipart/form-data">
<textarea name="postbody" rows="8" cols="80"></textarea>
<br />Upload an image:
<input type="file" name="postimg">
<input type="submit" name="post" value="Post">
</form>

<div class="posts">
<?php echo $posts; ?>
</div>

几乎每个功能都可以正常工作,只是这里有一些问题数据库也应该配置正确

这个查询可能有问题,但我不知道在哪里:

                        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));

谢谢你的回答,你是对的,但这不是问题,我不知道如何设置“\'\'”的值,因为我尝试了很多东西,但没有任何效果,但最后我解决了它,我必须设置值和创建新变量...我感觉很完美:DDD

关于php - fatal error : Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43463366/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com