- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
大家好,我尝试解决这个问题好几天了,但没有结果,你能帮助我吗?这就是所有问题:
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/
for /f "tokens=*" %%a in ('find /v ":" "%appdata%\gamelauncher\options.txt" ^| find "menu=a"') do (
我在 Javascript 中有一组全局计数器变量: var counter_0 = 0; var counter_1 = 0; var counter_2 = 0; 等等 然后我有一个 Javasc
好的,我正在阅读一些有关 RedBlackTrees 的代码。我注意到这一行“v1 = v2 = v3 = v4;”我理解类似“v1 += v2”(将 v2 添加到 v1 的当前值)和“v1 = v2
我正在为 C# 中的游戏数据加载制作一个 csv 阅读器,我想做的就是从数组(变量)的值声明一个变量,我们可以在 php 中像 $$foo 那样做。喜欢 void csvReader(string s
假设我有变量 内容为“ 123 ”和变量 b123 里面有一些文字。出于某种原因,我想使用变量 作为第二个 var 名称的一部分。像这样的东西: SET a=123 SET b123=some_tex
我对 javascript 有点陌生,我无法通过谷歌搜索找到任何内容,我正在编写一个程序,并且能够执行我所要求的操作: if (Variable == 1 或 Variable == 2 或 Vari
我发现我自己在做这种类型的 IF 语句分配。例如: if($variable == 1 || $variable == "whatever" || $variable == '492') { ...
我的虚拟 PC 在 MS-DOS 6.22 上运行时出现问题。 我需要使用变量 Date ,但我无法得到它,因为每当我尝试回显变量时,它都会显示 %variable%反而。 我在 Windows 控制
尝试运行此代码时: List list = em.createQuery("select balance b from Users where b.userName = '" + user_name.
我有一些代码,其中变量可以是 undefined、null 或正常值。无论变量是 undefined 还是 null,代码都需要做同样的事情。说有没有危险 for (var cur = this.bu
我正在编写一个批处理命令脚本,其中检查环境变量。我需要通过传递所有必需的变量来编写一个 FOR 循环,然后验证它是否已定义,如果未定义,则提示该键的值并永久设置该变量。 问题是我无法取消引用循环变量并
我知道这些是 Rails 的基础知识,但我仍然不知道 = 符号和 => 之间的全部区别以及 @some_variable 之间的区别、@@some_variable 和 :some_variable
我正在使用以下内容创建一个动态变量(PHP 术语中的“变量变量”): foo: "test1" set to-word (rejoin [foo "_result_data"]) array 5 但是
我一直在啃 PHP 套接字服务器和客户端的基础知识 here . 然后我偶然发现了这些行(摘自上面链接的第一个示例,发生在 while 中): if (false === ($buf = socket
这个问题在这里已经有了答案: What does "|=" mean? (pipe equal operator) (6 个答案) 关闭 9 年前。 我正在寻找一些编码来扩展我在 Java 方面的知
如何在 C++ 中从其他变量的值打印变量我只是 C++ 的新手。 在 php 中,我们可以通过其他变量的值来制作/打印一个变量。像这样。 $example = 'foo'; $foo = 'abc';
作为 Ruby on Rails 新手,我明白“@”和“:”引用有不同的含义。我看到了this post在 SO 中,其中描述了一些差异。 @ 表示实例变量(例如@my_selection) :表示别
编程新手/甚至更新。一个小的 go 程序有问题 - 不会编译带有 undefined variable 错误。代码: package main import ( "fmt" "io" "o
我知道其他一些语言,如PHP,支持“变量变量名”的概念--即,字符串的内容可以用作变量名的一部分。。我听说总的来说这不是一个好主意,但我认为它可以解决我在Python代码中遇到的一些问题。。有没有可能
我有两个版本的代码。 版本 1 Launcher.java class Launcher { public static void main(String[] args) {
我是一名优秀的程序员,十分优秀!