-2

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.

table here :

The code:

<?php
// connect to the database
// $serverName = "";   
//$database = "";  
$serverName = "";   
$database = "";  
$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));  
}

$x= $_POST['x'];
$y= $_POST['y'];

$sql = "SELECT count(x) as countnumber FROM books where x='$x' ";
    
$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 (x,x) 
        VALUES ('$x','$x' )";
        //  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";
    }
}
?>
noor ail
  • 1
  • 3
  • As per the question guide, please do not post images of code, data, error messages, etc. - copy or type the text into the question. Please reserve the use of images for diagrams or demonstrating rendering bugs, things that are impossible to describe accurately via text. – Dale K Mar 19 '22 at 08:50
  • 2
    Please read: [How can I prevent SQL injection in PHP?](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php/60496#60496) – Luuk Mar 19 '22 at 08:55

1 Answers1

0

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...';

}
ruleboy21
  • 5,510
  • 4
  • 17
  • 34
Zaid Naim
  • 16
  • 2