将下拉值写入SQL表字段

时间:2017-07-08 07:09:49

标签: php sql forms drop-down-menu

在PHP表单中创建一个下拉列表,其中此下拉列表的值应写入SQL表字段。

我几乎可以确定这个问题是name="txtStatus" value="<?php echo $dataSt['Opt'];?>">,但我无法让它发挥作用。

EDITED 这是edit.php的代码,现在我可以让页面上的下拉菜单工作,但$result["Status"]中的值不会被发送到save.php文件,当然也不属于SQL数据库表字段。

<html>
<head>
<title>TEST EDIT</title>
</head>
<style type="text/css">
@import url(http://fonts.googleapis.com/css?family=Roboto:400,500,700,300,100);
body {
  background-color: #576370;
  font-family: "Roboto", helvetica, arial, sans-serif;
  font-size: 16px;
  font-weight: 400;
  text-rendering: optimizeLegibility;
}

div.table-title {
  text-align:center;
  display: block;
  margin: auto;
  width: 100%;
}

.table-title h3 {
   color: #fafafa;
   padding:1px;
   font-size: 25px;
   font-weight: 300;
   font-style:normal;
   font-family: "Roboto", helvetica, arial, sans-serif;
   text-shadow: -1px -1px 1px rgba(0, 0, 0, 0.1);
   text-transform:uppercase;
}

.table-title h2 {
   color: #D8D8D8;
   font-size: 10px;
   font-weight: 50;
   font-style:normal;
   text-transform:uppercase;
}

.table-title h4 {
   color: #F3F781;
   font-size: 10px;
   font-weight: 50;
   font-style:normal;
   text-transform:uppercase;
}

/*** Table Styles **/

.table-fill {
  background: white;
  border-radius:3px;
  border-collapse: collapse;
  height: 15px;
  margin: auto;
  padding:1px;
  width: 100%;
  box-shadow: 0 5px 10px rgba(0, 0, 0, 0.1);
  animation: float 8s infinite;
}

th {
  color:#D5DDE5;
  text-align:left;
  background:#1b1e24;
  border-right: 1px solid #343a45;
  font-size:14px;
  font-weight: 50;
  padding:8px;
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);
  vertical-align:middle;
  width: 100px;
}

th:first-child {
  border-top-left-radius:3px;

}

th:last-child {
  border-top-right-radius:3px;
  border-right:none;
}

tr {
  border-top: 1px solid #C1C3D1;
  border-bottom-: 1px solid #C1C3D1;
  color:#666B85;
  font-size:16px;
  font-weight:normal;
  text-shadow: 0 1px 1px rgba(256, 256, 256, 0.1);
}

tr:hover td {
  background:#4E5066;
  color:#FFFFFF;
  border-top: 0px solid #22262e;
  border-bottom: 0px solid #22262e;
}

tr:first-child {
  border-top:none;
}

tr:last-child {
  border-bottom:none;
}

tr:nth-child(odd) td {
  background:#EBEBEB;
}

tr:nth-child(odd):hover td {
  background:#4E5066;

}

tr:last-child td:first-child {
  border-bottom-left-radius:3px;
}

tr:last-child td:last-child {
  border-bottom-right-radius:3px;
}

td {
  background:#FFFFFF;
  padding:8px;
  vertical-align:middle;
  font-weight:300;
  font-size:14px;
  text-shadow: -1px -1px 1px rgba(0, 0, 0, 0.1);
  border-right: 1px solid #C1C3D1;
}

.show {display:block;}
</style>
</head>
<body>
<div class="table-title">
    <h3>SCANIA FINANCE</h3>
<body>

<?php
   ini_set('display_errors', 1);
   error_reporting(~0);

    $serverName = 'localhost';
    $userName = 'user';
    $userPassword = 'password';
    $dbName = 'AdWorks';

   $strBusinessEntityID = null;

   if(isset($_GET["BusinessEntityID"]))
   {
       $strBusinessEntityID = $_GET["BusinessEntityID"];
   }

   $connectionInfo = array("Database"=>$dbName, "UID"=>$userName, "PWD"=>$userPassword, "MultipleActiveResultSets"=>true);

   $conn = sqlsrv_connect( $serverName, $connectionInfo);

   if( $conn === false ) {
      die( print_r( sqlsrv_errors(), true));
   }

    $stmt = "SELECT * FROM tKelvin32 WHERE BusinessEntityID = ? ";
    $params = array($strBusinessEntityID);

    $query = sqlsrv_query( $conn, $stmt, $params);

    $result = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC);


