尝试使用if语句将数据发布到数据库中

时间:2015-02-12 21:40:53

标签: php html sql-server pdo

当我在第二行Department2上提交信息时,我有一个问题,它插入空白数据,不确定我是否正确地使用了if语句?我试图让Department2,areacode2,offtime2,gytime2,ftime2,comments2和ini2在填充时插入。

insert.php

<?php

$serverName = "10.16.160.157";

$db= new PDO( "sqlsrv:server=$serverName ; Database=lightingschedule", "", "");
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );



$stmt= $db->prepare("INSERT INTO tbl_lighting(Department, 
    areaCode, offtime, gytime, ftime, ini, 
    sat_ob_department, sat_ib_department,
    sat_ob_onTime,sat_ib_onTime,
    sat_ob_offTime, sat_ib_offTime,
    ob_signature,ib_signature, 
    deptkey, comments,ib_comments,ob_comments,Requestdate

 ) Values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");

$stmt->bindParam(1, $_POST["Department"]);
$stmt->bindParam(2, $_POST["areaCode"]);
$stmt->bindParam(3, $_POST["offtime"]);
$stmt->bindParam(4, $_POST["gytime"]);
$stmt->bindParam(5, $_POST["ftime"]);
$stmt->bindParam(6, $_POST["ini"]);
$stmt->bindParam(7, $_POST["sat_ob_department"]);
$stmt->bindParam(8, $_POST["sat_ib_department"]);
$stmt->bindParam(9, $_POST["sat_ob_onTime"]);
$stmt->bindParam(10, $_POST["sat_ib_onTime"]);
$stmt->bindParam(11, $_POST["sat_ob_offTime"]);
$stmt->bindParam(12, $_POST["sat_ib_offTime"]);
$stmt->bindParam(13, $_POST["ob_signature"]);
$stmt->bindParam(14, $_POST["ib_signature"]);
$stmt->bindParam(15, $_POST["deptkey"]);
$stmt->bindParam(16, $_POST["comments"]);
$stmt->bindParam(17, $_POST["ib_comments"]);
$stmt->bindParam(18, $_POST["ob_comments"]);
$stmt->bindParam(19, $_POST["Requestdate"]);




$stmt->execute();

$Department2 = trim( $_POST['Department2'] );

if($Department2 =='Department2'){

    $stmt= $db->prepare("INSERT INTO tbl_lighting(Department, 
    areaCode, offtime, gytime, ftime, ini, 
    sat_ob_department, sat_ib_department,
    sat_ob_onTime,sat_ib_onTime,
    sat_ob_offTime, sat_ib_offTime,
    ob_signature,ib_signature, 
    deptkey, comments,ib_comments,ob_comments,Requestdate

 ) Values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");

$stmt->bindParam(1, $_POST["Department2"]);
$stmt->bindParam(2, $_POST["areaCode2"]);
$stmt->bindParam(3, $_POST["offtime1"]);
$stmt->bindParam(4, $_POST["gytime2"]);
$stmt->bindParam(5, $_POST["ftime2"]);
$stmt->bindParam(6, $_POST["ini2"]);
$stmt->bindParam(7, $_POST["sat_ob_department"]);
$stmt->bindParam(8, $_POST["sat_ib_department"]);
$stmt->bindParam(9, $_POST["sat_ob_onTime"]);
$stmt->bindParam(10, $_POST["sat_ib_onTime"]);
$stmt->bindParam(11, $_POST["sat_ob_offTime"]);
$stmt->bindParam(12, $_POST["sat_ib_offTime"]);
$stmt->bindParam(13, $_POST["ob_signature"]);
$stmt->bindParam(14, $_POST["ib_signature"]);
$stmt->bindParam(15, $_POST["deptkey2"]);
$stmt->bindParam(16, $_POST["comments2"]);
$stmt->bindParam(17, $_POST["ib_comments"]);
$stmt->bindParam(18, $_POST["ob_comments"]);
$stmt->bindParam(19, $_POST["Requestdate"]);

$stmt->execute();
}
?>

