在下拉菜单中正确显示后,为什么字符串会被缩短?

时间:2014-05-04 22:16:03

标签: php html sql database web

我正在尝试构建一个连接到小型数据库的简单Web界面。我把界面主题化为索契奥运会的名单。我在插入一名新运动员时遇到了麻烦。由于外键依赖性,新运动员只能添加到数据库中的现有团队。为了解决这个问题,我通过调用pg_fetch_all_columns创建了一个表的列数组。然后我遍历数组并为每个数组创建一个新的选项标记。这些名称在下拉列表中正确显示为选项,但是当我尝试将其添加到数据库中时,它只包含团队的第一个单词。例如,“美国男子冰壶队”成为“美国”。我通过调用echo $ variable找到了这个。

正确生成数组并格式化下拉列表的PHP部分:

<?php
                $db_handle = pg_connect("host='postgres.cs.wisc.edu' port='5432' dbname='cs564_s14' sslmode='require'") 
                        or die ("Connection failed".pg_last_error()); 
                $queryTeam = "SELECT name from sochi14_schema.team"; 
                $result = pg_exec($db_handle, $queryTeam);
                if (!$result) {
                echo "An error occurred.\n";
                exit();
                }
                $array =pg_fetch_all_columns($result, 0); 
                for ($i = 0; $i < count($array); ++$i) {
                    echo "<option value=".$array[$i]."> ".$array[$i]." </option>";
                }

                ?>

被调用的动作:     

                if (!isset($_POST['name']) || !isset($_POST['age'])){
                        echo "Name and age of the athlete must be specified";
                        exit();
                    }
                     if (isset($_POST['medals'])){
                        $medals = $_POST['medals'];
                     }else{
                        $medals = "''";
                     }
                    $name = $_POST['name'];
                    $age = $_POST['age'];
                    $team = $_POST['team'];
                    $country = $_POST['country'];

                    echo $name;
                    echo $medals;
                    echo $country;
                    echo $team;



                    if($age < 12){
                    echo "Age must be larger than 12";
                    exit();
                    }

                    if($age > 120){
                    echo "Age must be less than 120";
                    exit();
                    }

                        $db_handle = pg_connect(DATABASE INFO CORRECTLY HANDLED) 
                            or die ("Connection failed".pg_last_error()); 

                        $query = "INSERT into sochi14_schema.athlete values ( '".$name."',
                            ".$age.", '".$medals."', ".$team.", ".$country.")"; 
                        $result = pg_exec($db_handle, $query);

                        if($result){
                            echo "Success: ".$name." has been inserted<br>";
                        }
                        pg_close();
                        ?>

1 个答案:

答案 0 :(得分:0)

想出来......如果该选项在HTML中有多个单词,则必须有引号。例如:

'<option value="'.$row['car'].'">' . $row['car'] . '</option>';