title: Pass PHP variable to MySQL database
body: <p>I'm having trouble passing the value of a variable to a new record in MySQL database. I have created a form that the user fills out with basic information. Then they will upload an image. The PHP code renames the image with the next id to be assigned and concatenates the file type to create the new file name. I assign the new name to a variable $image with </p>
  
  <pre><code>$image = mysqli_insert_id($con) . "." . $imageFileType;
  </code></pre>
  
  <p>I can echo the variable and verify that is works fine - example 431.jpg would be the file name. Also, the image gets uploaded to the server with the correct renaming convention. Now I need to pass that value to the database when the form is submitted. It would be passed to the field name <em>image</em> in the database. So I have along with the other variables</p>
  
  <pre><code>$id = $_POST['id'];
  $firstname = $_POST['firstname'];
  $department = $_POST['department'];
  $email = $_POST['email'];
  $image = $_POST['image'];
  </code></pre>
  
  <p>I am trying to pass the value in the form using a hidden field that looks like this:</p>
  
  <pre><code>&lt;input name="image" type="hidden" value="&lt;?php echo $image; ?&gt;" /&gt;
  </code></pre>
  
  <p>All the other data gets passed into the database except for the image name. I have tried many variations - even tried to use $_POST['$image'] for the value. Again, I can echo the value when the form is submitted so the value exists - I just can't pass it into the database record. I can get it to work if I enter the data in the form field manually. I have made the field type VARCHAR and TEXT in phpMyAdmin just to try different things. </p>
  
  <p>If any can help that would be great.</p>
  
  <p>Below is the $sqlinsert statement:</p>
  
  <pre><code>$sqlinsert = "INSERT INTO `test_2015`.`test_table` ( `id` , `firstname`, `department`,  `email`, `image` ) 
  VALUES ( '$id',  '$firstname', '$department',  '$email', '$image' )";
  </code></pre>
  
  <p>BELOW IS THE PHP:</p>
  
  <pre><code>    &lt;?php
  
  if (isset($_POST['submitted'])){
  include('connect.php');
  // VARIABLES
      $id = $_POST['id'];
      $firstname = $_POST['firstname'];
      $department = $_POST['department'];
      $email = $_POST['email'];
      $image = $_POST['image'];
      $sqlinsert = "INSERT INTO `test_2015`.`test_table` ( `id` , `firstname`, `department`, `email`, `image` ) 
      VALUES ( '$id',  '$firstname', '$department', '$email', '$image' )";
  
      //NESTED IF STATEMENT
      // RUN THE QUERY
  if ( !mysqli_query($con, $sqlinsert) )
  {
      die('error inserting new record');
  
  } // END OF NESTED IF STATEMENT
  
  // START IMAGE UPLOAD HERE ******************************************
  
  $target_dir = "../images/";
  $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
  $uploadOk = 1;
  $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
  
  
      $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
      if($check !== false) {
          echo "File is an image - " . $check["mime"] . ".";
          $uploadOk = 1;
      } else {
          echo "File is not an image.";
          $uploadOk = 0;
      }
  //}  REMOVE THIS IF ISSET END
  
  // Check if file already exists
  if (file_exists($target_file)) {
      echo "Sorry, file already exists.   ";
      $uploadOk = 0;
  }
  // Allow certain file formats
  if($imageFileType != "jpg" &amp;&amp; $imageFileType != "png" &amp;&amp; $imageFileType != "jpeg" &amp;&amp; $imageFileType != "gif" ) {
      echo "Sorry, only JPG, JPEG, PNG &amp; GIF files are allowed.";
      $uploadOk = 0;
  }
  // Check if $uploadOk is set to 0 by an error
  if ($uploadOk == 0) {
      echo "&lt;div style ='font:16px Arial,tahoma,sans-serif;color:#ff0000;font-weight:bold'&gt;File was not uploaded.&lt;/div&gt;";
  
  
  // if everything is ok, try to upload file
  } else {
  
          $imageName = mysqli_insert_id($con) . "." . $imageFileType;
          if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $imageName ))
  
      {
  
  // CHANGE FILE NAME TO CURRENT ID
  // USING mysqli_insert_id($con) TO GET ID AND CONCATENATE FILE TYPE
          echo "New IMAGE file name is : ", $imageName;
  
  // PASS NAME FOR IMAGE TO $image HERE       
          $image = $imageName;
          echo "image =  : ", $image;
          echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
          echo "&lt;br&gt;";
  // GET THE ASSIGNED ID USING: mysqli_insert_id()
          echo "New Record ID is : "  . mysqli_insert_id($con);
  
      } else {
          echo "&lt;div style ='font:16px Arial,tahoma,sans-serif;color:#ff0000;font-weight:bold'&gt;
          Sorry, there was an error uploading your file.&lt;/div&gt;";
          echo "&lt;br&gt;";
  
      }
  }
      // END IMAGE UPLOAD HERE   ******************************************
      $newrecord = "1 record added to the database";
      echo "&lt;br&gt;";
  } // END MAIN IF STATEMENT
  // Close connection
  mysqli_close($con);
  ?&gt;
  </code></pre>
  
  <p>BELOW IS MY FORM:</p>
  
  <pre><code>    &lt;form method="post" action="add_record.php" enctype="multipart/form-data"&gt;
  &lt;input type="hidden" name="submitted" value="true" /&gt;
  &lt;fieldset&gt;
      &lt;legend&gt;New Record&lt;/legend&gt;
      &lt;label&gt;&lt;input type="hidden" name="id" /&gt;&lt;/label&gt;
      &lt;label&gt;First Name : &lt;input type="text" name="firstname" required="required" /&gt;&lt;/label&gt;&lt;br /&gt;&lt;br /&gt;
      &lt;label&gt;Department : &lt;input type="text" name="department" required="required" /&gt;&lt;/label&gt;&lt;br /&gt;&lt;br /&gt;
      &lt;label&gt;Email Address : &lt;input type="text" name="email" required="required" /&gt;&lt;/label&gt;&lt;br /&gt;&lt;br /&gt;
      &lt;label&gt;Image Name: &lt;input name="image" type="hidden" value="&lt;?php echo $image; ?&gt;" /&gt;&lt;/label&gt;
  &lt;/fieldset&gt;
  &lt;br /&gt;
  Select image to upload:
  &lt;input type="file" name="fileToUpload" id="fileToUpload"&gt;
  &lt;input type="submit" value="add new record button"  /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>RESULTS FROM var_dump($POST);</p>
  
  <p>array(6) { ["submitted"]=> string(4) "true" ["id"]=> string(0) "" ["firstname"]=> string(10) "first_name" ["department"]=> string(10) "department" ["email"]=> string(5) "email" ["image"]=> string(0) "" } </p>