title: php:Can't find the column but it is in the database
body: <p>I am having trouble figuring out what's wrong whit my code. The code is used to take out specific columns from 2 big tables from a database. But we also need to sum 1 specific value when after I have used the query.</p>
  
  <p>The problem is in this part</p>
  
  <pre><code>  $query = substr($query, 4);
      $sql2 = "SELECT SUM(Forspris) FROM orderrad
      JOIN orderhuvud on orderhuvud.OrderKund = orderrad.Orderdatum
       where ". $query;
          echo $sql2;
          $result = mysqli_query($conn, $sql2) or die(mysqli_error($conn));
          while($row = mysqli_fetch_array($result)){
              echo print_r($row);     }
  </code></pre>
  
  <p>this is the error i get:</p>
  
  <blockquote>
    <p>SELECT SUM(Forspris) FROM orderrad JOIN orderhuvud on orderhuvud.OrderKund = orderrad.Orderdatum where OrderKund = '15' AND Orderdatum between '2015-04-16' AND '2015-05-06'Unknown column 'orderrad.Orderdatum' in 'on clause'</p>
  </blockquote>
  
  <p>but it longer up in the code it can find the column orderdatum in the query</p>
  
  <pre><code>&lt;!doctype html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"&gt;
  &lt;link rel="stylesheet" href="css.css"&gt;
  &lt;link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.css"&gt;
  
      &lt;/head&gt;
      &lt;body&gt;
  
      &lt;?php
      $servername = "localhost";
      $username = "root";
      $password = "";
      $dbname = "garp";
  
      $conn = new mysqli ($servername, $username, $password, $dbname);
      if ($conn-&gt;connect_error) {
          die("Connection failed: " . $conn-&gt;connect_error);
      }
  
      /*    $query = $_GET['query']; */
          $orderkund = $_GET['Orderkundinput']; 
          $artikel = $_GET['Artikelinput']; 
          $startDate =$_GET['startDate']; 
          $endDate =$_GET['endDate'] ;
  
          $MCkys = "orderdatum";
  
          $min_length = 2;
          $artikelQuery="";
          $orderkundQuery="";
          if(strlen($artikel) &gt;= $min_length){ 
              $artikel = htmlspecialchars($artikel); 
  
              $artikel = mysqli_real_escape_string($conn, $artikel);
              $artikelQuery = " AND Artikelnr = '$artikel'";
          }
          if (strlen($orderkund) &gt;= $min_length){ 
              $orderkund = htmlspecialchars($orderkund); 
  
              $orderkund = mysqli_real_escape_string($conn, $orderkund);
              $orderkundQuery = " AND OrderKund = '$orderkund'";
          }
  
          if (strlen($startDate) &gt;= $min_length){ 
              $startDate = htmlspecialchars($startDate); 
  
              $startDate = mysqli_real_escape_string($conn, $startDate);
              $startDateQuery = " AND Orderdatum between '$startDate' ";
          }
  
          if (strlen($endDate) &gt;= $min_length){ 
              $endDate = htmlspecialchars($endDate); 
  
              $endDate = mysqli_real_escape_string($conn, $endDate);
              $endDateQuery = "AND '$endDate'";
          }
  
          $query = $artikelQuery.$orderkundQuery.$startDateQuery.$endDateQuery;
          if(strlen($query) &gt;= $min_length){ 
          $sql = "SELECT OrderHuvud.Ordernummer ,OrderHuvud.OrderserieIK ,OrderKund ,Fakturakund ,Orderdatum ,Erreferens ,Levereratvarde ,Radnummer ,Artikelnr ,Benamning ,Leveranstid ,Ursprungligtantal ,Levereratantal ,Forspris ,Bruttopris ,Varukostnad FROM garp.OrderHuvud left join garp.OrderRad on OrderHuvud.Ordernummer = OrderRad.Ordernummer where  OrderHuvud.OrderserieIK = 'K'" .$query;
          $raw_results = $conn-&gt;query ($sql);
              $row_cnt = false === $raw_results ? 0 : $raw_results-&gt;num_rows;
              echo " &lt;p class='rows'&gt; Numbers of rows loaded: $row_cnt &lt;/p&gt;";
              if($row_cnt &gt; 0){
         while($raw_result = mysqli_fetch_array($raw_results)){
             echo "&lt;table class='table'&gt;&lt;thead class='thead-light'&gt;&lt;tr&gt;&lt;th class='col'&gt;".'Ordernummer'."&lt;/th&gt;&lt;th class='col'&gt;".'OrderserieIK'."&lt;/th&gt;&lt;th class='col'&gt;".'Orderkund'."&lt;/th&gt;&lt;th class='col'&gt;".'fakturakund'."&lt;/th&gt;&lt;th class='col'&gt;".'orderdatum'."&lt;/th&gt;&lt;th class='col'&gt;".'erreferens'."&lt;/th&gt;&lt;th class='col'&gt;".'leveratvarde'."&lt;/th&gt;&lt;th class='col'&gt;".'radnummer'."&lt;/th&gt;&lt;th class='col'&gt;".'artikelnr'."&lt;/th&gt;&lt;th class='col'&gt;".'benamning'."&lt;/th&gt;&lt;th class='col'&gt;".'leveranstid'."&lt;/th&gt;&lt;th class='col'&gt;".'Ursprungligtantal'."&lt;/th&gt;&lt;th class='col'&gt;".'Levereratantal'."&lt;/th&gt;&lt;th class='col'&gt;".'forspris'."&lt;/th&gt;&lt;th class='col'&gt;".'bruttopris'."&lt;/th&gt;&lt;th class='col'&gt;".'varukostnad'."&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;";
             echo "&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;".$raw_result['Ordernummer']."&lt;/td&gt;&lt;td&gt;".$raw_result['OrderserieIK']."&lt;/td&gt;&lt;td&gt;".$raw_result['OrderKund']."&lt;/td&gt;&lt;td&gt;".$raw_result['Fakturakund']."&lt;/td&gt;&lt;td&gt;".$raw_result['Orderdatum']."&lt;/td&gt;&lt;td&gt;".$raw_result['Erreferens']."&lt;/td&gt;&lt;td&gt;".$raw_result['Levereratvarde']."&lt;/td&gt;&lt;td&gt;".$raw_result['Radnummer']."&lt;/td&gt;&lt;td&gt;".$raw_result['Artikelnr']."&lt;/td&gt;&lt;td&gt;".$raw_result['Benamning']."&lt;/td&gt;&lt;td&gt;".$raw_result['Leveranstid']."&lt;/td&gt;&lt;td&gt;".$raw_result['Ursprungligtantal']."&lt;/td&gt;&lt;td&gt;".$raw_result['Levereratantal']."&lt;/td&gt;&lt;td&gt;".$raw_result['Forspris']."&lt;/td&gt;&lt;td&gt;".$raw_result['Bruttopris']."&lt;/td&gt;&lt;td&gt;".$raw_result['Varukostnad']."&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;";
  
         }
              }
  
              else{
         echo "No return";
              }
          }
          else{
              echo "Minimum length is ".$min_length;
          }
      $query = substr($query, 4);
      $sql2 = "SELECT SUM(Forspris) FROM orderrad
      JOIN orderhuvud on orderhuvud.OrderKund = orderrad.Orderdatum
       where ". $query;
          echo $sql2;
          $result = mysqli_query($conn, $sql2) or die(mysqli_error($conn));
          while($row = mysqli_fetch_array($result)){
              echo print_r($row);     }
          ?&gt;
  </code></pre>
  
  <p>Column Names:</p>
  
  <p><a href="https://i.stack.imgur.com/ee4bl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ee4bl.png" alt="enter image description here"></a></p>