大家好我试着解决这个问题好几天但没有结果你能帮我吗? 这是所有问题:
致命错误:未捕获PDOException:SQLSTATE [HY093]:参数号无效:绑定变量数与C:\ xampp \ htdocs \ dashboard \ sn \ classes \ DB.php中的标记数不匹配:12堆栈跟踪: #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(& #39;',' 1',' 1')#3 {main}在C:\ xampp \ htdocs \ dashboard \ sn \ classes \ DB.php中引发第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也应该正确配置
这个查询可能有问题,但是idk在哪里:
DB::query('INSERT INTO posts VALUES (\'\', :postbody, NOW(), :userid, 0, \'\', \'\')', array(':postbody'=>$postbody, ':userid'=>$profileUserId, ':topics'=>$topics));
谢谢你的时间和精力
答案 0 :(得分:0)
$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