MySQL的HTML表单输入

时间:2015-09-25 02:43:39

标签: php html mysql

我有一个名为letter的MySQL表,希望通过HTML数据输入表单插入记录。代码如下:

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

但我无法通过此表单将记录添加到相关表格中。我无法理解我的错误。任何人都可以帮助我吗?...请...

1 个答案:

答案 0 :(得分:2)

表单的属性方法的默认值为获取 你应该像这样指定它

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

现在你可以使用$ _POST获取数据了!