-
Notifications
You must be signed in to change notification settings - Fork 0
/
postprocess.php
58 lines (47 loc) · 1.54 KB
/
postprocess.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
session_start();
$host = 'sql207.move.pk';
$name = 'mov_25165757';
$pass = '496tayyab';
$dbname = 'mov_25165757_shughal';
//saves current loged in user to session storage
$cUser = $_REQUEST['user'];
// $userSearch = "'$cUser'";
//process for new posts
if($_REQUEST["ins"] !== "loadposts"){
//connection with database
$dsn = 'mysql:host='.$host.';dbname='.$dbname;
$pdo = new PDO($dsn,$name,$pass);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_OBJ);
//validation of current User
$stmt = $pdo->prepare('SELECT * FROM users WHERE username LIKE :cUser');
$stmt->execute(['cUser'=>$cUser]);
$res = $stmt->fetch(PDO::FETCH_ASSOC);
if(count($res) > 0){
if(strtolower($res['username']) == $cUser){
$userID = $res['id'];
//send post to server
$stmt = $pdo->prepare('INSERT INTO posts(id,body) VALUES(:userID,:pbody)');
$stmt->execute(['userID'=>$userID,'pbody'=> $_REQUEST['pbody']]);
$res = $stmt->fetch();
echo "ok";
}
}
}
//load published posts from database
if($_REQUEST['ins'] == "loadposts"){
//connection with database
$dsn = 'mysql:host='.$host.';dbname='.$dbname;
$pdo = new PDO($dsn,$name,$pass);
//validation of current User
$stmt = $pdo->prepare('SELECT * FROM posts');
$stmt->execute();
// $res = $stmt->fetchAll(PDO::FETCH_ASSOC);
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$tst = $pdo->prepare('SELECT username FROM users WHERE id = :userID');
$tst->execute(['userID'=>$row['id']]);
$myres = $tst->fetch();
echo $myres['username'].":".$row['body'] . ',';
}
}
?>