Jump to content

No data inserted into database. Am I wrongdoing it?


pollysal

Recommended Posts

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();
?>

<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);

$_SESSION['ic_no']=$ic_no;
$_SESSION['name']=$name;
$_SESSION['address']=$address;
$_SESSION['tel_no']=$tel_no;
$_SESSION['email']=$email;
?>
</form>

</body>
</html>

 

Link to comment
Share on other sites

changed ur code a little  bit try this one..

 

<?php
session_start();
?>

<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
  if(isset($_POST['submit']))
  {
  $ic_no=$_POST['ic_no'];
  $name=$_POST['name'];
  $address=$_POST['address'];
  $tel_no=$_POST['tel_no'];
  $email=$_POST['email'];
$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 ('$ic_no', '$name', '$address','$tel_no','$email')");

mysql_close($con);

$_SESSION['ic_no']=$ic_no;
$_SESSION['name']=$name;
$_SESSION['address']=$address;
$_SESSION['tel_no']=$tel_no;
$_SESSION['email']=$email;
echo "Post Values=> ";
print_r($_POST);
echo "<br>Session Values=> ";
print_r($_SESSION);
}
?>
</form>

</body>
</html>

 

Link to comment
Share on other sites

perhaps being an 'old fart' I just am missing something; however...

 

the form page is "RegistrationForm.php" which POSTS to "BookingForm.php".  Since the page is not posting to itself, storing the values to the database should be done in "BookingForm.php"?

 

Make sense?

Link to comment
Share on other sites

thanks to those who reply.

 

ym_chaitu, i did use the code you give as reference, but it still not work out. Plus, it seems that it doesn't even insert any record into the database.

 

do actually a button can only make a function. I mean that, one button for submit data to the database, and one button to go to the next page?

 

this question might sound stupid, but i'm relatively just learn. so, if anyone please straighten me out if i'm wrong..

 

Link to comment
Share on other sites

Your original problem is because the php code you have placed in your form is executed unconditionally when the form is displayed. this is what inserts a blank row.

 

When you submit that form, either your code in BookingForm.php is setting the $_SESSION variables or register_globals are on and the $_POST variables with the same name as the $_SESSION variables are automatically setting the $_SESSION variables (which is why register_globals where turned off by default a really long time ago to prevent hacker from being able to set $_SESSION variables.)

 

Since you are using two different pages for the form and the form processing code, the php code that you have in the form page should be removed. The php code in the form processing page is what needs to perform the INSERT query.

Link to comment
Share on other sites

thanks to those who reply.

 

ym_chaitu, i did use the code you give as reference, but it still not work out. Plus, it seems that it doesn't even insert any record into the database.

 

do actually a button can only make a function. I mean that, one button for submit data to the database, and one button to go to the next page?

 

this question might sound stupid, but i'm relatively just learn. so, if anyone please straighten me out if i'm wrong..

 

 

if u want to submit the values and redirect to other page u can do some thing like this..

1.html

<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">
</form>
</body>
</html>

 

and the php file which is used to store values should be a separate one.

BookingForm.php

<?php
  if(isset($_POST['submit']))
  {
  $ic_no=$_POST['ic_no'];
  $name=$_POST['name'];
  $address=$_POST['address'];
  $tel_no=$_POST['tel_no'];
  $email=$_POST['email'];
$con = mysql_connect("localhost","root","admin");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("develop", $con);

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

mysql_close($con);

echo $_SESSION['ic_no']=$ic_no;
$_SESSION['name']=$name;
$_SESSION['address']=$address;
$_SESSION['tel_no']=$tel_no;
$_SESSION['email']=$email;
echo "<br>Post Values=> ";
print_r($_POST);
echo "<br>Session Values=> ";
print_r($_SESSION);
}
?>

 

 

this way u are using the one form for getting values and the other one for posting values

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.