Jump to content

Record Adds Itself when form loads


takeiteasy

Recommended Posts

i'm urgently needing for help here...hope kind people out there can help me with this project.
I'm having this problem here, i duno why when i load my form, it already shows Record in database when i haven even entered my data!
And when i press the Submit button, it just gives me Page Cannot Be displayed.

I know there's something wrong with the code but i can't figure out where is the fault, so can anyone guide me here?
Thanks in advance!

[code]<html>
<head>
<title>..</title>
</head>
<body>

<?  //Connect To The Database
$db_connection = mysql_connect ('localhost','','') or die(mysql_error());
$db_select = mysql_select_db('airticketbooking') or die (mysql_error());
?>

<form method=\"post\" action=\"$_SERVER[PHP_SELF]\">
<td colspan="4" valign="top"><table width="770" border="1" cellspacing="0" cellpadding="1">
      <table width="770" border="0" align="center" cellpadding="0" cellspacing="1">
        <tr>
<tr align="center" bgcolor="#52A6CE">
  <td colspan="2" bgcolor="#52A6CE" class="subhd"><div align="center"><span class="descw"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><font size="4" class="subhd">Add Air Ticket Booking Form</font><font size="4"></font></font></span><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><font class="smallheading" size="4"></font></font></div></td>
</tr>
   <td width="770"><br>
            <table width="90%" border="0" align="center" bgcolor="#F0FAFD"  cellpadding="3" cellspacing="0" class="descf" style="border:1px solid #9FD2EC">
              <tr valign="middle">
              <tr valign="middle" bgcolor="#C6E6F7">
                <td colspan="7" nowrap class="formtext1"><span class="descbig">Employer's Profile</span></td>
              </tr>
             <tr valign="middle" class="descadmin">
                <td width="142" nowrap class="formtext1">Employer's RefId:<span class="textRed"></span></td>
                <td colspan="6" align="left" nowrap><input type="text"  name="empRef" size="10"></td>
              </tr>
              <tr valign="middle" class="descadmin">
                <td width="142" nowrap class="formtext1">Employer's Name:<span class="textRed"></span></td>
                <td colspan="6" align="left" nowrap><input type="text"  name="empName" size="30"></td>
              </tr>
          <tr valign="middle" class="descadmin">
                <td width="142" nowrap class="formtext1">Worker's Name:<span class="textRed"></span></td>
                <td colspan="6" align="left" nowrap><input type="text"  name="workerName" size="30"></td>
              </tr>
<table width="770" border="0" align="center" cellpadding="0" cellspacing="1">
        <tr>
          <td>
        <table width="90%" border="0" align="center" cellpadding="3" cellspacing="0" bgcolor="#F0FAFD" class="tableborder">
              <tr bgcolor="#C6E6F7">
                <td colspan="7" nowrap ><span class="descbig">Air Ticket Information</span></td>
            </tr>
<tr valign="middle" class="descadmin">
                <td width="142" height="27" nowrap class="formtext1">Type of Ticket:<span class="textRed"></span></td>
                <td height="27" colspan="6" align="left" nowrap><input type="radio"  name="ticketType" value="One Way">One       <input type="radio" name ="ticketType" value="Return">Return       <input type="radio" name ="ticketType" value="PDA">PDA</td>
              </tr>
<tr valign="middle" class="descadmin">
                <td width="142" nowrap class="formtext1">Flight No.<span class="textRed"></span></td>
                <td colspan="6" align="left" nowrap><input type="text"  name="flightNo" size="10"></td>
              </tr>
<tr valign="middle" class="descadmin">
                <td width="142" nowrap class="formtext1">Airline Company:<span class="textRed"></span></td>
                <td colspan="6" align="left" nowrap><input type="text"  name="airCompany" size="30"></td>
              </tr>
<tr valign="middle" class="descadmin">
                <td width="142" nowrap class="formtext1">Destination:<span class="textRed"></span></td>
                <td colspan="6" align="left" nowrap><input type="text"  name="destination" size="10"></td>
              </tr>
