【发布时间】:2017-08-25 04:31:57
【问题描述】:
这是我的代码,起初我在 foreach 循环中有一个“postimg”(表单中的输入变量)的未定义索引通知,我调试了我的代码并通过检查是否设置了 postimg 来摆脱通知,但是现在我的帖子和图片不会显示,我相信问题仍然在于在 for each 循环中使用 postimg,我只是不知道它到底是什么。
<?php
include("connect.php");
include("check.php");
include("image.php");
$posts = "";
//$postimg = "";
if (isset($_POST['post'])) {
// $time = connect::query('SELECT posted_at FROM dry_posts WHERE id=:postid', array(':postid'=>$_GET['id']));
//$postimg = $_POST['postimg'];
//if(isset($_POST['postimg']))
//{
// $id = connect::query('SELECT id FROM dry_posts WHERE id=:id', array(':id'=>$_GET['id']));
if ($_FILES['postimg']['size'] == 0)
{
$postbody = $_POST['postbody'];
$loggedInUserId = check::isLoggedIn();
if (strlen($postbody) > 160 || strlen($postbody) < 1)
{
die('Incorrect length!');
}
connect::query('INSERT INTO dry_posts VALUES (null, :postbody, NOW(), 0)', array(':postbody'=>$postbody));
}
else
{
//$postid = Post::createImgPost($_POST['postbody']);
if (strlen($postbody) > 160) {
die('Incorrect length!');
}
connect::query('INSERT INTO dry_posts VALUES (null, :postbody, NOW(), 0)', array(':postbody'=>$postbody));
$postid = connect::query('SELECT id FROM dry_posts ORDER BY ID DESC LIMIT 1');
Image::uploadImage('postimg', "UPDATE dry_posts SET postimg=:postimg WHERE id=:postid", array(':postid'=>$postid));
}
// }
}
if (isset($_GET['postid']))
{
//Post::likePost($_GET['postid']);
if (!connect::query('SELECT post_id FROM post_likes WHERE post_id=:postid', array(':postid'=>$_GET['postid'])))
{
connect::query('UPDATE dry_posts SET likes=likes+1 WHERE id=:postid', array(':postid'=>$_GET['postid']));
connect::query('INSERT INTO post_likes VALUES (null, :postid)', array(':postid'=>$_GET['postid']));
}
else
{
connect::query('UPDATE dry_posts SET likes=likes-1 WHERE id=:postid', array(':postid'=>$_GET['postid']));
connect::query('DELETE FROM post_likes WHERE post_id=:postid', array(':postid'=>$_GET['postid']));
}
}
// $posts = Post::displayPosts();
$dbposts = connect::query('SELECT * FROM dry_posts ORDER BY id DESC');
$posts = "";
if(isset($_POST['postimg'])){
foreach($dbposts as $p) {
if (!connect::query('SELECT post_id FROM post_likes WHERE post_id=:postid', array(':postid'=>$p['id']))) {
$posts .="<img src='".$p['postimg']."'>".htmlspecialchars($p['body'])."
<form action='dry.php?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']."'>".htmlspecialchars($p['body'])."
<form action='dry.php?postid=".$p['id']."' method='post'>
<input type='submit' name='unlike' value='Unlike'>
<span>".$p['likes']." likes</span>
</form>
<hr /></br />
";
}
}
}
?>
这是头文件,“image.php”,我使用imgur上传图片,我相信我这里没有任何错误,有什么建议吗?
<?php
class Image
{
public static function uploadImage($query,$params)
{
$image = base64_encode(file_get_contents($_FILES[$formname]['tmp_name']));
$options = array('http'=>array(
'method'=>"POST",
'header'=>"Authorization: Bearer 813d9b0ee1b108a3383f6bd016dd9260873fa681\n".
"Content-Type: application/x-www-form-urlencoded",
'content'=>$image
));
$context = stream_context_create($options);
$imgurURL = "https://api.imgur.com/3/image";
if ($_FILES['profileimg']['size'] > 10240000) {
die('Image too big, must be 10MB or less!');
}
$response = file_get_contents($imgurURL, false, $context);
$response = json_decode($response);
$preparams = array($formname=>$response->$data->$link);
$params = $preparams + $params;
connect::query($query,$params);
}
}
?>
【问题讨论】:
-
查看:如何创建minimal reproducible example
标签: php forms image post imgur