-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";
    }
}
?>
2
  • 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. Commented Mar 19, 2022 at 8:50
  • 2
    Please read: How can I prevent SQL injection in PHP? Commented Mar 19, 2022 at 8:55

1 Answer 1

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

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.