<tr valign="middle" class="descadmin">
                <td width="142" nowrap class="formtext1">Time:<span class="textRed"></span></td>
                <td colspan="6" align="left" nowrap><input type="text"  name="time" size="10" maxlength="10"> </td>
              </tr>
<tr valign="middle" class="descadmin">
                <td width="142" nowrap class="formtext1">Date Of Departure:<span class="textRed"></span></td>
                <td colspan="6" align="left" nowrap><input type="text"  name="DOD" size="10" maxlength="10"> </td>
              </tr>
<tr valign="middle" class="descadmin">
                <td width="142" nowrap class="formtext1">Date Of Return:<span class="textRed"></span></td>
                <td colspan="6" align="left" nowrap><input type="text"  name="DOR" size="10" maxlength="10"> </td>
              </tr>
<br>
   <table width="770" border="0" align="center" cellpadding="0" cellspacing="1">
        <tr>
          <td><br>
        <table width="90%" border="0" align="center" cellpadding="3" cellspacing="0" bgcolor="#F0FAFD" class="tableborder">
              <tr bgcolor="#C6E6F7">
                <td colspan="7" nowrap ><span class="descbig">Air Ticket Price</span></td>
            </tr>
<tr valign="middle" class="descadmin">
                <td width="142" nowrap class="formtext1">Quotes:<span class="textRed"></span></td>
                <td colspan="6" align="left" nowrap>SG$ <input type="text"  name="quotes" size="10"></td>
              </tr>

<tr valign="middle" class="descadmin">
                <td width="142" nowrap class="formtext1">Amt. Paid:<span class="textRed"></span></td>
                <td colspan="6" align="left" nowrap>SG$ <input type="text"  name="amtPaid" size="10"></td>
              </tr>
<tr valign="middle" class="descadmin">
                <td width="142" height="27" nowrap class="formtext1">Payment Mode:<span class="textRed"></span></td>
                <td height="27" colspan="6" align="left" nowrap><input type="radio"  name="paymentMode" value="Cash">Cash       <input type="radio" name ="paymentMode" value="Nets">Nets       <input type="radio" name ="paymentMode" value="Cheque">Cheque No: <input type="text"  name="chequeNo" size="7"></td>
              </tr>
<tr valign="middle" class="descadmin">
                <td width="142" nowrap class="formtext1">Receipt No:<span class="textRed"></span></td>
                <td colspan="6" align="left" nowrap><input type="text"  name="receiptNo" size="10"></td>
              </tr>
<tr valign="middle" class="descadmin">
                <td width="142" nowrap class="formtext1">Agent Quote:<span class="textRed"></span></td>
                <td colspan="6" align="left" nowrap>SG$ <input type="text"  name="agentQuote" size="10"></td>
              </tr>
<tr valign="middle" class="descadmin">
                <td width="142" nowrap class="formtext1">Payment To Agent:<span class="textRed"></span></td>
                <td colspan="6" align="left" nowrap>SG$ <input type="text"  name="payAgent" size="10"></td>
              </tr>
   <table width="770" border="0" align="center" cellpadding="0" cellspacing="1">
        <tr>
          <td><br>
        <table width="90%" border="0" align="center" cellpadding="3" cellspacing="0" bgcolor="#F0FAFD" class="tableborder">
              <tr bgcolor="#C6E6F7">
                <td colspan="7" nowrap ><span class="descbig">Staff Fill-In</span></td>
            </tr>
<tr valign="middle" class="descadmin">
                <td width="142" nowrap class="formtext1">Arranged Date:<span class="textRed"></span></td>
                <td colspan="6" align="left" nowrap><input type="text"  name="AD" size="10">
            </tr>
<tr valign="middle" class="descadmin">
                <td width="142" nowrap class="formtext1">Remarks:<span class="textRed"></span></td>
                <td colspan="6" align="left" nowrap><textarea name="remarks"></textarea></td>
              </tr>

    
   <tr valign="middle" class="descd">
                <td nowrap class="formtext1"> </td>
                <td colspan="2" nowrap class="text1"> </td>
        <input type="hidden" name="op" value="ds">
                <td nowrap class="formtext1"><input type="submit" name="submit" value="Submit">    <input name="reset" type="reset" class="button" value="Reset "></td>
                <td colspan="3" nowrap> </td>

              </tr>
      </table>
  </form>

