<?php
  ///////////////////////////////////////////////////
  //2010 (C)
  //Карпухин Павел
  ///////////////////////////////////////////////////

  require_once "../utils/chlang.php";

  $title = $TEXT['book-title2'];

  require_once "../config_a.php";
  require_once "../utils/functions.php";
  require_once "../utils/top_adm.php";

  if($result2['administrator'] == 0) {puterror($result2['skin'],$TEXT['error-loginform2']); require_once "../utils/bottom_adm.php"; exit();}
  if(empty($_POST['book_id'])) {puterror($result2['skin'],$TEXT['error-book5']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['book_id'])>255) {puterror($result2['skin'],$TEXT['error-book6']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['familiya'])>255) {puterror($result2['skin'],$TEXT['error-aut4']); require_once "../utils/bottom_adm.php"; exit();}
  $_POST['familiya'] = rep($_POST['familiya']);
  $_POST['book_id'] = rep($_POST['book_id']);
  $book_id = $_POST['book_id'];

//function disabled by php > 5.5.3
////import_request_variables("gP", "in_");
// before using only ->  while(list($id,$value) = each($in_autor))

  while(list($id2,$value2) = each($_POST))
   {
    if($id2 == "autor")
    {
     while(list($id,$value) = each($value2))
      {
     if($id > 0 && $value == 'on')
     {
      $query_chk = mysqli_query($dbcnx,"SELECT COUNT(autor_id) AS c_autor_id FROM tbl_s_book_aut WHERE `autor_id` = '$id' AND `book_id` = '$book_id'");
      $coun = mysqli_fetch_array($query_chk);
       if($coun['c_autor_id'] == 0)
       {
       $query = "INSERT INTO tbl_s_book_aut (`s_book_aut` , `autor_id` , `book_id` ) VALUES ( NULL , '$id' , '$book_id')";
        $result = mysqli_query($dbcnx,$query);
         if(!$result) {puterror($result2['skin'],$TEXT['error-tbl16']); require_once "../utils/bottom_adm.php"; exit();}
       } elseif ($coun['c_autor_id'] >1)
        {
        $query_clear = mysqli_query($dbcnx,"DELETE FROM tbl_s_book_aut WHERE `book_id`= '$book_id' AND `autor_id` = '$id' ");
         if(!$query_clear) {puterror($result2['skin'],$TEXT['error-tbl16']); require_once "../utils/bottom_adm.php"; exit();}

        $query = "INSERT INTO tbl_s_book_aut (`s_book_aut` , `autor_id` , `book_id` ) VALUES ( NULL , '$id' , '$book_id')";
        $result = mysqli_query($dbcnx,$query);
         if(!$result) {puterror($result2['skin'],$TEXT['error-tbl16']); require_once "../utils/bottom_adm.php"; exit();}
        }
     }
   }
  }
 }
    echo "<html><head>
          <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=add_autor.php?book_id=".$book_id."&amp;familiya=".$_POST['familiya']."' />
          </head></html>";

  require_once "../utils/bottom_adm.php";
?>