File moving problem

Does not move the workbook or create a new entry in the database. Why, I don’t understand. The old one is deleted and the new one is not created.

if($_GET['w'] && $_GET['id']){
if ($_GET['w'] == "on"){
$idi = $_GET['id'];
$on = "SELECT * FROM `test_b` WHERE `id` = ".$idi;
$on = mysql_query($on);
$on = mysql_fetch_assoc($on);
$d_on = "INSERT INTO `books` (`id`,`name`,`avtor`,avtor_2`,avtor_3`,`info`, `ctil`, `cym`,`kol`,`date`) VALUES 
(NULL,'".$on['name']."','".$on['avtor']."','".$on['avtor_2']."','".$on['avtor_3']."','".$on['info']."' ,'".$on['ctil']."',0,0,CURRENT_TIMESTAMP)";
$d_on = mysql_query($d_on);
$id_b = mysql_fetch_assoc($d_on);
if (rename('book_t/'.$idi.'.pdf','book/'.$id_b['id'].'.pdf')){
rename('book_t/img/'.$idi.'.jpg','book/img/'.$id_b['id'].'.jpg');
$lol = "DELETE FROM `test_b` WHERE `id`='".$idi."'";
$lol = mysql_query($lol);
$onn = "SELECT * FROM `register` WHERE `id` ='".$on['id_av']."'";
$onn = mysql_query($onn);
$onn = mysql_fetch_assoc($onn);
$post = $onn['kol_b'] +1;
$got = "UPDATE  `register` SET `kol_b` = '".$post."' , `da` ='".$onn['da']."' WHERE  `id` = '".$onn['id']."' ";
$got = mysql_query($got);
$onnn = "SELECT * FROM `register` WHERE `id` ='".$onn['id']."'";
$onnn = mysql_query($onnn);
$onnn = mysql_fetch_assoc($onnn);
if($onnn['kol_b'] >= 5 and $onnn['rang'] < 1){
$gott = "UPDATE  `register` SET `rang` = '1' , `da` ='".$got['da']."'  WHERE  `id` = '".$onn['id']."' ";
$gott = mysql_query($gott);}
if($onnn['kol_b'] >= 15 and $onnn['rang'] < 2){
$gott = "UPDATE  `register` SET `rang` = '2' , `da` ='".$got['da']."' WHERE  `id` = '".$onn['id']."' ";
$gott = mysql_query($gott);}
if($onnn['kol_b'] >= 30 and $onnn['rang'] < 3){
$gott = "UPDATE  `register` SET `rang` = '3' ,`da` ='".$got['da']."'  WHERE  `id` = '".$onn['id']."' ";
$gott = mysql_query($gott);}
header("Request-URI: admin.php?v=book");
header("Content-Location: admin.php?v=book");
header("Location: admin.php?v=book");
}}
else if ($_GET['w'] == "off"){
$idi = $_GET['id'];
$lol = "DELETE FROM `test_b` WHERE `id`='".$idi."'";
$lol = mysql_query($lol);
if(unlink("book_t/".$idi.".pdf")){unlink("book_t/img/".$idi.".jpg");
header("Request-URI: admin.php?v=book");
header("Content-Location: admin.php?v=book");
header("Location: admin.php?v=book");}else{echo "   !";}
}}

Answer 1, authority 100%

  1. What are the permissions on the book folder?
  2. What is the title of the book? If the book contains ” then the sql query will break. It is advisable to quote the book names with the mysql_real_escape_string
  3. function

  4. Remove id from the insert query, respectively, you need to remove NULL from the VALUES section
  5. to find out the id of the inserted record, you need to use the mysql_insert_id function after mysql_query with an INSERT query