<?

if ($submit != "no")
{
mysql_query("INSERT INTO airticketbooking (empRef, empName, workerName, flightNo, airCompany, ticketType, destination, time, DOD, DOR, receiptNo, quotes, amtPaid, paymentMode, chequeNo, AD,agentQuote, payAgent, remarks) VALUES ('$_POST[empRef]','$_POST[empName]','$_POST[workerName]','$_POST[flightNo]','$_POST[airCompany]','$_POST[ticketType]','$_POST[destination]','$_POST[time]', '$_POST[DOD]','$_POST[DOR]','$_POST[receiptNo]','$_POST[quotes]','$_POST[amtPaid]','$_POST[paymentMode]','$_POST[chequeNo]','$_POST[AD]','$_POST[agentQuote]','$_POST[payAgent]','$_POST[remarks]')",$db_connection) or die(mysql_error());
mysql_close($db_connection);

echo "Record Added In Database";
}
else if ($submit == "no")
{
echo "Record Not Added";
  }

?>



</body>
</html>



[/code]
Link to comment
Share on other sites

i have tried, but i still can't the problem still persist.
But aniway, thanks for helping!

what i need is, when i pressed the Submit button, then the data entered is insert into MySQL...but i can't seem to do that...as everytime i pressed the Submit button, the page could not be displayed error came out.
Link to comment
Share on other sites

Firstly I would suggest you not to use registered globals.

A better solution for your problem would be

[code]
if (isset($_POST['submit']) && $_POST['submit'] == "Submit"))
{
    //Run the insert Query
    //You can add some ideas fo your own to display the relevan messages after insertion.
}

#Forget the else part.
#Let the form be displayed by default
[/code]

This is very bad on your part

Check your line # 8 which says

<form method=\"post\" action=\"$_SERVER[PHP_SELF]\">

well it should be

<form method="post" action="<? echo $_SERVER[PHP_SELF]; ?>">

Note: You have not enclosed the PHP variables in PHP tags.

Check it out .....

Let me know if it works after the change
Link to comment
Share on other sites

1st of all thank you so much for your help Tux User!
The solution for
<form method="post" action="<? echo $_SERVER[PHP_SELF]; ?>">

is really helpful! now when i pressed Submit button, the page doesn't give page could not be displayed error.

But the problem of Record Added In Database still appears when i tried to load the page.

I know the code is some sort written like when the page loads,immediately insert data in MySQL.

But what i want is when i have typed all the data, press the Submit button, den the data is inserted in MySQL.

But i don't know how the code should be like...so hope everyone can help me with this...

Thanks so much!

here is the edited code:
[code]
<html>
<head>
<title></title>
</head>
<body>

<? //Connect To The Database
$db_connection = mysql_connect ('localhost','','') or die(mysql_error());
$db_select = mysql_select_db('airticketbooking') or die (mysql_error());

if ((isset($_POST['submit']) && $_POST['submit'] == "Submit"))
{
mysql_query("INSERT INTO airticketbooking (empRef, empName, workerName, flightNo, airCompany, ticketType, destination, time, DOD, DOR, receiptNo, quotes, amtPaid, paymentMode, chequeNo, AD,agentQuote, payAgent, remarks) VALUES ('$_POST[empRef]','$_POST[empName]','$_POST[workerName]','$_POST[flightNo]','$_POST[airCompany]','$_POST[ticketType]','$_POST[destination]','$_POST[time]', '$_POST[DOD]','$_POST[DOR]','$_POST[receiptNo]','$_POST[quotes]','$_POST[amtPaid]','$_POST[paymentMode]','$_POST[chequeNo]','$_POST[AD]','$_POST[agentQuote]','$_POST[payAgent]','$_POST[remarks]')",$db_connection) or die(mysql_error());
mysql_close($db_connection);

echo "Record Added In Database";
}


