<?php
	include 'db_conn_class2.php';
	$name = str_replace("'"," ",$_POST['name']);
	$email = str_replace("'"," ",$_POST['email']);
	$phone = str_replace("'"," ",$_POST['phone']);
	$comment = str_replace("'","’",$_POST['comment']);
	$blogid = $_POST['blogid'];
	$comment_id = $_POST['comment_id'];
	echo "comment_id =".$comment_id;
	//return;
	//echo $blogid;
	//return;
	//$name ='test';
//	$email = '123@gmail.com';
//	$phone = '12323';
//	$comment = 'TEST';
//	$blogid = '112';
	date_default_timezone_set("Asia/Kolkata");
	$date = date("Y-m-d");
	$time = date("H:i:s");
	
	//to generate maximum comment_id FROM tbl_comment_det
	$sql_max_commentid = "select max(comment_id) from tbl_comment_det";
	$result_commentid = mysqli_query($conn,$sql_max_commentid);
	//echo $sql_max_commentid;
	
	if($result_commentid)
	{
		$row = mysqli_fetch_row($result_commentid);
		$commentid = $row[0] + 1;
		//echo "<br>".$commentid;
	}
	
	$sql_id = "select login_id from tbl_login where login_user = '".$email."'";
	//echo $sql_id;
		
	$result_id = mysqli_query($conn,$sql_id);
	$rowcount = mysqli_num_rows($result_id);
	
		if($rowcount>0)
		{
			$row = mysqli_fetch_row($result_id);
			$id = $row[0];
			//echo "<br>".$id;
			
			$sql_comment = "INSERT INTO tbl_comment_det (comment_id, reference_comment_id,blog_id,comment,file_path,image_path,comment_dt,login_user_id,read_status,status,deleted,like_comm,share_comm,modified_dt,posting_time)  
			VALUES (".$commentid.",".$comment_id.",".$blogid.",'".$comment."','','','".$date."',".$id.",'','ACTIVE',0,0,0,'".$date."','".$time."')";		
			echo $sql_comment;
			
			$result_comment = mysqli_query($conn,$sql_comment);
		}
		
		else
		{		
			//to generate maximum id in tbl_login
			$sql_max_id = "select max(login_id) from tbl_login";
			$result_max_id = mysqli_query($conn,$sql_max_id);
			//echo "<br>".$sql_max_id;
			
			if($result_max_id)
			{
				$row = mysqli_fetch_row($result_max_id);
				$id = $row[0] + 1;
				//echo $id;
				
			}
			
			$sql = "INSERT INTO tbl_login (login_id,login_user, password,first_name,contact_number,status,deleted,login_user_type) 
			VALUES (".$id.",'".$email."','act123','".$name."','".$phone."', 'ACTIVE', 0,'user')";
			
			$result_sql = mysqli_query($conn,$sql);
			if($result_sql){
			//echo "<br>".$sql;
			//return;
			
			$sql_comment = "INSERT INTO tbl_comment_det (comment_id , reference_comment_id,blog_id,comment,file_path,image_path,comment_dt,login_user_id,read_status,status,deleted,like_comm,share_comm,modified_dt,posting_time) 
					VALUES (".$commentid.",".$comment_id.",".$blogid.",'".$comment."','','','".$date."',".$id.",'','ACTIVE',0,0,0,'".$date."','".$time."')";	
			echo "<br>".$sql_comment;
							
				if (mysqli_query($conn, $sql_comment)) {
					echo "Form Submitted Succesfully";
				} 
				else {
					echo "Error";
				}
			}
			else {
					echo "Error";
				 }
		}
		
	mysqli_close($conn);
	
?>