Here’s my code, at first I had an undefined index notice for “postimg” (the input variable in the form) in the foreach loop, I debugged my code and got rid of the notice by checking if postimg was set, however now my posts and images won’t show up, I believe the problem still lies with the use of postimg in the for each loop, I just don’t know what exactly it is.
[php]<?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 />
";
}
}
}
?>
Here’s the header file,“image.php”, where I upload images using imgur, I don’t believe I have any errors here, any suggestions?
<?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); } } ?>[/php]