?>


<form method="post" action="<? echo $_SERVER[PHP_SELF]; ?>">

<td colspan="4" valign="top"><table width="770" border="1" cellspacing="0" cellpadding="1">
<table width="770" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<tr align="center" bgcolor="#52A6CE">
<td colspan="2" bgcolor="#52A6CE" class="subhd"><div align="center"><span class="descw"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><font size="4" class="subhd">Add Air Ticket Booking Form</font><font size="4"></font></font></span><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><font class="smallheading" size="4"></font></font></div></td>
</tr>
<td width="770"><br>
<table width="90%" border="0" align="center" bgcolor="#F0FAFD" cellpadding="3" cellspacing="0" class="descf" style="border:1px solid #9FD2EC">
<tr valign="middle">
<tr valign="middle" bgcolor="#C6E6F7">
<td colspan="7" nowrap class="formtext1"><span class="descbig">Employer's Profile</span></td>
</tr>
<tr valign="middle" class="descadmin">
<td width="142" nowrap class="formtext1">Employer's RefId:<span class="textRed"></span></td>
<td colspan="6" align="left" nowrap><input type="text" name="empRef" size="10"></td>
</tr>
<tr valign="middle" class="descadmin">
<td width="142" nowrap class="formtext1">Employer's Name:<span class="textRed"></span></td>
<td colspan="6" align="left" nowrap><input type="text" name="empName" size="30"></td>
</tr>
<tr valign="middle" class="descadmin">
<td width="142" nowrap class="formtext1">Worker's Name:<span class="textRed"></span></td>
<td colspan="6" align="left" nowrap><input type="text" name="workerName" size="30"></td>
</tr>
<table width="770" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td>
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="0" bgcolor="#F0FAFD" class="tableborder">
<tr bgcolor="#C6E6F7">
<td colspan="7" nowrap ><span class="descbig">Air Ticket Information</span></td>
</tr>
<tr valign="middle" class="descadmin">
<td width="142" height="27" nowrap class="formtext1">Type of Ticket:<span class="textRed"></span></td>
<td height="27" colspan="6" align="left" nowrap><input type="radio" name="ticketType" value="One Way">One <input type="radio" name ="ticketType" value="Return">Return <input type="radio" name ="ticketType" value="PDA">PDA</td>
</tr>
<tr valign="middle" class="descadmin">
<td width="142" nowrap class="formtext1">Flight No.<span class="textRed"></span></td>
<td colspan="6" align="left" nowrap><input type="text" name="flightNo" size="10"></td>
</tr>
<tr valign="middle" class="descadmin">
<td width="142" nowrap class="formtext1">Airline Company:<span class="textRed"></span></td>
<td colspan="6" align="left" nowrap><input type="text" name="airCompany" size="30"></td>
</tr>
<tr valign="middle" class="descadmin">
<td width="142" nowrap class="formtext1">Destination:<span class="textRed"></span></td>
<td colspan="6" align="left" nowrap><input type="text" name="destination" size="10"></td>
</tr>
<tr valign="middle" class="descadmin">
<td width="142" nowrap class="formtext1">Time:<span class="textRed"></span></td>
<td colspan="6" align="left" nowrap><input type="text" name="time" size="10" maxlength="10"> </td>
</tr>
<tr valign="middle" class="descadmin">
<td width="142" nowrap class="formtext1">Date Of Departure:<span class="textRed"></span></td>
<td colspan="6" align="left" nowrap><input type="text" name="DOD" size="10" maxlength="10"> </td>
</tr>
<tr valign="middle" class="descadmin">
<td width="142" nowrap class="formtext1">Date Of Return:<span class="textRed"></span></td>
<td colspan="6" align="left" nowrap><input type="text" name="DOR" size="10" maxlength="10"> </td>
</tr>
<br>
<table width="770" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td><br>
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="0" bgcolor="#F0FAFD" class="tableborder">
<tr bgcolor="#C6E6F7">
<td colspan="7" nowrap ><span class="descbig">Air Ticket Price</span></td>
</tr>
<tr valign="middle" class="descadmin">
<td width="142" nowrap class="formtext1">Quotes:<span class="textRed"></span></td>
<td colspan="6" align="left" nowrap>SG$ <input type="text" name="quotes" size="10"></td>
</tr>

