title: Inserting inputted data and session ID into database
body: <p>I seem to be having a lot of trouble with the question so if anybody could help me out I'd really appreciate it. </p>
  
  <p>I'm learning programming at the moment (early stages keep in mind) and I'm making this mini application myself to teach me different things. I have a HTML file which creates a form so the user can add in a deal name and a deal description to the databases</p>
  
  <p>The text entered into the 2 textbox's in the form will be captured and will create a JSON object via a javascript function</p>
  
  <p>The Javascript function will then be read over to a PHP file created which will take the JSON object and write the given values into the database like so:</p>
  
  <pre><code>&lt;?php
  
  $var1 = $_REQUEST['action']; // We dont need action for this tutorial, but in a complex code you need a way to determine ajax action nature
  $jsonObject = json_decode($_REQUEST['outputJSON']); // Decode JSON object into readable PHP object
  
  $name = $jsonObject-&gt;{'name'}; // Get name from object
  $desc = $jsonObject-&gt;{'desc'}; // Get desc from object
  
  
  mysql_connect("localhost","root","");  // Conect to mysql, first parameter is location, second is mysql username and a third one is a mysql password
  @mysql_select_db("findadeal") or die( "Unable to select database"); // Connect to database called test
  
  $query = "INSERT INTO deal (dname, description, restaurantid) VALUES ('$name' ,'$desc' '$username') WHERE $username=(Select restaurantid FROM restaurant where username = '$username')";
  $result=mysql_query($query);
  $num = mysql_numrows($result);
  
  if($num != 0) {
  
      echo "true";
  
  } else {
  
      echo "false"; 
  
  }
  ?&gt;
  </code></pre>
  
  <p>This is where my problem is lying. This HTML file where you can add the deal is part of a larger project im creating. The user would have already logged in and a session has already been created - I know this works as the mini php statement I have inserted into the top of my HTML files echos "It Works" meaning the user is logged in and the details are still in the array:</p>
  
  <pre><code>&lt;?php
  if( !isset( $_SESSION ) ){
   session_start();
  echo "Whats Happening";
      }
  
  if( isset( $_SESSION['username'] ) ){
    /* User is logged in */
    echo "IT WORKS!";
      } ?&gt;
  </code></pre>
  
  <p>What I cant do and need help with is this:</p>
  
  <p>When deal data is being inserted into the database, the fields they are being inserted in include:</p>
  
  <ol>
  <li>dealid which is autoimplemented so I dont need to do anything to that</li>
  <li>dname which is the deal name of which the user inserted the info into the form</li>
  <li>description which again the user inserted the info for into the form</li>
  <li>restaurantid which is the id of the logged in user</li>
  </ol>
  
  <p>What I can't figure out to do is find out how to get the restaurantid from the session variable and include in the MYSQL statement.</p>
  
  <p>The SQL statement in the PHP file below is wrong I'm aware but I was just trying to piece it together myself to see if I could create some sort of logic to the problem.</p>
  
  <p>In plain english what im trying to do is get the username which is being stored in the session variable and then create an sql statement where I go:</p>
  
  <ul>
  <li>insert into deals table the name, description and restaurantid where restaurantid is from  the restaurant tabel and it can be gotten by finding which username from the session variable is the same as a username within the restaurant table. </li>
  </ul>
  
  <p>From that I'd be able to commit the deal to being inserted as I would have gotten the restaurant id from the other table!</p>
  
  <p>I hope this makes sense, I've been at this for ages and had no avail! If this is shown to me I'd be able to move onto lots of other areas such as amending and deleting etc it's just I need this getting the session piece figured out first. MASSIVE THANKS IN ADVANCE FOR THE HELP!!! </p>