2017-06-12 42 views
0

我想上傳得到數據記錄在用戶同樣喜歡的FacebookTwitter的。這裏我的代碼正在做的是,它檢索登錄後由不同​​用戶發佈的所有圖像。請幫助我。我想要的是,如果用戶登錄,配置文件頁面應該只獲取登錄用戶的數據,而不是所有的用戶數據以及如何在mysql中爲用戶插入圖像值。如何上傳圖像到登錄的用戶,並得到用戶的圖像PHP MYSQL

這裏是我的SQL tabeles用戶和帖子

CREATE TABLE `user` (
    `id` int(11) NOT NULL, 
    `first_name` varchar(50) NOT NULL, 
    `username` varchar(50) NOT NULL, 
    `email` varchar(100) NOT NULL, 
    `password` varchar(100) NOT NULL, 
    `hash` varchar(32) NOT NULL, 
    `active` tinyint(1) NOT NULL DEFAULT '0', 
    `image` varchar(200) NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=COMPACT; 

帖子SQL

CREATE TABLE `posts` (
    `id` int(11) NOT NULL, 
    `userid` int(11) NOT NULL, 
    `image` varchar(255) NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=COMPACT; 

這裏是PHP代碼中Profile.php

<?php 
    session_start(); 
    include 'ssql.php'; 
    require 'db.php'; 

    // Check if user is logged in using the session variable 
    if ($_SESSION['logged_in'] != 1) { 
     $_SESSION['message'] = "You must log in before viewing your profile page!"; 
     header("location: error.php");  
    } 
    else { 
     // Makes it easier to read 
     $first_name = $_SESSION['first_name']; 
     $username = $_SESSION['username']; 
     $email = $_SESSION['email']; 
     $active = $_SESSION['active']; 

    } 
    ?> 
<!DOCTYPE html> 
<html> 
<head> 

<meta name="viewport" width="device-width"> 

<meta name="viewport" content="width=device-width, initial-scale=1.0"> 

<link rel="stylesheet" type="text/css" href="fcb.css" /> 
<link rel="stylesheet" type="text/css" href="css/font-awesome.css" /> 

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> 
<script type="text/javascript"> 
$(document).ready(function(){ 
var flag = 0; 
$.ajax({ 
type: "GET", 
url: "get_data.php", 
data: { 
'offset':0, 
'limit' :10 
}, 
success: function(data){ 
$('#post1').html(data); 


flag += 10 
} 

}); 

$(window).scroll(function() { 

if($(window).scrollTop() >= $(document).height() - $(window).height()) {  
$.ajax({ 
type: "GET", 
url: "get_data.php", 
data: { 
'offset': flag, 
'limit' : 10 
}, 
success: function (data) { 
$('#post1').append(data); 

flag += 10; 
} 

}); 

} 
}); 
}); 
</script>  
<body> 
<div id="post1"> 
</div> 
</body> 

這裏是數據庫db.php中

<?php 
/* Database connection settings */ 
$host = 'localhost'; 
$user = 'root'; 
$pass = ''; 
$db = 'accounts'; 
$mysqli = new mysqli($host,$user,$pass,$db) or die($mysqli->error); 

下面是帖子上傳ssql.php

<?php 

if (isset($_POST['update'])){ 


header('Location:profile.php'); 

$target = "images/".basename($_FILES['image']['name']); 
$db = mysqli_connect("localhost", "root", "", "accounts"); 
$image = $_FILES['image']['name']; 
$data = mysqli_query($db, "SELECT `id` FROM `user`"); 
$sql = "insert into posts (userid, image) values ('$userid', '$image')"; 
mysqli_query($db, $sql); 

if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) { 

} else { 

} 

} 



?> 

下面是get_data.php檢索圖像的代碼

<?php 

session_start(); 
include_once("db.php"); 
include_once("ssql.php"); 

if(isset($_GET['offset']) && isset($_GET['limit'])){ 

$limit = $_GET['limit']; 
$offset = $_GET['offset']; 
$db = mysqli_connect('localhost', 'root', '', 'accounts'); 
$data = mysqli_query($db, "SELECT `image` FROM `posts` ORDER BY `id` DESC LIMIT {$limit} OFFSET {$offset} "); 
while($row = mysqli_fetch_array($data)) { 
    echo "<div id='post1'>"; 
    echo "<img src='images/".$row['image']."' >"; 
    echo "</div>"; 
} 
} 
?> 

請幫幫我。我想要的是,如果用戶登錄,配置文件頁面應該只獲取登錄用戶的數據,而不是所有的用戶數據以及如何在mysql中爲用戶插入圖像值。

回答

0

您需要添加條件的用戶ID的查詢,例如:

$data = mysqli_query($db, "SELECT `image` FROM `posts` WHERE userid={$loggedInUserId} ORDER BY `id` DESC LIMIT {$limit} OFFSET {$offset} "); 
+0

如何在MySQL –

+0

插入圖像值用戶'如果(isset($ _ POST [「更新」 ])){ \t $ target =「images /」。basename($ _ FILES ['image'] ['name']); \t if(move_uploaded_file($ _ FILES ['image'] ['tmp_name'],$ target)){ \t \t $ db = mysqli_connect(「localhost」,「root」,「」,「accounts」); \t \t $ image = $ _FILES ['image'] ['name']; \t \t $ userid = $ _SESSION ['user_id']; //使用你設置的用戶ID \t \t $ sql =「insert into posts(userid,image)values('$ userid','$ image')」; \t \t mysqli_query($ db,$ sql); \t \t header('Location:profile.php'); \t \t} }' –

+0

我得到相同的用戶帖子,如果我與任何用戶登錄,請幫我 –

相關問題