如何在输入超出限制的数据时提示警报?

I am working on library system and having a table of rack and a table of books.

Every book should belong to one rack and no rack can contain more than 10 books when user is adding a book in database when he select a rack if it already contains 10 books in it then it should prompt an alert.

Only 10 books can be added in a rack. An alert should prompt if user is trying to add more books. How can I do please help me.

     <?php

            $msg="";
            $query1 = "SELECT * FROM tbl_rack"; 
            $result1 = mysql_query($query1); 
            if(isset($_POST['btnadd']))

                {


                    $title      =$_POST['txttitle'];
                    $author =$_POST['txtauthor'];
                    $year       =$_POST['txtyear'];
                    $rack       =$_POST['txtrack'];
                    $query="insert into tbl_books(book_title,book_author,book_year,book_rack)values('$title','$author','$year','$rack')";

                    $rs     =   mysql_query($query);
                    if ($rs == 1)
                        $msg="Book has been added successfully" ;
                    else  
                        $msg="Try Again";


                }

You can use mysqli_num_rows to get the number of results your $result1 variable was able to fetch. After that just use a conditional to check if that number is already 10.

  1. You need to add RackID to your POST request so that you know which rack to add books to.
  2. Modify your SELECT statement to have a WHERE clause filtering by RackID passed through POST request.
  3. Inside the IF statement, check for total number of rows returned by the SELECT query (as mentioned by @Fer Salas) using mysql_num_rows. Keep in mind this function is deprecated in PHP 7.0.0 (http://php.net/manual/en/function.mysql-num-rows.php)