<h1 align="center">Successfully Entered!</h1>

create.php

    <?php include("includes/header.php"); ?>
<!DOCTYPE>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="en-us" http-equiv="Content-Language" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Create Form</title>
<script type="text/javascript" src="Javascript/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="Javascript/autocompleteAreaCode.js"></script>
<script type="text/javascript" src="Javascript/autocompletedeptkey.js"></script>
<script type="text/javascript" src="Javascript/validator.js"></script>
<link href="css/default.css" rel="stylesheet" type="text/css" >



</head>

<body style="background-image: url('images/background.jpg')">

<form  action = "insert_process.php" onsubmit="return validateForm()" name="form" id="form"  method ="post" class="style1" style="width: 1125px; height: 800px; margin-top: 30px; margin-left:0px;">

<table align="center" id="tfhover" class="tftable" border="1">
<br><br>
<tr>
<td colspan="7"><h1 align="center">Lighting Schedule Form</h1></td>
</tr>

<tr>
<th>Department</th><th style="width: 75px">Area Code</th><th style="width: 144px">Off Time</th><th>Grave Yard On Time</th><th>First Shift OnTime</th><th width="125px">Comments Or Date By</th><th>Initials</th></tr>

<tr>

    <!--First row accross on the table-->
<td><select name="Department" id="Department" >
            <option value ="">Please select ...</option>
            <option value ="Upstairs Hang East">Upstairs Hang East</option>
            <option value ="Upstairs Hang West">Upstairs Hang West</option>
            <option value ="RDR">RDR</option>
            <option value ="Hang Sortation">Hang Sortation</option>
            <option value ="RACK WAREHOUSE">RACK WAREHOUSE</option>
            <option value ="Hang To Flat">HANG TO FLAT</option>
            <option value ="RACK SHOES">RACK SHOES</option>
            <option value ="FLAT 2">FLAT 2</option>
            <option value ="FLAT 1">FLAT 1</option>
            <option value ="Claims">Claims</option>
            <option value ="INBOUND">INBOUND</option>
            <option value ="OUTBOUND">OUTBOUND</option>
            </select></td>
<td style="width: 75px"><input id="areaCode" name="areaCode" onkeydown="return false;" type="text"/></td>
<td style="width: 144px"><input class="offtime" id="offtime" name="time"  type="text" /></td>
<td><input id="gytime" name="gystime" type="text" /></td>
<td><input id="ftime" name="ftime" type="text" /></td>
<td><input name="comments" type="comments" style="width: 139px" /></td>
<td><input id="ini" name="ini" type="text" style="width: 68px" /></td>
</tr>


    <!--end of first row-->

<tr>

    <!--First row accross on the table-->
<td><select name="Department2" id="Department2" >
            <option value ="">Please select ...</option>
            <option value ="Upstairs Hang East">Upstairs Hang East</option>
            <option value ="Upstairs Hang West">Upstairs Hang West</option>
            <option value ="RDR">RDR</option>
            <option value ="Hang Sortation">Hang Sortation</option>
            <option value ="RACK WAREHOUSE">RACK WAREHOUSE</option>
            <option value ="Hang To Flat">HANG TO FLAT</option>
            <option value ="RACK SHOES">RACK SHOES</option>
            <option value ="FLAT 2">FLAT 2</option>
            <option value ="FLAT 1">FLAT 1</option>
            <option value ="Claims">Claims</option>
            <option value ="INBOUND">INBOUND</option>
            <option value ="OUTBOUND">OUTBOUND</option>
            </select></td>