?>
<form action="save_bck.php" name="frmAdd" method="post">
<table align="center">
  <tr>
    <th width="120">BusinessEntityID</th>
    <td><input type="hidden" name="txtBusinessEntityID" value="<?php echo $result["BusinessEntityID"];?>"><?php echo $result["BusinessEntityID"];?></td>
    </tr>
  <tr>
    <th width="120">FirstName</th>
    <td><input type="hidden" name="txtFirstName" value="<?php echo $result["FirstName"];?>"><?php echo $result["FirstName"];?></td>
    </tr>
  <tr>
    <th width="120">LastName</th>
    <td><input type="hidden" name="txtLastName" size="25" value="<?php echo $result["LastName"];?>"><?php echo $result["LastName"];?></td>
    </tr>
  <tr>
    <th width="120">JobTitle</th>
    <td><input type="hidden" name="txtJobTitle" value="<?php echo $result["JobTitle"];?>"><?php echo $result["JobTitle"];?></td>
    </tr>
  <tr>
    <th width="120">PhoneNumber</th>
    <td><input type="hidden" name="txtPhoneNumber" value="<?php echo $result["PhoneNumber"];?>"><?php echo $result["PhoneNumber"];?></td>
    </tr>
  <tr>
    <th width="120">PhoneNumberType</th>
    <td><input type="hidden" name="txtPhoneNumberType" value="<?php echo $result["PhoneNumberType"];?>"><?php echo $result["PhoneNumberType"];?></td>
    </tr>
    <tr>
    <th width="120">EmailAddress</th>
    <td><input type="hidden" name="txtEmailAddress" value="<?php echo $result["EmailAddress"];?>"><?php echo $result["EmailAddress"];?></td>
    </tr>
    <tr>
    <th width="120">AddressLine1</th>
    <td><input type="hidden" name="txtAddressLine1" value="<?php echo $result["AddressLine1"];?>"><?php echo $result["AddressLine1"];?></td>
    </tr>
    <tr>
    <th width="120">City</th>
    <td><input type="hidden" name="txtCity" value="<?php echo $result["City"];?>"><?php echo $result["City"];?></td>
    </tr>
    <tr>
    <th width="120">PostalCode</th>
    <td><input type="hidden" name="txtPostalCode" value="<?php echo $result["PostalCode"];?>"><?php echo $result["PostalCode"];?></td>
    </tr>
    <tr>
    <th width="120">CountryRegionName</th>
    <td><input type="hidden" name="txtCountryRegionName" value="<?php echo $result["CountryRegionName"];?>"><?php echo $result["CountryRegionName"];?></td>
    </tr>
    <tr>
    <th width="120">Status</th>
    <td>
    <input type="hidden" name="txtStatus" value="<?php echo $result["Status"];?>"><select name="TowerSelect"><option> Choose </option>
    <?php
    //STATUS Query
    $sqlSt = "SELECT DISTINCT Opt FROM StatsOptions ORDER BY Opt";
    $resultSt = sqlsrv_query($conn,$sqlSt) or die("Couldn't execut query");
    while ($dataSt=sqlsrv_fetch_array($resultSt, SQLSRV_FETCH_ASSOC)){
    echo '<option value="'.$dataSt['Opt'].'">';
    echo $dataSt['Opt']; 
    echo "</option>";
    }
    ?>
    </select>
    </td>
    </tr>
  </table>
  <input type="submit" name="submit" value="submit">
</form>
<?php
sqlsrv_close($conn);
?>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

您忘记关闭选择标记。关闭php标签后关闭你的select标签。

...</select>....

并查看表单提交后是否获得下拉值。

答案 1 :(得分:0)

好的,我明白了,问题不是save.php代码,而是$_POST["txtStatus"]代码,我调用了错误的变量,我调用的是$_POST["Desc"]而不是if( $conn === false ) { die( print_r( sqlsrv_errors(), true)); } $sql = "UPDATE tKelvin32 SET FirstName = ? , LastName = ? , JobTitle = ? , PhoneNumber = ? , PhoneNumberType = ?, EmailAddress = ?, AddressLine1 = ?, City = ?, PostalCode = ?, CountryRegionName = ?, Status = ? WHERE BusinessEntityID = ? "; $params = array($_POST["txtFirstName"], $_POST["txtLastName"], $_POST["txtJobTitle"], $_POST["txtPhoneNumber"], $_POST["txtPhoneNumberType"], $_POST["txtEmailAddress"], $_POST["txtAddressLine1"], $_POST["txtCity"], $_POST["txtPostalCode"], $_POST["txtCountryRegionName"], $_POST["Desc"],$_POST["txtBusinessEntityID"]); 1}},请参阅以下代码。

cache: packages