No data inserted into database. Am I wrongdoing it?

I have a form named:“RegistrationForm.php”. when i click the “submit button”, there’s a data inserted in the database (the prove is, there a new row in the database),
but there’s no value from user textfield(ic_no,name,email…) that have been inserted.
It means like there’s a row of data inserted but without value…

p/s- however, when i click the submit button, it successfully directed me to the “BookingForm.php” with all the session value…it’s just that there’s no data inserted into the database.

can someone straighten this up for me? am i doing wrong with the coding with the submit button?
here’s the code



<?php
session_start();
$_SESSION['ic_no']=$ic_no;
$_SESSION['name']=$name;
$_SESSION['address']=$address;
$_SESSION['tel_no']=$tel_no;
$_SESSION['email']=$email;
?>


<html>
<body>

<form action="BookingForm.php" method="post">
  <p><strong>REGISTRATION FORM</strong></p>
  <table width="285" border="1">
    <tr>
      <th width="120" scope="row">Ic No :</th>
      <td width="149"><label>
        <input type="text" name="ic_no" id="ic_no" value="<?php $ic_no; ?>">
      </label></td>
    </tr>
    <tr>
      <th scope="row">Name :</th>
      <td><label>
        <input type="text" name="name" id="name" value="<?php $name; ?>">
      </label></td>
    </tr>
    <tr>
      <th scope="row">Address :</th>
      <td><label>
        <input type="text" name="address" id="address" value="<?php $address; ?>" >
      </label></td>
    </tr>
    <tr>
      <th scope="row">Contact No :</th>
      <td><label>
        <input type="text" name="tel_no" id="tel_no" value="<?php $tel_no; ?>">
      </label></td>
    </tr>
    <tr>
      <th scope="row">Email :</th>
      <td><label>
        <input type="text" name="email" id="email"  value="<?php $email; ?>">
      </label></td>
    </tr>
  </table>
  <input type="submit" name="submit" id="submit" value="Submit">
  <?php
$con = mysql_connect("localhost","root","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("ambos", $con);

mysql_query("INSERT INTO customer (ic_no, name, address, tel_no, email)
VALUES ('$_POST[ic_no]', '$_POST[name]', '$_POST[address]','$_POST[tel_no]','$_POST[email]')");
mysql_close($con);

</form>
 </body>
</html>


any ideas is really appreciated

Hello silvery,

Try to echo your values, if the values are coming from different page then check (by echoing) whether you are getting those value or not.

Try the following query and echo it first so you ll come to know that which values your are missing.

mysql_query("INSERT INTO customer (ic_no, name, address, tel_no, email)
VALUES ('$ic_no', '$name', '$address','$tel_no','$email')");

Contrary to what you might think, the code that inserts the data in the database is not executed after the form has been submitted. Even though you put that code at the end of the page, after the form code, it is executed before the form is shown in your browser.
This is because all the php code is executed server side and the resulting HTML page is sent to your browser.

So, the empty row inserted in your database is not inserted after you submit the form, but even before the form is shown to you. And of course, the form fields have no value.

Try this code (read the comments):


<?php
session_start();

// I didn't eliminate these lines, but since $ic_no and the other variables 
// have no value at this point, it makes no sense storing them in the session.
$_SESSION['ic_no']=$ic_no;
$_SESSION['name']=$name;
$_SESSION['address']=$address;
$_SESSION['tel_no']=$tel_no;
$_SESSION['email']=$email;

// Check if the form has been submitted. 
// If so, store the info in the database.
// Remember to check all user input before using it in a query to avoid sql injection!
if (isset($_POST['submit']) {
  $con = mysql_connect("localhost", "root", "password");
  if (!$con) {
    die('Could not connect: ' . mysql_error());
  }
  mysql_select_db("ambos", $con);

  $query = "
    INSERT INTO customer (ic_no, name, address, tel_no, email)
    VALUES (
        '" . mysql_real_escape_string($_POST['ic_no']) . "'
      , '" . mysql_real_escape_string($_POST['name']) . "'
      , '" . mysql_real_escape_string($_POST['address']) . "'
      , '" . mysql_real_escape_string($_POST['tel_no']) . "'
      , '" . mysql_real_escape_string($_POST['email']) . "'
    )
  ";
  mysql_query($query) or die('sql error: ' . mysql_error() . ' in query: ' . $query);

  mysql_close($con);
}
?>
<html>
<body>

<form action="BookingForm.php" method="post">
  <p><strong>REGISTRATION FORM</strong></p>
  <table width="285" border="1">
    <tr>
      <th width="120" scope="row">Ic No :</th>
      <td width="149"><label>
        <input type="text" name="ic_no" id="ic_no" value="<?php $ic_no; ?>">
      </label></td>
    </tr>
    <tr>
      <th scope="row">Name :</th>
      <td><label>
        <input type="text" name="name" id="name" value="<?php $name; ?>">
      </label></td>
    </tr>
    <tr>
      <th scope="row">Address :</th>
      <td><label>
        <input type="text" name="address" id="address" value="<?php $address; ?>" >
      </label></td>
    </tr>
    <tr>
      <th scope="row">Contact No :</th>
      <td><label>
        <input type="text" name="tel_no" id="tel_no" value="<?php $tel_no; ?>">
      </label></td>
    </tr>
    <tr>
      <th scope="row">Email :</th>
      <td><label>
        <input type="text" name="email" id="email"  value="<?php $email; ?>">
      </label></td>
    </tr>
  </table>
  <input type="submit" name="submit" id="submit" value="Submit">
  <?php

</form>
 </body>
</html>