I have table named books, this table will store the data of each book . each book has 5 pages only and each page has different details which belong to the same book. the name of the book stored in a column named "jalad" and the pages stored in a column named "sanad"
I want PHP allows to me inserted a new book after totally completing the insertion of the first book which has five-page and in case I entered less than 5 pages then will stop me to insert a new book before completing the first one. Any idea, please. I used this code but it does not work perfectly. Please any help.
The code:
<?php
// connect to the database
// $serverName = "SALARY_SERVER\SALARY_SERVER";
//$database = "roatb";
$serverName = "LENOVO";
$database = "tt";
$connectionInfo = array( "Database"=>$database );
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn )
{
echo "Connection established.\n";
}
else
{
echo "Connection could not be established.\n";
die( print_r( sqlsrv_errors(), true));
}
$jalad1 = $_POST['jalad'];
$sanad2 = $_POST['sanad'];
$sql = "SELECT count(Sanad) as countnumber FROM books where Jalad='$jalad1' ";
$stmt = sqlsrv_query( $conn, $sql );
while($row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)) {
$rowc= $row['countnumber'];
echo $rowc;
if ($rowc <=5) {
$sql = "INSERT INTO books (Jalad,Sanad)
VALUES ('$jalad1','$sanad2' )";
// echo $sql;
if (sqlsrv_query($conn, $sql)) {
echo "your data saved";
}
else {echo "error";}
}
else {
echo"you have to complete the page of the current book";
}
}
?>
CodePudding user response:
You need to select 'jalad' of the last uploaded book. you can do that however you want, but let's say you store the column value in a variable named $jalad_last
you just need to make your if statement like this
if ($rowc == 0 || $rowc < 5 && $jalad1 == $jalad_last) {
YOUR INSERT CODE HERE
} elseif ($rowc == 5) {
echo 'this book is already completed';
} else {
echo 'you have to complete...';
}
Good luck