我有一个工作表单代码form.php如下:
<?php
//database connection file setting.inc will need to be modified for production
include ("settings.inc");
$db = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
$sql = <<<SQL
SELECT DISTINCT `AllianceName`
FROM `City` ORDER BY `AllianceName`
SQL;
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
?>
<html>
<form id='Alliance' name='' method='post' action='results.php'>
<p><label>Alliance Name:</label></p>
<select style="width:300px" class="" id="AllianceName" size="1" name="Alliance">
<?php
while($row = $result->fetch_assoc()){
echo '<option value='.$row['AllianceName'].'>'.$row['AllianceName'].'</option>';
}
?>
</select>
<?php
$sql2 = <<<SQL
SELECT DISTINCT `Continent`
FROM `City` ORDER BY `Continent`
SQL;
if(!$result = $db->query($sql2)){
die('There was an error running the query [' . $db->error . ']');
}
?>
<p><label>Continent:</label></p>
<select style="width:300px" class="" id="Continent" size="1" name="Continent">
<?php
while($row = $result->fetch_assoc()){
echo '<option value='.$row['Continent'].'>'.$row['Continent'].'</option>';
}
?>
</select>
<input type="submit" value="Send" />
</form>
</html>
这会调用results.php
<?php
include ("settings.inc");
$db = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
if(isset($_POST['Alliance']) )
{
$varname = $_POST['Alliance'];
$varcontinent = $_POST['Continent'];
}
echo $_POST['Alliance'];
$sql3 = <<<SQL
SELECT * FROM `City` WHERE `AllianceName` = '$varname'
AND `Continent` = '$varcontinent'
SQL;
if(!$result = $db->query($sql3)){
die('There was an error running the query [' . $db->error . ']');
}
?>
<html>
<?php
while($row = $result->fetch_assoc()){
//arguments etc go here for displaying data in a table
}
?>
现在,我确信results.php没问题,因为我已经使用编码的静态查询进行测试,并从db输出数据。
我的问题很奇怪,我没有找到任何解释为什么会发生这种情况的文件。
在form.php中,所选的联盟值可以是两个或三个单词的字符串。现在这个字符串应该只是解析,但我看到的是它在第一个空格切断,即如果字符串是“有一个美好的一天”,我的$ _POST ['联盟']只会显示单词“Have”。
我试图在联盟的select标签中添加一个length =“255”属性,但这没有用。
答案 0 :(得分:0)
更改为此echo '<option value="'.$row['AllianceName'].'">'.$row['AllianceName'].'</option>';
来自此echo '<option value='.$row['AllianceName'].'>'.$row['AllianceName'].'</option>';