title: how to save the data that the user input in form page after the script executed?
body: <p>may I know is it possible to save the data from the form which the user input earlier?</p>
  
  <p>Here is the form file:</p>
  
  <pre><code>    &lt;!DOCTYPE HTML&gt;
      &lt;html&gt;
      &lt;head&gt;
          &lt;meta http-equiv="content-type" content="text/html" /&gt;        
          &lt;title&gt;Data&lt;/title&gt;
          &lt;style type="text/css"&gt;
          input {
              background-color: #999;
              border-style: solid;
              border-color: #999;
              border: 0px 4px 0px 4px;
              padding: 3px 6px 3px 6px;
              margin: 10px 0;
          }
          input:focus {
              border-style: solid;
              border-color: black;
          }
          &lt;/style&gt;
      &lt;/head&gt;
  
      &lt;body&gt;
  
      &lt;form action="count.php" method="post" action="post"&gt;
      Name: &lt;input type="text" name="customername" /&gt;&lt;br /&gt;
      DOB: &lt;input type="text" name="date" /&gt; &lt;input type="text" name="month" /&gt; &lt;input type="text" name="year" /&gt;&lt;br /&gt;
      First Row: &lt;input type="text" name="rowone" /&gt;&lt;br /&gt;
      Second Row: &lt;input type="text" name="rowtwo" /&gt;&lt;br /&gt;
      Third Row: &lt;input type="text" name="rowthree" /&gt;&lt;br /&gt;
      &lt;input type="submit" value="Go" /&gt;
      &lt;/form&gt;
  
      &lt;/body&gt;
      &lt;/html&gt;
  </code></pre>
  
  <p>And here is the php that execute the form data that input by the user.</p>
  
  <pre><code> &lt;?php
      $cname = $_POST['customername'];
      $dob_date = $_POST['date'];
      $dob_month = $_POST['month'];
      $dob_year = $_POST['year'];
      $year = gmdate("Y");
      $month = gmdate("m");
      $day = gmdate("d");
      $age = $year - $dob_year; // $age calculates the user's age determined by only the year
      if ($month &lt; $dob_month) { // this checks if the current month is before the user's month of birth
          $age = $age - 1;
      } else if (
              $month == $dob_month &amp;&amp; $day &gt;= $dob_date) { // this checks if the current month is the same as the user's month of birth and then checks if it is the user's birthday or if it is after it
          $age = $age;
      } else if ($month == $dob_month &amp;&amp; $day &lt; $dob_date) { //this checks if the current month is the user's month of birth and checks if it before the user's birthday
          $age = $age - 1;
      }
  
      //add all initial data into an matrix variable for easier access to them later
      //To access rowone use $rows[0][0], rowtwo $rows[1][0] ect.
      //The matrix is an array which contains multiple array. eg. 2-dimensional arrays
      //To get all the variables with $r1X simply retrieve the first array of the matrix eg $rows[0]
      $rows = array(array($_POST['rowone']), array($_POST['rowtwo']), array($_POST['rowthree']), array());
  
      //Similarities between row1 and row2 made me incoporate modulo value as an argument.
      function incmod($a, $m) {
          return ($a % $m) + 1;
      }
  
      //Population each row, with $populationCount number of elements, where each element is added with incmod(X, $mod)
      function populateRow($rowId, $populationCount, $mod) {
          //The global keyword is needed in order for the function to access the global variable.
          global $rows;
          $row = $rows[$rowId];
          while (sizeof($row) &lt; $populationCount) {
              $rowInd = sizeof($row) - 1;
              $m = incmod($row[$rowInd], $mod);
              array_push($row, $m);
          }
  
          //Due to how php works with variables and references we need to set the row back into the global variable.
          $rows[$rowId] = $row;
      }
  
      //This function makes sure that the values allways are between 1 and 12.
      function bindToRange($v) {
          if ($v == 0)
              return 1;
          return ($v - 1) % 12 + 1;
      }
  
      //Population the first three rows
      populateRow(0, 7, 7);
      populateRow(1, 12, 12);
      populateRow(2, 12, 12);
  
      //Creating the forth row by nested forloops.
      //The first loop iterates over the entries in a row (in your example this would be the letters e.g r1a r1b ect)
      //The second (inner) loop iterates of the rows (in you example this would be the number you had in your variables.)
      //The sum over each of the three rows are added, then bound to 1-12 range, before being added to the forth row.
      for ($cId = 0; $cId &lt; 7; $cId++) {
          $sum = 0;
          for ($rId = 0; $rId &lt; 3; $rId++) {
              $sum += $rows[$rId][$cId];
          }
          array_push($rows[3], bindToRange($sum));
      }
  
      //Same as above, but for the last two remaining values. Should give a total of nine entries in the forth row.
      for ($cId = 7; $cId &lt; 12; $cId++) {
          $sum = 0;
          for ($rId = 1; $rId &lt; 3; $rId++) {
              $sum += $rows[$rId][$cId];
          }
          array_push($rows[3], bindToRange($sum));
      }
  
      function lower_than_2($var){
          return ($var &gt; 1);
      }
      $cssClassName = "match";
  
      // array_count_values will count how many times each value is in the array
      $cssBase = array_count_values($rows[3]);
      // remove from array values that are lower than 2
      $cssBase = array_filter($cssBase, "lower_than_2");
  
      $cssNumber = array();
      $cssCounter = 1;
      // make $cssNumber be a mirror of $cssBase (same keys), but with serial values
      foreach ($cssBase as $key =&gt; $value) {
          $cssNumber[$key] = $cssCounter;
          $cssCounter++;
      }
      unset($cssCounter);
      // ------------------------------------------------
  
      ?&gt;
      &lt;!DOCTYPE HTML&gt;
      &lt;html&gt;
          &lt;head&gt;
              &lt;meta http-equiv="content-type" content="text/html" /&gt;
              &lt;title&gt;Result&lt;/title&gt;
              &lt;link rel="stylesheet" type="text/css" href="./css/style.css" /&gt;
          &lt;/head&gt;
  
          &lt;body&gt;
              Customer Name: &lt;?php echo $cname; ?&gt;&lt;br /&gt;
              DOB: &lt;?php echo $dob_date; ?&gt; / &lt;?php echo $dob_month; ?&gt; / &lt;?php echo $dob_year; ?&gt;&lt;br /&gt;
              &lt;b&gt;&lt;?php echo $age; ?&gt;&lt;/b&gt; Years Old
              &lt;table&gt;
                  &lt;?php
                  //Instead of listing up (hard-coded) I've used nested forloops to generate the html.
                  //The loops are similar to the ones above, but use sizeof keyword to figure out how many iterations it needs.
  
                  $lines = sizeof($rows)+1; // $rows have 4 rows, we will need 1 more
                  for ($rId = 0; $rId &lt; $lines; $rId++) {
                      echo "&lt;tr&gt;\n";
  
                      if($rId &lt; 3){
                          $row = $rows[$rId];
                          $rowSize = sizeof($row);
  
                          for ($cId = 0; $cId &lt; $rowSize; $cId++) {
                              echo "&lt;td&gt;" . $row[$cId] . "&lt;/td&gt;\n";
                          }
                      } else if($rId == 3){
                          $row = $rows[$rId];
                          $rowSize = sizeof($row);
  
                          for ($cId = 0; $cId &lt; $rowSize; $cId++) {
                              echo "&lt;td"; // open td
                              // if the value is in cssBase array, we will apply a css class
                              if(array_key_exists($row[$cId], $cssBase))
                                  echo ' class="'. $cssClassName . $cssNumber[$row[$cId]] .'"';
                              echo "&gt;"; // close td
                              echo $row[$cId];
                              echo "&lt;/td&gt;\n";
                          }
                      } else if($rId == 4){
                          for ($cId = 0; $cId &lt; 12; $cId++) {
                              if($cId == (($age-1)%12)){
                                  echo '&lt;td&gt;'. "$age Years Old" .'&lt;/td&gt;'."\n";
                              } else {
                                  echo "&lt;td&gt;&lt;/td&gt;\n";
                              }
                          }
                      }
                      echo "&lt;/tr&gt;\n";
                  }
  
      // ------------------------------------------------
  
                  ?&gt;
              &lt;/table&gt;&lt;br /&gt;&lt;br /&gt;
              &lt;a href="./" target="_blank" title="Calculate Again"&gt;Calculate Again&lt;/a&gt;
  
          &lt;/body&gt;
      &lt;/html&gt;
  </code></pre>
  
  <p>Is it possible to add a save button on the count.php page and the value will be the one that the user key in earlier in the form page. And the save button will echo "Data Saved" or if exist it will echo "Data Already Existed" whithin the same page. If yes, what is the method i need to use? I'm totally new as i search google, i couldnt find exactly the answer that i need. I already create table in mysql to store the data.</p>
  
  <p>The data that i want to store is the Date of Birth, rowone, rowtwo and row three.
  Create table in mysql with dob as date, rowone, rowtwo and rowthree as varchar (255) and id as int (11).</p>
  
  <p>Here is my insertdata.php</p>
  
  <pre><code>  &lt;?php
  
      $dbhost = "localhost";
  
      $dbuser = "root";
  
      $dbpass = "";
  
      $dbname = "mama";
  
      class dob {
          public function set_dob($dob_date, $dob_month, $dob_year) {
              $this-&gt;dob = $dob_date.'/'.$dob_month.'/'.$dob_year;
          }
      }
  
      $con=mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
      $sql="INSERT INTO profile (dob,rowone,rowtwo,rowthree) VALUES ('$_POST[dob]','$_POST[rowone]','$_POST[rowtwo]','$_POST[rowthree]')";
  
      if (!mysqli_query($con,$sql))
        {
        die('Error: ' . mysqli_error($con));
        }
      echo "1 record added";
  
      /* close connection */
      $mysqli-&gt;close();
      ?&gt;
  </code></pre>
  
  <p>and it shows error as below:
  Notice: Undefined index: dob in C:\xampp\htdocs\mama\inc\insertdata.php on line 18</p>
  
  <p>Notice: Undefined index: rowone in C:\xampp\htdocs\mama\inc\insertdata.php on line 18</p>
  
  <p>Notice: Undefined index: rowtwo in C:\xampp\htdocs\mama\inc\insertdata.php on line 18</p>
  
  <p>Notice: Undefined index: rowthree in C:\xampp\htdocs\mama\inc\insertdata.php on line 18
  1 record added
  Notice: Undefined variable: mysqli in C:\xampp\htdocs\mama\inc\insertdata.php on line 27</p>
  
  <p>Fatal error: Call to a member function close() on a non-object in C:\xampp\htdocs\mama\inc\insertdata.php on line 27</p>