0

I have a MySQL table named as letter and wish to insert records through the HTML data input form. Code as follows:

<head>
<meta charset="utf-8">
    <link href="css/jquery-ui-1.10.1.css" rel="stylesheet">
    <script src="js/jquery-1.9.1.js"></script>
    <script src="js/jquery-ui-1.10.1.min.js"></script>
    <script>
       $(function() {
          $( "#datepicker" ).datepicker(({ dateFormat: "yy-mm-dd" }));
       });

</script>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Add Letter</title>
<style type="text/css" media="screen">
    @import "style_contactform.css";
</style>
</head>    

<?php
$querymethod = "select r_method_code, r_method from r_method order by r_method";
$resultmethod = mysql_query($querymethod) or die ( mysql_error());

$querybranch = "select bcode, branch from branch order by branch";
$resultbranch = mysql_query($querybranch) or die ( mysql_error());

$querytype = "select tcode, type from type order by type";
$resulttype = mysql_query($querytype) or die ( mysql_error());

?>       

<?php

if  (isset($_POST["submit"]))
{

$rno =$_POST["rno"];  
$lno =$_POST["lno"]; 
$dol = mysql_real_escape_string($_POST["doi"]);
$hdg =$_POST["hdg"];   
$from =$_POST["from"];       
$address =$_POST["address"];
$method =$_POST["method"];
$type =$_POST["type"];
$branch =$_POST["branch"];

if ((empty($hdg))){
echo '<script language="javascript">';
echo 'alert("All fields must be required")';
echo '</script>';
}
else
{
$query ="INSERT INTO letter (reference_no, letter_no, date_stamp, heading, from_1, address, r_method_code, tcode, bcode) VALUES ('$rno', '$lno', '$dst', '$hdg', '$from', '$address', '$method', '$type', '$branch')";
   $result = mysql_query($query) or die ( mysql_error());
   $rc = mysql_affected_rows();
   echo '<script language="javascript">';
   echo 'alert("Added Successfully")';
   echo '</script>';
}
}
?>
<html>

<form id="contactform">

<div class="formcolumn">

<label for="rno">Reference No:</label>
<input type="text" name="rno" />

<label for="lno">Letter No:</label>
<input type="text" name="lno" />

<label for="dst">Date of the Letter:</label>
<input type="text" name="dst" id="datepicker" />

<label for="hdg">Heading:</label>
<textarea name="hdg"></textarea>

</div>

<div class="formcolumn">

<label for="from">From 1:</label>
<input type="text" id="from" />

<label for="address">Address:</label>
<textarea id="address"></textarea>

<label for="method">Received Method:</label>
<select name="method" span  class="al">
 <?php
do { 
?>
<option value="<?php echo $rowmethod['r_method_code']?>"><?php echo $rowmethod['r_method']?></option>
 <?php
} while ($rowmethod = mysql_fetch_assoc($resultmethod));
?> 
</select>

<label for="type">Type:</label>
<select name="type" span  class="al">
 <?php
do { 
?>
<option value="<?php echo $rowtype['tcode']?>"><?php echo $rowtype['type']?></option>
 <?php
} while ($rowtype = mysql_fetch_assoc($resulttype));
?> 
</select>

<label for="branch">Branch:</label>
<select name="branch" span  class="al">
 <?php
do { 
?>
<option value="<?php echo $rowbranch['bcode']?>"><?php echo $rowbranch['branch']?></option>
 <?php
} while ($rowbranch = mysql_fetch_assoc($resultbranch));
?> 
</select>

</div>

<div class="buttons">
<input class="button" type="submit" value="Submit!" />

</div>
</form>
</html>

But I was unable to add records through this form to the relevant table. I can not understand what I am going wrong. Can any one help me?... Pls...

3
  • 1
    Are you getting an error or are the records just not being inserted into the database? Commented Sep 25, 2015 at 2:45
  • 1
    @ StillLearnin. the records just not being inserted into the database Commented Sep 25, 2015 at 2:46
  • 1
    The first step to understanding is to format your code. I for one, can't even spot an insert statement in it. Commented Sep 25, 2015 at 2:49

1 Answer 1

2

Form's Attribute method's default value is get You should specify it like this

<form id="contactform" method="post">

Now you can use $_POST to get data!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.