No data inserted into database. Am I wrongdoing it?

Discussion in 'PHP' started by sally89, Mar 29, 2010.

  1. #1
    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>
    
    
    Code (markup):
    any ideas is really appreciated
     
    sally89, Mar 29, 2010 IP
  2. meloncreative

    meloncreative Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    it seems that the insert is running when you run the script the first time, ie when you load the form.

    you need a condition to see if the script has been submitted or not

    
    if ($_POST['submitted']) {
       // do stuff with database
    }
    else {
       // Output form
    }
    
    Code (markup):
    Also you need to validate your form, ie check fields are not blank before you submit, and also use mysql_escape_string() to protect against injection
     
    meloncreative, Mar 29, 2010 IP
  3. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #3
    your php code to insert data should be in BookingForm.php
     
    javaongsan, Mar 29, 2010 IP
  4. moneycms

    moneycms Peon

    Messages:
    265
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Make sure you have entered the correct name value pairs in your php script.
     
    moneycms, Mar 29, 2010 IP