I have a page that upload files to my database, im trying to record the day when the file is uploaded so i can check and view it to my table, but everytime i upload a file, it doesnt record any dates
<?php
function upload_image()
{
if ( isset($_FILES["user_image"]) )
{
$date = date('M-D-Y');
$destination = './upload/' . $_FILES['user_image']['name'];
move_uploaded_file($_FILES['user_image']['tmp_name'], $destination);
return $_FILES['user_image']['name'];
}
}
function get_image_name($user_id)
{
include('db3.php');
$statement = $connection->prepare("SELECT image FROM users1 WHERE id = '$user_id'");
$statement->execute();
$result = $statement->fetchAll();
foreach($result as $row)
{
return $row["image"];
}
}
function get_total_all_records()
{
include('db3.php');
$statement = $connection->prepare("SELECT * FROM users1");
$statement->execute();
$result = $statement->fetchAll();
return $statement->rowCount();
}
?>
My Insert Query:
<?php
include('db3.php');
include('function.php');
if(isset($_POST["operation"]))
{
if($_POST["operation"] == "Add")
{
$image = '';
if($_FILES["user_image"]["name"] != '')
{
$image = upload_image();
}
$statement = $connection->prepare("
INSERT INTO users1 (memorandum, titlee, image, date)
VALUES (:memorandum, :titlee, :image, date)
");
$result = $statement->execute(
array(
':memorandum' => $_POST["memorandum"],
':date' => $_POST["date"],
':titlee' => $_POST["titlee"],
':image' => $image
)
);
if(!empty($result))
{
echo 'Data Inserted';
}
}
if($_POST["operation"] == "Edit")
{
$image = '';
if($_FILES["user_image"]["name"] != '')
{
$image = upload_image();
}
else
{
$image = $_POST["hidden_user_image"];
}
$statement = $connection->prepare(
"UPDATE users1
SET memorandum = :memorandum, titlee = :titlee, image = :image , date = :date ,
WHERE id = :id
"
);
$result = $statement->execute(
array(
':memorandum' => $_POST["memorandum"],
':titlee' => $_POST["titlee"],
':date' => $_POST["date"],
':image' => $image,
':id' => $_POST["user_id"]
)
);
if(!empty($result))
{
echo 'Data Updated';
}
}
}
?>
Related
I have the following code which up dates my database table perfectly. However, I now wish it to either, update an existing row if the value of $status is 'open', or create a new row if the value of $status is 'completed'.
This is my code so far;
<?php
if (isset($_POST['submit_update_activity'])); {
require 'dbh.inc.php';
$activity_id = $_POST['hidden_activity_id'];
$idFromKnowledgeBase = $_POST['hidden_idFromKnowledgeBase'];
$hiddenUserID = $_POST['hidden_userId'];
$title = $_POST['title'];
$description = $_POST['description'];
$assigned_to = $_POST['assigned_to'];
$category = $_POST['category'];
$cost = $_POST['cost'];
$next_due = $_POST['next_due'];
$due_every = $_POST['due_every'];
$frequency = $_POST['frequency'];
$supplier = $_POST['supplier'];
$status = $_POST['status'];
$comments = $_POST['comments'];
$emptyAssignedTo = $_POST['empty_assigned_to'];
$emptyStatus = $_POST['empty_status'];
$emptyCategory = $_POST['empty_category'];
$dateCompleted = $_POST['date_completed'];
$emptyFrequency = $_POST['empty_frequency'];
$emptyNextDue = $_POST['empty_next_due'];
$next_due = date('Y-m-d', strtotime($dateCompleted. " + {$due_every} $frequency"));
if (empty($frequency)) {
$frequency = $emptyFrequency;
}
if (empty($status)) {
$status = $emptyStatus;
}
if (empty($assigned_to)) {
$assigned_to = $emptyAssignedTo;
}
if (empty($category)) {
$category = $emptyCategory;
}
//This line isn't working
if ($status == 'open') {
$next_due = $emptyNextDue;
} else {
$next_due = date('Y-m-d', strtotime($dateCompleted. " + {$due_every} $frequency"));
}
$stmt = $conn->prepare("UPDATE activities SET idFromKnowledgeBase = ?, userId = ?, title = ?, description = ?, assigned_to = ?, category = ?, cost = ?, last_completed = ?,next_due = ?, frequency = ?, supplier = ?, status = ?, comments = ? WHERE id = ?");
$stmt->bind_param("ssssssssssssss", $idFromKnowledgeBase, $hiddenUserID, $title, $description, $assigned_to, $category, $cost, $dateCompleted, $next_due, $frequency, $supplier, $status, $comments, $activity_id);
$stmt->execute();
if($stmt->affected_rows >0) {
header('Location: ../all_activities.php?updated');
}
}
?>
I've tried =, == and === for comparing $status. If I take it back a notch and echo either 'open' or 'completed' depending on the value of $status, it works fine, echoing the correct answer each time.
Advice is appreciated.
You are missing the INSERT statement
<?php
if (isset($_POST['submit_update_activity'])); {
require 'dbh.inc.php';
$activity_id = $_POST['hidden_activity_id'];
$idFromKnowledgeBase = $_POST['hidden_idFromKnowledgeBase'];
$hiddenUserID = $_POST['hidden_userId'];
$title = $_POST['title'];
$description = $_POST['description'];
$assigned_to = $_POST['assigned_to'];
$category = $_POST['category'];
$cost = $_POST['cost'];
$next_due = $_POST['next_due'];
$due_every = $_POST['due_every'];
$frequency = $_POST['frequency'];
$supplier = $_POST['supplier'];
$status = $_POST['status'];
$comments = $_POST['comments'];
$emptyAssignedTo = $_POST['empty_assigned_to'];
$emptyStatus = $_POST['empty_status'];
$emptyCategory = $_POST['empty_category'];
$dateCompleted = $_POST['date_completed'];
$emptyFrequency = $_POST['empty_frequency'];
$emptyNextDue = $_POST['empty_next_due'];
$next_due = date('Y-m-d', strtotime($dateCompleted. " + {$due_every} $frequency"));
if (empty($frequency)) {
$frequency = $emptyFrequency;
}
if (empty($status)) {
$status = $emptyStatus;
}
if (empty($assigned_to)) {
$assigned_to = $emptyAssignedTo;
}
if (empty($category)) {
$category = $emptyCategory;
}
//This line isn't working
if ($status == 'open') {
$next_due = $emptyNextDue;
} else {
$next_due = date('Y-m-d', strtotime($dateCompleted. " + {$due_every} $frequency"));
}
if($status == 'completed')
{
$stmt = $conn->prepare('INSERT INTO activities(idFromKnowledgeBase, userId, title , description, assigned_to, category, cost, last_completed,next_due, frequency, supplier , status, comments) VALUES(:kb, :uid, :title, :descr, :assign, :cat, :cost, :last, :next, :freq, :sup, :stat, :com)');
$stmt->execute(array(
'kb' => $idFromKnowledgeBase,
'uid' => $hiddenUserID,
'title' => $title,
'descr' => $description,
'assign' => $assigned_to,
'cat' => $category,
'cost' => $cost,
'last' => $dateCompleted,
'next' => $next_due,
'freq' => $frequency,
'sup' => $supplier,
'stat' => $status,
'com' => $comments,
));
}
else
{
$stmt = $conn->prepare("UPDATE activities SET idFromKnowledgeBase = :kb, userId = :uid, title = :title, description = :descr, assigned_to = :assign, category = :cat, cost = :cost, last_completed = :last,next_due = :next, frequency = :freq, supplier = :sup, status = :stat, comments = :com WHERE id = :id");
$stmt->execute(array(
'kb' => $idFromKnowledgeBase,
'uid' => $hiddenUserID,
'title' => $title,
'descr' => $description,
'assign' => $assigned_to,
'cat' => $category,
'cost' => $cost,
'last' => $dateCompleted,
'next' => $next_due,
'freq' => $frequency,
'sup' => $supplier,
'stat' => $status,
'com' => $comments,
'id' => $activity_id
));
}
if($stmt->affected_rows >0) {
header('Location: ../all_activities.php?updated');
}
}
?>
I have this problem, I have to update a record of a table that has the values of a serialized column. the call to the function works and passes the data correctly. I can enter the record, but I can not update. This is my code:
public function update($user_id, $array_check)
{
$arrayB = array();
$array_check = unserialize($array_check);
foreach ($array_check $key => $value) {
if($value["id"] == $array_check){
$idRow = $value["id"];
if($value["value"] == "0"){
$valueRow = "1";
}else{
$valueRow = "0";
}
}else{
$idRow = $value["id"];
$valueRow = $value["value"];
}
$row = array("id" => $idRow, "value" => $valueRow);
$arrayB[] = $row;
}
$stmt = $this->_db->prepare('UPDATE data_docs SET docs_selected = :docs_selected WHERE user_id = :user_id');
$row = $stmt->execute(array(':user_id' => $user_id, ':docs_selected' => serialize($arrayB) ) );
return $arrayB;
}
edit.
Replace this:
$stmt = $this->_db->prepare('UPDATE data_docs SET docs_selected = :docs_selected WHERE user_id = :user_id);
with:
$deseralized = serialize($arrayB);
$stmt = $this->_db->prepare('UPDATE data_docs SET docs_selected = '$deseralized ' WHERE user_id = '$user_id');
please help guys i have code for inserting name and etc.. and for inserting a image i cant combine the codes
here is the ouput in phpmyadmin i only insert one value thou...
thanks for your help guys
<---!THIS CODE BELOW IT INSERT ID,NAME AND ETC --->
<?php
require 'db.php';
$message = '';
$Error = '';
if (isset ($_POST['Attendee_id']) &&
isset($_POST['RFID_number']) &&
isset($_POST['Attendee_Name']) &&
isset($_POST['CourseOrDepartment']) &&
isset ($_POST['Status']) ) {
$Attendee_id = $_POST['Attendee_id'];
$RFID_number = $_POST['RFID_number'];
$Attendee_Name = $_POST['Attendee_Name'];
$CourseOrDepartment = $_POST['CourseOrDepartment'];
$Status = $_POST['Status'];
$sql = 'INSERT INTO tbl_listofregister(Attendee_id,
RFID_number,Attendee_Name,CourseOrDepartment,Status)
VALUES(:Attendee_id,
:RFID_number,:Attendee_Name,:CourseOrDepartment,:Status)';
$statement = $connection->prepare($sql);
if ($statement->execute([':Attendee_id' => $Attendee_id, ':RFID_number' =>
$RFID_number,':Attendee_Name' => $Attendee_Name,':CourseOrDepartment' =>
$CourseOrDepartment,':Status' => $Status])) {
$message = 'DATA INSERTED SUCCESSFULLY';
}
else
{
$Error = "ID SHOULD BE UNIQUE";
}
}
?>
<---! HERE IS FOR IMAGE --->
<?php
$msg = '';
if($_SERVER['REQUEST_METHOD']=='POST'){
$image = $_FILES['Image']['tmp_name'];
$img = file_get_contents($image);
$con = mysqli_connect('localhost','root','','dbattendancelibrary') or
die('Unable To connect');
$sql = "insert into tbl_listofregister (image) values(?)";
$stmt = mysqli_prepare($con,$sql);
mysqli_stmt_bind_param($stmt, "s",$img);
mysqli_stmt_execute($stmt);
$check = mysqli_stmt_affected_rows($stmt);
if($check==1){
$msg = 'Image Successfullly UPloaded';
}else{
$msg = 'Error uploading image';
}
mysqli_close($con);
}
?>
This would combine the two inserts into one action. But I would advise against storing the images in the db. Store the path relative to your site of the image instead.
<?php
require 'db.php';
$message = '';
$Error = '';
if(isset($_POST['Attendee_id']) &&
isset($_POST['RFID_number']) &&
isset($_POST['Attendee_Name']) &&
isset($_POST['CourseOrDepartment']) &&
isset($_POST['Status']) &&
isset($_FILES['Image']['tmp_name']) &&
$_SERVER['REQUEST_METHOD']=='POST') {
$Attendee_id = $_POST['Attendee_id'];
$RFID_number = $_POST['RFID_number'];
$Attendee_Name = $_POST['Attendee_Name'];
$CourseOrDepartment = $_POST['CourseOrDepartment'];
$Status = $_POST['Status'];
$image = $_FILES['Image']['tmp_name'];
$img = file_get_contents($image);
$sql = 'INSERT INTO tbl_listofregister(
Attendee_id,
RFID_number,
Attendee_Name,
CourseOrDepartment,
Status,
image)
VALUES(:Attendee_id,
:RFID_number,
:Attendee_Name,
:CourseOrDepartment,
:Status,
:Image)';
$statement = $connection->prepare($sql);
if($statement->execute(
[':Attendee_id' => $Attendee_id,
':RFID_number' => $RFID_number,
':Attendee_Name' => $Attendee_Name,
':CourseOrDepartment' => $CourseOrDepartment,
':Status' => $Status,
':Image' => $img]
)) {
$message = 'DATA INSERTED SUCCESSFULLY';
} else {
$Error = "ID SHOULD BE UNIQUE";
}
}
?>
$Attendee_id = $_POST['Attendee_id'];
$RFID_number = $_POST['RFID_number'];
$Attendee_Name = $_POST['Attendee_Name'];
$CourseOrDepartment = $_POST['CourseOrDepartment'];
$Status = $_POST['Status'];
After these lines , you can add this line for an img
move_uploaded_file($_FILES['file']['tmp_name'], "filename/".$_FILES['file']['name']);
Then, add it in the query as ( $_FILES['file']['name'] ).
in HTML file write this line ( ).
I hope it's work well <3
I am trying to get data from ajax response as json. But I am getting error response. I had this problem once, and i manage to solve it by adding header('Content-type: application/json'); to my php file. But if I do it now it kind of break my code, because i am including the file to another file that has a lot of html code in it and it loads the code in response. Can i get json response without that header somehow ?
$('#catlist').change(function() {
var opt = $(this).val();
console.log(opt);
$.ajax({
url: 'includes/processproducts.php',
type: 'get',
dataType: "json",
data: {opt: opt},
success: function(options){
console.log(options);
},
error: function(options){
console.log(options);
console.log("not working");
}
});
$('#brandlist').attr('disabled', false);
});
<?php
if(!isset($_SESSION)){
session_start();
}
//
include '../../core/initialize.php';
/*
include 'includes/head.php';
include 'includes/navigation.php';
include 'includes/functions.php';
*/
$sql = "SELECT * FROM product";
$result = $db->query($sql);
if(isset($_REQUEST['submitbtn'])){
if(isset($_REQUEST['edit'])){
?>
<script> alert("EDIT") </script>
<?php
if(!empty($_POST['handleName'])){
$nameEdVar = $_POST['handleName'];
} else {
$nameEdVar = NULL;
}
if(!empty($_POST['handleCat'])){
$catEdVar = $_POST['handleCat'];
} else {
$catEdVar = NULL;
}
if(!empty($_POST['handleBrand'])){
$brandEdVar = $_POST['handleBrand'];
} else {
$brandEdVar = NULL;
}
if(!empty($_POST['handleDesc'])){
$descEdVar = $_POST['handleDesc'];
} else {
$descEdVar = NULL;
}
if(!($_FILES['image']['size'] == 0)){
$path = processImg($_REQUEST['edit']);
} else {
$path = NULL;
}
//$path = NULL;
$edit_id = (int)$_REQUEST['edit'];
$sqled = "UPDATE product SET name='$nameEdVar', cat_id='$catEdVar', brand_id='$brandEdVar', image='$path', description='$descEdVar' WHERE product.id_P = $edit_id";
$db->query($sqled);
//header('Location: products.php');
}else{
if(($_POST['handleName'] =='') || ($_POST['handleCat'] == '')
|| ($_POST['handleBrand'] == '') || ($_POST['handleDesc'] == '')
|| ($_FILES['image']['size'] == 0)){ ?>
<script> alert('ADD časť niečo nevyplnene'); </script>
<?php
}else{
?> <script> alert('ADD časť všetko vyplnene'); </script>
<?php
$nameVar = $_POST['handleName'];
$catVar = $_POST['handleCat'];
$brandVar = $_POST['handleBrand'];
$descVar = $_POST['handleDesc'];
$qVar = 1;
//$path = "https://localhost/shop/imgs/31.jpg";
$sql = "SELECT MAX(id_p) FROM product";
$stmt = $db->prepare($sql);
$stmt->execute();
$stmt->bind_result($maxid);
$stmt->fetch();
$path = processImg(++$maxid);
$stmt->close();
$sql = "INSERT INTO product (id_p, name, cat_id, brand_id, image, description, quantity) VALUES(?, ?, ?, ?, ?, ?, ?)";
$stmt = $db->prepare($sql);
$stmt->bind_param('sssssss', $nullvar = NULL, $nameVar, $catVar, $brandVar, $path, $descVar, $qVar);
$stmt->execute();
$stmt->close();
//header('Location: products.php');
}
}
}
//Delete Product
if(isset($_GET['delete']) && !empty($_GET['delete'])){
$delete_id = (int)$_GET['delete'];
$sqldel = "DELETE FROM product WHERE product.id_p = $delete_id";
$stmt = $db->prepare($sqldel);
$stmt->execute();
$stmt->close();
header('Location: products.php');
}
if(isset($_REQUEST['edit'])){
echo "
<script>
$(document).ready(function() {
$('#addBox').modal('show');
})
</script>";
}
$sqlcat = "SELECT * FROM category";
$stmtcat = $db->prepare($sqlcat);
$stmtcat->execute();
$stmtcat->bind_result($cat_id, $type);
$stmtcat->store_result();
$cat = array();
while($stmtcat->fetch()){
$cat[$cat_id] = array();
$cat[$cat_id]["type"] = $type;
}
$stmtcat->close();
$sqlbrand = "SELECT * FROM brand";
$stmtbrand = $db->prepare($sqlbrand);
$stmtbrand->execute();
$stmtbrand->bind_result($brand_id, $name);
$stmtbrand->store_result();
$brand = array();
while($stmtbrand->fetch()){
$brand[$brand_id] = array();
$brand[$brand_id]["name"] = $name;
}
$stmtbrand->close();
if(isset($_REQUEST['submitcategory'])){
$sql = "INSERT INTO category (cat_id, type) VALUES(?, ?) ON DUPLICATE KEY UPDATE cat_id = cat_id";
$stmt = $db->prepare($sql);
$stmt->bind_param('is', $cat_id = NULL, $_REQUEST['category']);
$stmt->execute();
$stmt->close();
foreach ($_REQUEST as $key=>$value){if($key != "category" && $value != "Submit"){
if($value == ""){
exit();
}
var_dump($value);
$sql = "INSERT INTO brand(brand_id, NAME) VALUES(?, ?) ON DUPLICATE KEY UPDATE brand_id = brand_id";
$stmt = $db->prepare($sql);
$stmt->bind_param('is', $brand_id = NULL, $value);
$stmt->execute();
$stmt->close();
$sql = "SELECT brand_id, cat_id FROM brand,category WHERE brand.name= ? AND category.type = ?";
$stmt = $db->prepare($sql);
$stmt->bind_param('ss', $value, $_REQUEST['category']);
$stmt->execute();
$stmt->bind_result($brand_id, $cat_id);
$stmt->fetch();
$stmt->close();
$sql = "INSERT INTO brand_category(brand_id, cat_id) VALUES(?, ?) ON DUPLICATE KEY UPDATE brand_id = brand_id";
$stmt = $db->prepare($sql);
$stmt->bind_param('ii', $brand_id, $cat_id );
$stmt->execute();
$stmt->close();
}
}
}elseif(isset($_REQUEST['submitbrand'])){
$sql = "INSERT INTO brand (brand_id, name) VALUES(?, ?) ON DUPLICATE KEY UPDATE brand_id = brand_id";
$stmt = $db->prepare($sql);
$stmt->bind_param('is', $brand_id = NULL, $_REQUEST['brand']);
$stmt->execute();
$stmt->close();
foreach ($_REQUEST as $key=>$value){if($key != "brand" && $value != "Submit"){
if($value == ""){
exit();
}
$sql = "INSERT INTO category (cat_id, type) VALUES(?, ?) ON DUPLICATE KEY UPDATE cat_id = cat_id";
$stmt = $db->prepare($sql);
$stmt->bind_param('is', $cat_id = NULL, $value);
$stmt->execute();
$stmt->close();
$sql = "SELECT brand_id, cat_id FROM brand,category WHERE brand.name= ? AND category.type = ?";
$stmt = $db->prepare($sql);
$stmt->bind_param('ss', $_REQUEST['brand'], $value);
$stmt->execute();
$stmt->bind_result($brand_id, $cat_id);
$stmt->fetch();
$stmt->close();
$sql = "INSERT INTO brand_category(brand_id, cat_id) VALUES(?, ?) ON DUPLICATE KEY UPDATE brand_id = brand_id";
$stmt = $db->prepare($sql);
$stmt->bind_param('ii', $brand_id, $cat_id );
$stmt->execute();
$stmt->close();
}
}
}
$sql = "SELECT brand.name, brand.brand_id FROM brand JOIN brand_category ON brand_category.brand_id = brand.brand_id WHERE brand_category.cat_id = ?";
$stmt = $db->prepare($sql);
$stmt->bind_param('s', $_REQUEST['opt']);
$stmt->execute();
$stmt->bind_result($name, $brand_id);
$stmt->store_result();
$options = array();
echo $name;
echo $brand_id;
while($stmt->fetch()){
$options[$brand_id] = array();
$options[$brand_id]["name"] = $name;
}
//header('Content-type: application/json');
echo json_encode($options);
$stmt->close();
?>
Remove the following line from your ajax call:
dataType: "json"
and change the line
success: function(options){
var data = JSON.parse(options); // convert the json to js array and use it
}
Try this, it will work
Solved it by creating file just for ajax call.
I would like to get the newest comments in following code, but now only showing the oldest 50 comments, how can I edit code to showing the newest 50 comments? thanks so much
Code here:
<?php
class comments extends db_connect
{
private $requestFrom = 0;
private $language = 'en';
public function __construct($dbo = NULL)
{
parent::__construct($dbo);
}
public function allCommentsCount()
{
$stmt = $this->db->prepare("SELECT max(id) FROM comments");
$stmt->execute();
return $number_of_rows = $stmt->fetchColumn();
}
public function count($postId)
{
$stmt = $this->db->prepare("SELECT count(*) FROM comments WHERE postId = (:postId) AND removeAt = 0");
$stmt->bindParam(":postId", $postId, PDO::PARAM_INT);
$stmt->execute();
return $number_of_rows = $stmt->fetchColumn();
}
public function create($postId, $text, $notifyId = 0)
{
$result = array("error" => true,
"error_code" => ERROR_UNKNOWN);
if (strlen($text) == 0) {
return $result;
}
$post = new post($this->db);
$postInfo = $post->info($postId);
unset($post);
$currentTime = time();
$ip_addr = helper::ip_addr();
$u_agent = helper::u_agent();
$stmt = $this->db->prepare("INSERT INTO comments (fromUserId, postId, comment, createAt, notifyId, ip_addr, u_agent) value (:fromUserId, :postId, :comment, :createAt, :notifyId, :ip_addr, :u_agent)");
$stmt->bindParam(":fromUserId", $this->requestFrom, PDO::PARAM_INT);
$stmt->bindParam(":postId", $postId, PDO::PARAM_INT);
$stmt->bindParam(":comment", $text, PDO::PARAM_STR);
$stmt->bindParam(":createAt", $currentTime, PDO::PARAM_INT);
$stmt->bindParam(":notifyId", $notifyId, PDO::PARAM_INT);
$stmt->bindParam(":ip_addr", $ip_addr, PDO::PARAM_STR);
$stmt->bindParam(":u_agent", $u_agent, PDO::PARAM_STR);
if ($stmt->execute()) {
$result = array("error" => false,
"error_code" => ERROR_SUCCESS,
"commentId" => $this->db->lastInsertId(),
"comment" => $this->info($this->db->lastInsertId()));
if ($this->requestFrom != $postInfo['fromUserId']) {
$gcm = new gcm($this->db, $postInfo['fromUserId']);
$gcm->setData(GCM_NOTIFY_COMMENT, "You have a new comment.", $postId);
$gcm->send();
}
}
return $result;
}
public function remove($commentId)
{
$result = array("error" => true,
"error_code" => ERROR_UNKNOWN);
$commentInfo = $this->info($commentId);
if ($commentInfo['error'] === true) {
return $result;
}
// if ($commentInfo['fromUserId'] != $this->requestFrom) {
//
// return $result;
// }
$currentTime = time();
$stmt = $this->db->prepare("UPDATE comments SET removeAt = (:removeAt) WHERE id = (:commentId)");
$stmt->bindParam(":commentId", $commentId, PDO::PARAM_INT);
$stmt->bindParam(":removeAt", $currentTime, PDO::PARAM_INT);
if ($stmt->execute()) {
$result = array("error" => false,
"error_code" => ERROR_SUCCESS);
}
return $result;
}
public function removeAll($postId) {
$currentTime = time();
$stmt = $this->db->prepare("UPDATE comments SET removeAt = (:removeAt) WHERE postId = (:postId)");
$stmt->bindParam(":postId", $postId, PDO::PARAM_INT);
$stmt->bindParam(":removeAt", $currentTime, PDO::PARAM_INT);
}
public function info($commentId)
{
$result = array("error" => true,
"error_code" => ERROR_UNKNOWN);
$stmt = $this->db->prepare("SELECT * FROM comments WHERE id = (:commentId) LIMIT 1");
$stmt->bindParam(":commentId", $commentId, PDO::PARAM_INT);
if ($stmt->execute()) {
if ($stmt->rowCount() > 0) {
$row = $stmt->fetch();
$time = new language($this->db, $this->language);
$profile = new profile($this->db, $row['fromUserId']);
$fromUserId = $profile->get();
unset($profile);
$lowPhotoUrl = "/img/profile_default_photo.png";
if (strlen($fromUserId['lowPhotoUrl']) != 0) {
$lowPhotoUrl = $fromUserId['lowPhotoUrl'];
}
$post = new post($this->db);
$post->setRequestFrom($this->getRequestFrom());
$postInfo = $post->info($row['postId']);
$result = array("error" => false,
"error_code" => ERROR_SUCCESS,
"id" => $row['id'],
"fromUserId" => $row['fromUserId'],
"fromUserState" => $fromUserId['state'],
"fromUserUsername" => $fromUserId['username'],
"fromUserFullname" => $fromUserId['fullname'],
"fromUserPhotoUrl" => $lowPhotoUrl,
"postId" => $row['postId'],
"postFromUserId" => $postInfo['fromUserId'],
"comment" => htmlspecialchars_decode(stripslashes($row['comment'])),
"createAt" => $row['createAt'],
"notifyId" => $row['notifyId'],
"timeAgo" => $time->timeAgo($row['createAt']));
}
}
return $result;
}
public function get($postId, $commentId = 0)
{
if ($commentId == 0) {
$commentId = $this->allCommentsCount() + 1;
}
$comments = array("error" => false,
"error_code" => ERROR_SUCCESS,
"commentId" => $commentId,
"postId" => $postId,
"comments" => array());
$stmt = $this->db->prepare("SELECT id FROM comments WHERE postId = (:postId) AND id < (:commentId) AND removeAt = 0 ORDER BY id ASC LIMIT 0,38");
$stmt->bindParam(':postId', $postId, PDO::PARAM_INT);
$stmt->bindParam(':commentId', $commentId, PDO::PARAM_INT);
if ($stmt->execute()) {
while ($row = $stmt->fetch()) {
$commentInfo = $this->info($row['id']);
array_push($comments['comments'], $commentInfo);
$comments['commentId'] = $commentInfo['id'];
unset($commentInfo);
}
}
return $comments;
}
public function getPreview($postId)
{
$commentId = $this->allCommentsCount() + 1;
$comments = array("error" => false,
"error_code" => ERROR_SUCCESS,
"commentId" => $commentId,
"postId" => $postId,
"count" => $this->count($postId),
"comments" => array());
$stmt = $this->db->prepare("SELECT id FROM comments WHERE postId = (:postId) AND id < (:commentId) AND removeAt = 0 ORDER BY id ASC LIMIT 3");
$stmt->bindParam(':postId', $postId, PDO::PARAM_INT);
$stmt->bindParam(':commentId', $commentId, PDO::PARAM_INT);
if ($stmt->execute()) {
while ($row = $stmt->fetch()) {
$commentInfo = $this->info($row['id']);
array_push($comments['comments'], $commentInfo);
$comments['commentId'] = $commentInfo['id'];
unset($commentInfo);
}
}
return $comments;
}
public function setLanguage($language)
{
$this->language = $language;
}
public function getLanguage()
{
return $this->language;
}
public function setRequestFrom($requestFrom)
{
$this->requestFrom = $requestFrom;
}
public function getRequestFrom()
{
return $this->requestFrom;
}
}
Try to edit your LIMIT from LIMIT 0,38 to LIMIT 38.