<td style="width: 75px"><input id="areaCode2" name="areaCode2" onkeydown="return false;" type="text"/></td>
<td style="width: 144px"><input class="offtime2" id="offtime2" name="offtime2"  type="text" /></td>
<td><input id="gytime2" name="gystime2" type="text" /></td>
<td><input id="ftime2" name="ftime2" type="text" /></td>
<td><input name="comments2" type="comments2" style="width: 139px" /></td>
<td><input id="ini2" name="ini2" type="text" style="width: 68px" /></td>
</tr>
<td style="width: 75px"><input id="deptkey2" name="deptkey2"  type="text"/></td>
</tr>

    <!--end of first row-->












    <!--start of the weekend section-->
<tr>
<td colspan="7"><h1 align="center">Weekend Scheduling Request</h1></td>
</tr>

<tr>
<th>Department</th><th style="width: 75px">Saturday On Time</th><th style="width: 144px">Saturday Off Time</th><th>Comments or Date By</th><th>Signature</th></tr>

<tr>
<td><select name="sat_ob_department" id="sat_ob_department" >

            <option value ="">Please select ...</option>
            <option value ="Upstairs Hang East">Upstairs Hang East</option>
            <option value ="Upstairs Hang West">Upstairs Hang West</option>
            <option value ="RDR">RDR</option>
            <option value ="Hang Sortation">Hang Sortation</option>
            <option value ="RACK WAREHOUSE">RACK WAREHOUSE</option>
            <option value ="Hang To Flat">HANG TO FLAT</option>
            <option value ="RACK SHOES">RACK SHOES</option>
            <option value ="FLAT 2">FLAT 2</option>
            <option value ="FLAT 1">FLAT 1</option>
            <option value ="Claims">Claims</option>
            <option value ="INBOUND">INBOUND</option>
            <option value ="OUTBOUND">OUTBOUND</option>
            </select></td>

<td style="width: 75px"><input id="sat_ob_onTime" name="sat_ob_onTime" type="text" /></td>
<td style="width: 144px"><input class="sat_ob_offTime" id="sat_ob_offTime" name="time"  type="text" /></td>
<td><input id="ob_comments" name="ob_comments" type="text" /></td>
<td><input id="ob_signature" name="ob_signature" type="text" /></td>
<td style="width: 75px"><input id="deptkey" name="deptkey" onkeydown="return false;" type="text"/></td>
</tr>


<tr>
<td><select name="sat_ib_department" id="sat_ib_department" >

            <option value ="">Please select ...</option>
            <option value ="Upstairs Hang East">Upstairs Hang East</option>
            <option value ="Upstairs Hang West">Upstairs Hang West</option>
            <option value ="RDR">RDR</option>
            <option value ="Hang Sortation">Hang Sortation</option>
            <option value ="RACK WAREHOUSE">RACK WAREHOUSE</option>
            <option value ="Hang To Flat">HANG TO FLAT</option>
            <option value ="RACK SHOES">RACK SHOES</option>
            <option value ="FLAT 2">FLAT 2</option>
            <option value ="FLAT 1">FLAT 1</option>
            <option value ="Claims">Claims</option>
            <option value ="INBOUND">INBOUND</option>
            <option value ="OUTBOUND">OUTBOUND</option>
            </select></td>

<td style="width: 75px"><input id="sat_ib_onTime" name="sat_ib_onTime" type="text" /></td>
<td style="width: 144px"><input class="sat_ib_offTime" id="sat_ib_offTime" name="time"  type="text" /></td>
<td><input id="ib_comments" name="ib_comments" type="text" /></td>
<td><input id="ib_signature" name="ib_signature" type="text" /></td>

</tr>


</table>

<div><br><p align="center"><input type="submit" value= "Submit Request "></p></div> 

<input type="hidden" value ="<?php echo date("m/d/Y",time())?>"name="Requestdate" id="Requestdate">



</form>


</body>
</html>

1 个答案:

答案 0 :(得分:0)

我更改了$ Department2 = trim($ _POST ['Department2']);

if($ Department2 =='Department2')只有当($ _POST ['Department2'] =!''')

它现在正在运作。