达到 PHP 和 SQL 的限制后停止插入

Stop inserting after reach the limit in PHP and SQL

我有table本命名的书,这个table会存储每本书的数据。每本书只有 5 页,每一页都有属于同一本书的不同详细信息。书名存储在名为“jalad”的列中,页面存储在名为“sanad”的列中

我想要 PHP 允许我在完全完成第一本有五页的书的插入后插入一本新书,如果我输入的书少于 5 页,那么将阻止我插入新书在完成第一个之前预订。任何想法,请。我使用了这段代码,但效果不佳。请任何帮助。

table here :

代码:

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

您需要 select 'jalad' 最后上传的图书。您可以随心所欲地执行此操作,但假设您将列值存储在名为 $jalad_last

的变量中

你只需要这样写你的 if 语句

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

}