title: how to insert and update mysql table in php
body: <p>i have two tables
  one is sale
  2nd is stock</p>
  
  <p>in stock table i have all item for sale</p>
  
  <p>then i use this code to sale items and update stock table quantity also but its not update record and insert data in mysql i want to to update and insert data in mysql how can i do this</p>
  
  <p>please help me to fix this issue thanks</p>
  
  <p>Form
           
            
           </p>
  
  <pre><code>       &lt;div align="center"&gt;
           &lt;legend align="center" &gt;Stock!&lt;/legend&gt;
         &lt;/div&gt;
         &lt;div class="fieldset"&gt;
           &lt;p&gt;
         &lt;label class="field" for="date"&gt;Date: &lt;/label&gt;
  
             &lt;input name="date" type="text" class="tcal" value="&lt;?php echo date("Y-m-d");; ?&gt;" size="30"/&gt;
               &lt;/p&gt;
         &lt;p&gt;
           &lt;label class="field" for="username"&gt;User Name : &lt;/label&gt;
  
           &lt;input name="username" type="text"  id="username"  value="&lt;?php echo $username; ?&gt;" size="30"/&gt;
         &lt;/p&gt;
         &lt;p&gt;
           &lt;label class="field" for="item"&gt;Item: &lt;/label&gt;
  
           &lt;input name="item" type="text"  value="&lt;?php echo $item; ?&gt;" size="30"/&gt;
         &lt;/p&gt;
  
  
             &lt;p&gt;
             &lt;label class="field" &gt;Quantity :&lt;/label&gt;
             &lt;input  name="quantity" type="text" value="&lt;?php echo $quantity; ?&gt;"  size="30"/&gt;
           &lt;/p&gt; 
             &lt;p&gt;
             &lt;label class="field" &gt;Amount :&lt;/label&gt;
             &lt;input  name="amount" type="text" value="&lt;?php echo $amount; ?&gt;"  size="30"/&gt;
           &lt;/p&gt; 
        &lt;/div&gt;
       &lt;/fieldset&gt;
         &lt;p align="center" class="required style3"&gt;Please Fill The Complete Form &lt;/p&gt;
         &lt;div align="center"&gt;
           &lt;input name="submit" type="submit" class="style1" value="Submit"&gt;
  
         &lt;/div&gt;
       &lt;/form&gt; 
  
   &lt;?php 
   // if there are any errors, display them
   if ($error != '')
   {
   echo '&lt;div style="padding:4px; border:1px solid red; color:red;"&gt;'.$error.'&lt;/div&gt;';
   }
   ?&gt; 
  </code></pre>
  
  <p>Php Code</p>
  
  <pre><code>  &lt;?php
  /* 
   NEW.PHP
   Allows user to create a new entry in the database
  */
  
   // creates the new record form
   // since this form is used multiple times in this file, I have made it a function that is easily reusable
   function renderForm($id ,$date ,$username,$item,$quantity,$amount, $error)
  
   {
   ?&gt;
  
   &lt;?php 
       }
  
  
       $item = $_GET['item']
  
       // connect to the database
       include('connect-db.php');
  
       // check if the form has been submitted. If it has, start to process the form and save it to the database
       if (isset($_POST['submit']))
       { 
       // get form data, making sure it is valid
       $id = mysql_real_escape_string(htmlspecialchars($_POST['id']));
       $date = mysql_real_escape_string(htmlspecialchars($_POST['date']));
       $username = mysql_real_escape_string(htmlspecialchars($_POST['username']));
       $item = mysql_real_escape_string(htmlspecialchars($_POST['item']));
       $quantity = mysql_real_escape_string(htmlspecialchars($_POST['quantity']));
       $amount = mysql_real_escape_string(htmlspecialchars($_POST['amount']));
  
  
  
       // check to make sure both fields are entered
       if ($date == '' || $quantity == '')
       {
       // generate error message
       $error = 'ERROR: Please fill in all required fields!';
  
       // if either field is blank, display the form again
       renderForm($id ,$date ,$username,$item,$quantity,$amount,  $error);
  
       }
       else
       {
       // save the data to the database
        mysql_query("INSERT INTO  sale SET date='$date', username='$username',item='$item',quantity='$quantity',amount='$amount'")
  
  
      $result = mysql_query("UPDATE stock SET quantity='-$quantity' WHERE item='$item'") 
  
  
  
  
       or die(mysql_error()); 
       echo "&lt;center&gt;Stock Enter Complete!&lt;/center&gt;";
       // once saved, redirect back to the view page
  
       }
       }
       }
       else
       // if the form hasn't been submitted, display the form
       {
       renderForm('','','','','','','','');
       }
  
               ?&gt;
  </code></pre>