<?php

	$cmd = '';
	$p_barcode_last = '';
	//echo "Manage Patrons(new/edit/del)";
	$h_barcode =  $_POST['h_barcode'];
	$callno = $_POST['callno'];
	$title = $_POST['title'];
	$author = $_POST['author'];
	$status = $_POST['status'];
	$count = $_POST['count'];
	
	

	$conn = new mysqli('localhost', 'root', 'tester123','book_store');
	if ($conn->connect_error) {
		die("Connection failed: " . $conn->connect_error);
	}else{
		//echo "ok";
	}
	
	
	if($p_barcode == ""){		
		$sql = "select p_barcode from booklist";			
		$result = $conn->query($sql);		
		if ($result->num_rows > 0) {
			while($row = $result->fetch_assoc()) {
				$p_barcode_last = $row['p_barcode'];
			}		
		}
		//echo $p_barcode_last."<br/>";
			
		$p_barcode_last = substr($p_barcode_last,1);
		$p_barcode_last = (int)$p_barcode_last;	
		$p_barcode_new = $p_barcode_last+ 1;
		
		$p_barcode_new = "B".str_pad($p_barcode_new, 7, "0", STR_PAD_LEFT);
		//echo $p_barcode_new;
		$sql_manage = 	"INSERT INTO patrons (p_barcode, s_name, f_name,l_name)
				VALUES ('".$p_barcode_new."', '".$s_name."', '".$f_name."','".$l_name."')";	
	
		
		if ($conn->query($sql_manage) === TRUE) {
			echo "New record created successfully";
		} else {
			echo "Error: " . $sql_manage . "<br>" . $conn->error;
		}
	}else{
		
		$sql_update = 	"update patrons set
			
			callno= '".$title."',
			title='".$title."',
			author='".$author."'
			status='".$status."'
			count='".$count."'
			where barcode='".$h_barcode."'
		";	
	
		if ($conn->query($sql_update) === TRUE) {
			echo "Update successfully";
		} else {
			echo "Error: " . $sql_update . "<br>" . $conn->error;
		}
	}
	
	 header( "location: http://127.0.0.1/booking/patrons.php" );
	
	$conn->close();
?>