SQL Server Stop inserting after reach the limit in PHP and SQL

chhkpiq4  于 2023-08-02  发布在  PHP
关注(0)|答案(1)|浏览(121)

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";
    }
}
?>
kxe2p93d

kxe2p93d1#

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

}

相关问题