title: php mysqli escape string error in sql syntax
body: <p>I am trying to escape fields posted from a form.  I can successfully insert into the SQL database by commenting out the code that escapes the string.</p>
  
  <p>The error received is:</p>
  
  <pre><code>You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\"test\",\"0123456789\",\"test@test.com\",\"1\",\"1\",\"fgsdfdfndfndfndfndfndfn\' at line 1
  </code></pre>
  
  <p>Here is the code I am using:</p>
  
  <pre><code>$Name= $_POST['fullname'];
  $Phone = $_POST['phone'];
  $email = $_POST['email'];
  $inBuilding = $_POST['inbuilding'];
  $floor = $_POST['floor'];
  $inRoom = $_POST['inroom'];
  $majorDescription = $_POST['majorcategory'];
  $description = $_POST['desc'];
  
  $query = "INSERT INTO `problem`.`reports` (`Name`, `PhoneNumber`, `EmailAddress`, `inBuilding`, `inRoom`, `Description`, `MajorDescription`) VALUES (";
  $query .= '"' . $Name. '","' . $Phone . '","' . $email . '","' . $inBuilding . '","' . $inRoom . '","' . $description . '","' . $majorDescription . '");';
  
  
  $query = mysqli_real_escape_string($connect, $query);
  </code></pre>
  
  <p>I have also tried:</p>
  
  <pre><code>$query = mysqli_escape_string($connect, $query);
  </code></pre>
  
  <p>with the same error.</p>
  
  <p>According to other examples on stack overflow I changed the INSERT INTO code to the following:</p>
  
  <pre><code>$query = "INSERT INTO `problem`.`reports` (Name, PhoneNumber, EmailAddress, inBuilding, inRoom, Description, MajorDescription) VALUES ('$Name', '$Phone', '$email', '$inBuilding', '$inRoom', '$description', '$majorDescription')");
  </code></pre>
  
  <p>This code gave server 500 error.</p>
  
  <p>MySQL is fully updated.</p>
  
  <p>Any assistance appreciated!</p>
  
  <p>MikeW's solution worked.  Also realized I was trying to escape the string before I had opened the database making <code>mysqli_real_escape_string</code> return null.  Connecting to the database first, (<code>$connect= new connect("server","user","password");</code>) solved this problem.  Hopefully this will help anyone else with the same problems.</p>