-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertbook.php
More file actions
63 lines (62 loc) · 1.91 KB
/
insertbook.php
File metadata and controls
63 lines (62 loc) · 1.91 KB
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
59
60
61
62
63
<?php
session_start();
if(!(isset($_SESSION['username']) && $_SESSION['username'] != ""))
{
header("location:logout.php");
} else {
$user=$_SESSION['username'];
}?>
<?php
$con = mysqli_connect('localhost', 'root', 'root');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysqli_select_db($con, "library");
echo mysqli_error($con);
?>
<?php
$bookid = $_POST['bookid'];
$bookname = $_POST['bookname'];
$date = $_POST['date'];
$quantity = $_POST['quantity'];
$status = $_POST['status'];
$bookdesc = $_POST['bookdesc'];
$author = $_POST['author'];
$price = $_POST['price'];
$sql = "select * from books where bookid='$bookid'";
$result = mysqli_query($con, $sql);
$result=mysqli_fetch_assoc($result);
$rows = mysqli_num_rows($result);
if ($rows > 0) {
$available=0;
$available += $result['available'];
$available += $quantity;
$quantity += $result['quantity'];
$sql = "UPDATE books set quantity='$quantity', available='$available' WHERE bookid='$bookid'";
$result = mysqli_query($con, $sql);
if ($result) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($con);
}
} else {
// strip tags to avoid breaking any html
$string = strip_tags($bookdesc);
if (strlen($bookdesc) >= 100) {
// truncate string
$bookdesc = substr($bookdesc, 0, 100);
$bookdesc = $bookdesc."...";
}
$sql = "Insert into books VALUES ('$bookid','$bookname','$bookdesc','$author','$date','$status','$price','$quantity', '$quantity')";
$result = mysqli_query($con,$sql);
if ($result) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($con);
}
}
?>
<p>Back To <a href="admin.php">Admin Centre</a>
</p>
<p><a href="logout.php">Logout</a></p>