【发布时间】:2017-10-28 15:17:30
【问题描述】:
我创建了一个表单,其中一些字段是多项选择,但是在提交表单后,只有在进行多项选择的字段中的最终选择被插入到 mysql 中。我不知道该怎么办。我是 mysql 和 php 的新手,所以我需要保持简单。先感谢您。
这是我的代码:
if(isset($_POST['add'])){
$dbhost = 'xxxxxxx';
$dbuser = 'xxxxxxx';
$dbpass = 'xxxxxxx';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ){
die('Could not connect: ' . mysql_error());
}
if(! get_magic_quotes_gpc() ){
$TypeOfDwelling = addslashes ($_POST['TypeOfDwelling']);
$EmailAdd = addslashes ($_POST['EmailAdd']);
}else{
$TypeOfDwelling = $_POST['TypeOfDwelling'];
$EmailAdd = $_POST['EmailAdd'];
}
$sql = "
INSERT INTO zzzzDB
( TypeOfDwelling
, EmailAdd
, CreateDate) VALUES
('$TypeOfDwelling'
,'$EmailAdd'
, NOW())
";
mysql_select_db('xxxxx_xxxxx');
$retval = mysql_query( $sql, $conn );
if(! $retval ){
die('Could not enter data: ' . mysql_error());
}
echo '<img src="../../../images/content__images/image_480x320_a.jpg" alt="image" width="300" height="200" class="img-responsive">';
echo "<br />Thank you for your submission!";
mysql_close($conn);
?>
<form name="MLSrequest" action="<?php $_PHP_SELF ?>" method="post" class="form-inline" role="form">
<table width="95%" cellspacing="0" cellpadding="0" border="0" align="left">
<tbody>
<tr>
<td>
<input type="hidden" name="recipient" value="xxx@xxx.ca">
</td>
</tr>
<tr>
<td colspan="2"> <div class="form-group">
<label class="sr-only" for="TypeOfDwelling">Select The Type Of Dwelling</label>
<select name="TypeOfDwelling" size="5" class="form-control form-control-lg" id="TypeOfDwelling" multiple="multiple">
<option selected>Select DWELLING TYPE.</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</div>
</td>
</tr>
<tr>
<td colspan="2">
<div class="form-group">
<label class="sr-only" for="EmailAdd">Email Address</label>
<input name="EmailAdd" type="email" class="form-control form-control-lg" id="EmailAdd" size="17px" maxlength="30" placeholder="Email Address" required>
</div>
</td>
</tr>
<td colspan="2">
<button name="add" id="add" type="submit" class="btn btn-primary">Submit Request</button>
</td>
</tr>
</tbody>
</table>
</form>
<?php
}
【问题讨论】:
-
作为单独的建议,出于安全原因,我建议您停止使用 mysql() 函数并升级到 mysqli 的准备好的语句或 pdo。此外,您可能要考虑不要在
Select DWELLING TYPE.上使用selected,因为如果您的用户不取消选择它,那么选项值将与提交一起发送。 -
另外,在编辑您的问题时,我注意到您有一些额外的和缺少的 html 标签。具体来说,我发现了一个额外的
</div>和一个缺失的<tr>。最后我担心你写了一段自相矛盾的代码,开头的 isset() 条件需要$_POST["add"]才能进入代码块。代码块内的表单 POSTadd键控元素。 ...它发布的方式看起来你永远不会进入条件。我希望这一切对你有帮助,并且我没有让你超负荷。
标签: php mysql arrays forms multiple-select