<tr valign="middle" class="descadmin">
<td width="142" nowrap class="formtext1">Amt. Paid:<span class="textRed"></span></td>
<td colspan="6" align="left" nowrap>SG$ <input type="text" name="amtPaid" size="10"></td>
</tr>
<tr valign="middle" class="descadmin">
<td width="142" height="27" nowrap class="formtext1">Payment Mode:<span class="textRed"></span></td>
<td height="27" colspan="6" align="left" nowrap><input type="radio" name="paymentMode" value="Cash">Cash <input type="radio" name ="paymentMode" value="Nets">Nets <input type="radio" name ="paymentMode" value="Cheque">Cheque No: <input type="text" name="chequeNo" size="7"></td>
</tr>
<tr valign="middle" class="descadmin">
<td width="142" nowrap class="formtext1">Receipt No:<span class="textRed"></span></td>
<td colspan="6" align="left" nowrap><input type="text" name="receiptNo" size="10"></td>
</tr>
<tr valign="middle" class="descadmin">
<td width="142" nowrap class="formtext1">Agent Quote:<span class="textRed"></span></td>
<td colspan="6" align="left" nowrap>SG$ <input type="text" name="agentQuote" size="10"></td>
</tr>
<tr valign="middle" class="descadmin">
<td width="142" nowrap class="formtext1">Payment To Agent:<span class="textRed"></span></td>
<td colspan="6" align="left" nowrap>SG$ <input type="text" name="payAgent" size="10"></td>
</tr>
<table width="770" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td><br>
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="0" bgcolor="#F0FAFD" class="tableborder">
<tr bgcolor="#C6E6F7">
<td colspan="7" nowrap ><span class="descbig">Staff Fill-In</span></td>
</tr>
<tr valign="middle" class="descadmin">
<td width="142" nowrap class="formtext1">Arranged Date:<span class="textRed"></span></td>
<td colspan="6" align="left" nowrap><input type="text" name="AD" size="10">
</tr>
<tr valign="middle" class="descadmin">
<td width="142" nowrap class="formtext1">Remarks:<span class="textRed"></span></td>
<td colspan="6" align="left" nowrap><textarea name="remarks"></textarea></td>
</tr>
<tr valign="middle" class="descd">
<td nowrap class="formtext1"></td>
<td colspan="2" nowrap class="text1"></td>
<td nowrap class="formtext1"><input type="submit" name="submit" value="Submit"> <input name="reset" type="reset" class="button" value="Reset "></td>
<td colspan="3" nowrap></td>

</tr>
</table>
</form>

</body>
</html>

[/code]
Link to comment
Share on other sites

This is how your code should look.
I have rewritten few top lines of your code.

[code]
<?
#initialise variable
$temp_flag   = FALSE;

if ((isset($_POST['submit']) && $_POST['submit'] == "Submit"))
{
    #your PHP code here to insert into the database
    
    #After inerting the values set the flag to true
    $temp_flag  = TRUE;
}
?>
<html>
<head>
<title></title>
</head>
<body>

<form method="post" action="<? echo $_SERVER[PHP_SELF]; ?>">

<td colspan="4" valign="top"><table width="770" border="1" cellspacing="0" cellpadding="1">
<table width="770" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<tr align="center" bgcolor="#52A6CE">

</tr>
<td width="770">
<?
#check if the flag is set, IF yes then display the message(dont write an else part)
if (isset($temp_flag) && $temp_flag == TRUE)
{
    print ("<center>Added sucessfully</center>");
}
?>
<table width="90%" border="0" align="center" bgcolor="#F0FAFD" cellpadding="3" cellspacing="0" class="descf" style="border:1px solid #9FD2EC">
<tr valign="middle">
[/code]

Hope this helps
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.