title: Php Mysql: Button does not insert into database
body: <p>I am new here so apologies in advance if my explanations are not straightforward enough.</p>
  
  <p>I am trying to insert information into the database via a button function but I am having no luck. I receive no errors so I'm guessing I have code in the wrong place but I cannot work it out. Thank you in advance for any help given.</p>
  
  <pre><code>&lt;?php
  session_start();
  include("includes/db.php");
  include("functions/functions.php");
  ?&gt;
  
  &lt;?php
  
  
  $tutorial_id = @$_GET['tut_id'];
  
  $get_tutorials = "select * from tutorials where tutorial_url='$tutorial_id'";
  
  $run_tutorials = mysqli_query($con,$get_tutorials);
  
  $check_tutorials = mysqli_num_rows($run_tutorials);
  
  if($check_tutorials == 0){
  
  echo "&lt;script&gt; window.open('index.php','_self') &lt;/script&gt;";
  
  }
  else{
  
  
  
  $row_tutorials = mysqli_fetch_array($run_tutorials);
  
  $tut_id = $row_tutorials['tutorial_id'];
  
  $tut_title = $row_tutorials['tutorial_title'];
  
  $tut_img1 = $row_tutorials['tutorial_img1'];
  
  $tut_img2 = $row_tutorials['tutorial_img2'];
  
  $tut_img3 = $row_tutorials['tutorial_img3'];
  
  $tut_video = $row_tutorials['tutorial_video'];
  
  $tut_keywords = $row_tutorials['tutorial_keywords'];
  
  $status = $row_tutorials['status'];
  
  $tut_url = $row_tutorials['tutorial_url'];
  ?&gt;
  
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  
  &lt;head&gt;
  
  &lt;title&gt; &lt;?php echo $tut_title; ?&gt; &lt;/title&gt;
  
  &lt;meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" &gt;
  
  &lt;meta name="keywords" content="&lt;?php echo $tut_keywords; ?&gt;" &gt;
  
  &lt;meta name="author" content="YouCanCode" &gt;
  
  &lt;link href="http://fonts.googleapis.com/css?family=Roboto:400,500,700,300,100" rel="stylesheet" &gt;
  
  &lt;link href="styles/bootstrap.min.css" rel="stylesheet"&gt;
  
  &lt;link href="styles/style.css" rel="stylesheet"&gt;
  
  &lt;link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous"&gt;
  
  &lt;/head&gt;
  
  &lt;body&gt;
  
  &lt;div id="top"&gt;&lt;!-- top Starts --&gt;
  
  &lt;div class="container"&gt;&lt;!-- container Starts --&gt;
  
  &lt;div class="col-md-6 offer"&gt;&lt;!-- col-md-6 offer Starts --&gt;
  
  &lt;a href="#" class="btn btn-success btn-sm" &gt;
  &lt;?php
  if(!isset($_SESSION['user_email'])){
  echo "Welcome: Guest";
  }else{
  echo "Welcome: " . $_SESSION['user_email'] . "";
  }
  ?&gt;
  &lt;/a&gt;
  
  &lt;/div&gt;&lt;!-- col-md-6 offer Ends --&gt;
  
  &lt;div class="col-md-6"&gt;&lt;!-- col-md-6 Starts --&gt;
  &lt;ul class="menu"&gt;&lt;!-- menu Starts --&gt;
  
  
  &lt;li&gt;
  &lt;?php
  if(!isset($_SESSION['user_email'])){
  echo "&lt;a href='login.php' &gt;MY PROFILE&lt;/a&gt;";
  }
  else{
  echo "&lt;a href='users/my_account.php?my_favourites'&gt;MY PROFILE&lt;/a&gt;";
  }
  ?&gt;
  &lt;/li&gt;
  
  
  &lt;li&gt;
  &lt;?php
  
  if(!isset($_SESSION['user_email'])){
  echo "&lt;a href='login.php'&gt;LOGIN&lt;/a&gt;";
  }else {
  echo "&lt;a href='logout.php'&gt;LOGOUT&lt;/a&gt;";
  }
  ?&gt;
  &lt;/li&gt;
  
  &lt;/ul&gt;&lt;!-- menu Ends --&gt;
  
  &lt;/div&gt;&lt;!-- col-md-6 Ends --&gt;
  
  &lt;/div&gt;&lt;!-- container Ends --&gt;
  &lt;/div&gt;&lt;!-- top Ends --&gt;
  
  &lt;div class="navbar navbar-default" id="navbar"&gt;&lt;!-- navbar navbar-default Starts --&gt;
  &lt;div class="container" &gt;&lt;!-- container Starts --&gt;
  
  &lt;div class="navbar-header"&gt;&lt;!-- navbar-header Starts --&gt;
  
  &lt;a class="navbar-brand home" href="index.php" &gt;&lt;!--- navbar navbar-brand home Starts --&gt;
  YouCanCode
  &lt;/a&gt;&lt;!--- navbar navbar-brand home Ends --&gt;
  
  &lt;button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navigation"  &gt;
  
  &lt;span class="sr-only" &gt;Toggle Navigation &lt;/span&gt;
  
  &lt;i class="fa fa-align-justify"&gt;&lt;/i&gt;
  
  &lt;/button&gt;
  
  &lt;button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#search" &gt;
  
  &lt;span class="sr-only" &gt;Toggle Search&lt;/span&gt;
  
  &lt;i class="fas fa-search" &gt;&lt;/i&gt;
  
  &lt;/button&gt;
  
  
  &lt;/div&gt;&lt;!-- navbar-header Ends --&gt;
  
  &lt;div class="navbar-collapse collapse" id="navigation" &gt;&lt;!-- navbar-collapse collapse Starts --&gt;
  
  &lt;div class="padding-nav" &gt;&lt;!-- padding-nav Starts --&gt;
  
  &lt;ul class="nav navbar-nav navbar-left"&gt;&lt;!-- nav navbar-nav navbar-left Starts --&gt;
  
  &lt;li&gt;
  &lt;a href="index.php"&gt; Home &lt;/a&gt;
  &lt;/li&gt;
  
  &lt;li class="active" &gt;
  &lt;a href="tutorials.php"&gt;Tutorials&lt;/a&gt;
  &lt;/li&gt;
  
  &lt;li&gt;
  &lt;?php
  
  if(!isset($_SESSION['user_email'])){
  echo "&lt;a href='login.php' &gt;My Profile&lt;/a&gt;";
  }
  else{
  echo "&lt;a href='users/my_account.php?my_favourites'&gt;My Profile&lt;/a&gt;";
  }
  ?&gt;
  &lt;/li&gt;
  
  &lt;li&gt;
  &lt;a href="about.php"&gt; About Us &lt;/a&gt;
  &lt;/li&gt;
  
  &lt;li&gt;
  &lt;a href="contact.php"&gt; Contact Us &lt;/a&gt;
  &lt;/li&gt;
  
  &lt;/ul&gt;&lt;!-- nav navbar-nav navbar-left Ends --&gt;
  
  &lt;/div&gt;&lt;!-- padding-nav Ends --&gt;
  
  &lt;div class="navbar-collapse collapse right"&gt;&lt;!-- navbar-collapse collapse right Starts --&gt;
  &lt;button class="btn navbar-btn btn-primary" type="button" data-toggle="collapse" data-target="#search"&gt;
  &lt;span class="sr-only"&gt;Toggle Search&lt;/span&gt;
  &lt;i class="fa fa-search"&gt;&lt;/i&gt;
  &lt;/button&gt;
  &lt;/div&gt;&lt;!-- navbar-collapse collapse right Ends --&gt;
  
  &lt;div class="collapse clearfix" id="search"&gt;&lt;!-- collapse clearfix Starts --&gt;
  
  &lt;form class="navbar-form" method="get" action="results.php"&gt;&lt;!-- navbar-form Starts --&gt;
  
  &lt;div class="input-group"&gt;&lt;!-- input-group Starts --&gt;
  &lt;input class="form-control" type="text" placeholder="Search" name="user_query" required&gt;
  &lt;span class="input-group-btn"&gt;&lt;!-- input-group-btn Starts --&gt;
  &lt;button type="submit" value="Search" name="search" class="btn btn-primary"&gt;
  &lt;i class="fa fa-search"&gt;&lt;/i&gt;
  &lt;/button&gt;
  &lt;/span&gt;&lt;!-- input-group-btn Ends --&gt;
  &lt;/div&gt;&lt;!-- input-group Ends --&gt;
  
  &lt;/form&gt;&lt;!-- navbar-form Ends --&gt;
  
  &lt;/div&gt;&lt;!-- collapse clearfix Ends --&gt;
  
  &lt;/div&gt;&lt;!-- navbar-collapse collapse Ends --&gt;
  
  &lt;/div&gt;&lt;!-- container Ends --&gt;
  &lt;/div&gt;&lt;!-- navbar navbar-default Ends --&gt;
  
  
  &lt;div id="content" &gt;&lt;!-- content Starts --&gt;
  &lt;div class="container" &gt;&lt;!-- container Starts --&gt;
  
  &lt;div class="col-md-12" &gt;&lt;!--- col-md-12 Starts --&gt;
  
  &lt;ul class="breadcrumb" &gt;&lt;!-- breadcrumb Starts --&gt;
  
  &lt;li&gt;
  &lt;a href="index.php"&gt;Home&lt;/a&gt;
  &lt;/li&gt;
  
  &lt;li&gt;&lt;a href="tutorials.php"&gt;Tutorials&lt;/a&gt;&lt;/li&gt;
  
  &lt;li&gt; &lt;?php echo $tut_title; ?&gt; &lt;/li&gt;
  
  &lt;/ul&gt;&lt;!-- breadcrumb Ends --&gt;
  &lt;/div&gt;&lt;!--- col-md-12 Ends --&gt;
  
  
  
  &lt;div class="col-md-12"&gt;&lt;!-- col-md-12 Starts --&gt;
  
  &lt;div class="row" id="tuorialtMain"&gt;&lt;!-- row Starts --&gt;
  
  &lt;div class="col-sm-6"&gt;&lt;!-- col-sm-6 Starts --&gt;
  
  &lt;div id="mainImage"&gt;&lt;!-- mainImage Starts --&gt;
  
  &lt;div id="myCarousel" class="carousel slide" data-ride="carousel"&gt;
    &lt;div class="carousel-inner"&gt;
      &lt;div class="carousel-item active"&gt;
  
  &lt;?php echo $tut_video; ?&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;/div&gt;&lt;!-- mainImage Ends --&gt;
  &lt;/div&gt;&lt;!-- col-sm-6 Ends --&gt;
  
  &lt;div class="col-sm-6" &gt;&lt;!-- col-sm-6 Starts --&gt;
  
  &lt;div class="box" &gt;&lt;!-- box Starts --&gt;
  
  &lt;h1 class="text-center" &gt; &lt;?php echo $tut_title; ?&gt; &lt;/h1&gt;
  
  
  &lt;p class="text-center buttons" &gt;&lt;!-- text-center buttons Starts --&gt;
  
  &lt;button class="btn btn-primary" type="submit" name="add_favourite"&gt;
  &lt;i class="fas fa-heart"&gt;&lt;/i&gt; Add To Favourites
  &lt;/button&gt;
  
  &lt;?php
  
  if(isset($_POST['add_favourite'])){
  
  if(!isset($_SESSION['user_email'])){
  
  echo "&lt;script&gt;alert('You Must Be Logged In!')&lt;/script&gt;";
  
  echo "&lt;script&gt;window.open('login.php','_self')&lt;/script&gt;";
  
  }
  else{
  
  $user_session = $_SESSION['user_email'];
  
  $get_user = "select * from users where user_email='$user_session'";
  
  $run_user = mysqli_query($con,$get_user);
  
  $row_user = mysqli_fetch_array($run_user);
  
  $user_id = $row_user['user_id'];
  
  $select_favourite = "select * from favourite where user_id='$user_id' AND tutorial_id='$tut_id'";
  
  $run_favourite = mysqli_query($con,$select_favourite);
  
  $check_favourite = mysqli_num_rows($run_favourite);
  
  if($check_favourite == 1){
  
  echo "&lt;script&gt;alert('This Tutorial Is Already In Your Favourites!')&lt;/script&gt;";
  
  echo "&lt;script&gt;window.open('$tut_url','_self')&lt;/script&gt;";
  
  }
  else{
  
  $insert_favourite = "insert into favourite (user_id,tutorial_id) values ('$user_id','$tut_id')";
  
  $run_favourite = mysqli_query($con,$insert_favourite);
  
  if($run_favourite){
  
  echo "&lt;script&gt; alert('Tutorial Has Been Added To Favourites!') &lt;/script&gt;";
  
  echo "&lt;script&gt;window.open('$tut_url','_self')&lt;/script&gt;";
  
  }
  
  }
  
  }
  
  }
  
  ?&gt;
  
  &lt;/p&gt;&lt;!-- text-center buttons Ends --&gt;
  
  &lt;/form&gt;&lt;!-- form-horizontal Ends --&gt;
  
  &lt;/div&gt;&lt;!-- box Ends --&gt;
  
  
  &lt;div class="row" id="thumbs" &gt;&lt;!-- row Starts --&gt;
  
  &lt;div class="col-xs-4" &gt;&lt;!-- col-xs-4 Starts --&gt;
  &lt;a href="#" class="thumb" &gt;
  &lt;img src="admin_area/tutorial_images/&lt;?php echo $tut_img1; ?&gt;" class="img-responsive" &gt;
  &lt;/a&gt;
  &lt;/div&gt;&lt;!-- col-xs-4 Ends --&gt;
  
  &lt;div class="col-xs-4" &gt;&lt;!-- col-xs-4 Starts --&gt;
  &lt;a href="#" class="thumb" &gt;
  &lt;img src="admin_area/tutorial_images/&lt;?php echo $tut_img2; ?&gt;" class="img-responsive" &gt;
  &lt;/a&gt;
  &lt;/div&gt;&lt;!-- col-xs-4 Ends --&gt;
  
  &lt;div class="col-xs-4" &gt;&lt;!-- col-xs-4 Starts --&gt;
  &lt;a href="#" class="thumb" &gt;
  &lt;img src="admin_area/tutorial_images/&lt;?php echo $tut_img3; ?&gt;" class="img-responsive" &gt;
  &lt;/a&gt;
  &lt;/div&gt;&lt;!-- col-xs-4 Ends --&gt;
  
  
  &lt;/div&gt;&lt;!-- row Ends --&gt;
  &lt;/div&gt;&lt;!-- col-sm-6 Ends --&gt;
  
  
  &lt;/div&gt;&lt;!-- row Ends --&gt;
  
  &lt;br&gt;&lt;br&gt;
  
  &lt;div class="col-md-3 col-sm-6"&gt;&lt;!-- col-md-3 col-sm-6 Starts --&gt;
  
  &lt;div class="box same-height headline"&gt;&lt;!-- box same-height headline Starts --&gt;
  
  &lt;h3 class="text-center"&gt;Check These Tutorials Out&lt;/h3&gt;
  
  &lt;/div&gt;&lt;!-- box same-height headline Ends --&gt;
  
  &lt;/div&gt;&lt;!-- col-md-3 col-sm-6 Ends --&gt;
  
  &lt;?php
  
  $get_tutorials = "select * from tutorials order by rand() LIMIT 0,3";
  
  $run_tutorials = mysqli_query($con,$get_tutorials);
  
  while($row_tutorials = mysqli_fetch_array($run_tutorials)) {
  
  $tut_id = $row_tutorials['tutorial_id'];
  
  $tut_title = $row_tutorials['tutorial_title'];
  
  $tut_img1 = $row_tutorials['tutorial_img1'];
  
  $language_id = $row_tutorials['language_id'];
  
  $get_language= "select * from code_languages where language_id='$language_id'";
  
  $run_language = mysqli_query($db,$get_language);
  
  $row_language = mysqli_fetch_array($run_language);
  
  $language_name = $row_language['language_title'];
  
  $tut_url = $row_tutorials['tutorial_url'];
  
  
  echo "
  
  &lt;div class='col-md-3 col-sm-6 center-responsive' &gt;
  
  &lt;div class='tutorial' &gt;
  
  &lt;a href='$tut_url' &gt;
  
  &lt;img src='admin_area/tutorial_images/$tut_img1' class='img-responsive' &gt;
  
  &lt;/a&gt;
  
  &lt;div class='text' &gt;
  
  &lt;center&gt;
  &lt;p class='btn btn-primary'&gt; $language_name &lt;/p&gt;
  &lt;/center&gt;
  
  &lt;hr&gt;
  
  &lt;h3&gt;&lt;a href='$tut_url' &gt;$tut_title&lt;/a&gt;&lt;/h3&gt;
  
  &lt;p class='buttons' &gt;
  
  &lt;a href='$tut_url' class='btn btn-default' &gt;View Tutorial&lt;/a&gt;
  
  &lt;/p&gt;
  
  &lt;/div&gt;
  
  &lt;/div&gt;
  
  &lt;/div&gt;
  
  ";
  
  
  }
  
  
  ?&gt;
  
  
  &lt;?php } ?&gt;
  
  &lt;/div&gt;&lt;!-- row same-height-row Ends --&gt;
  &lt;/div&gt;&lt;!-- col-md-12 Ends --&gt;
  
  &lt;/div&gt;&lt;!-- container Ends --&gt;
  &lt;/div&gt;&lt;!-- content Ends --&gt;
  
  &lt;?php
  include("includes/footer.php");
  ?&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>