用于填充选择列表的数据库

时间:2013-05-05 13:51:08

标签: php database selection populate

我正在尝试创建一个表单,并且在该表单中有一个选择列表,其中选项自动填充来自数据库的数据(即客户的姓氏),之后从列表中选择姓氏时提交按钮被点击与客户ID相关联,该客户ID与数据库中的姓氏相关,将被提交到另一个PHP文件(task8.php)以通过进一步查询发送。我希望我以一种可以理解的方式解释这一切。我已经去了一些代码,但我真的不确定如何做到这一点,或者我写的是在正确的道路上。

这是我到目前为止所写的内容:

<body>

<?php
$conn = mysql_connect("localhost", "twa312", "dam6av9a");
mysql_select_db("warehouse312", $conn)
or die ('Database not found ' . mysql_error() );

$sql = "select customerID, lastName from customer";
$rs = mysql_query($sql, $conn)
or die ('Problem with query' . mysql_error());

$options= '<option value="0">Choose</option>';

while ($row=mysql_fetch_array($rs)) {
$id=$row["customerID"];
$name=$row["lastName"];
$options="<OPTION VALUE='" . $id . "'>" . $name ."</option>";
}

?>

<form method="GET" action="task8.php" id="custinfo" >

Choose name:<select name="lname" id="lname"><?php echo $options; ?>  
</select>

<p><input type="submit" name="submit" value="Save Data"/>&nbsp;<input type="reset" value="Clear Form" />

</form>

我要对代码执行的操作是访问“customer”表并填写“customerID”和“lastName”字段。使用客户的姓氏作为选项,将客户的ID作为选择列表中的选项值。目前,当代码显示数据库中的所有名称时,代码仅在选择列表中显示单个名称作为选项。对此的任何帮助都会非常好,因为我相当不确定。

2 个答案:

答案 0 :(得分:1)

我可以看到代码中的错误会导致PHP生成通知错误。

while循环中,您在.=变量上使用$options尚未定义,因此PHP会对此进行限制。

除此之外,在从mysql迭代结果集之前等待$_GET['submit']设置是没有意义的。据我所知,当你第一次点击这个页面时,选择中会有一个选项(“选择”),并且由于表单提交到另一个页面,我认为你没有看到过客户姓氏列表。

最后,并不是真的建议将提交按钮命名为“submit”,因为当浏览器解析页面时,特定表单的所有表单元素都被创建为该表单的属性,JS表单对象具有“提交” '方法所以当你命名一个输入'submit'时,你破坏了表单对象中的那个值,这使得用JS提交该表单真的很难。

答案 1 :(得分:1)

首先离开mysql_functions。

其次创建一个模型,其中包含与客户相关的所有查询,这些查询将处理提取/放置/更新与客户数据库相关的数据。

<?php 
Class CustomerModel{
    private $db;

    function __construct($host,$dbname,$user,$pass){
        $this->dbhost = $host;
        $this->dbname = $dbname;
        $this->dbuser = $user;
        $this->dbpass = $pass;
    }

    private function connect(){
        if (!$this->db instanceof PDO){
            $this->db = new PDO('mysql:dbname='.$this->dbname.';host='.$this->dbhost, $this->dbuser, $this->dbpass);
            $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
            $this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
        }
    }

    public function select_all_customer($cols="*"){
        $this->connect();
        $sql = "SELECT $cols FROM customer";
        $statement = $this->db->prepare($sql);
        $statement->execute();
        return $statement->fetchAll(PDO::FETCH_ASSOC);
    }

    public function select_customer($cols="*",$where=null, $id=null){
        $this->connect();
        $sql = "SELECT $cols FROM customer WHERE $where = :id";
        $statement = $this->db->prepare($sql);
        $statement->bindParam(':id', $id, PDO::PARAM_STR);
        $statement->execute();
        return $statement->fetchAll(PDO::FETCH_ASSOC);
    }

}
?>

现在您可以访问模型,如:     

<form method="POST" action="task8.php" id="custinfo" >
Choose name:
<select name="customerID" id="customerID">
    <option value="0">Choose</option>
    <?php foreach($customer->select_all_customer("customerID, lastName") as $row): ?>
      <option value="<?php echo $row['customerID']?>"><?php echo $row['lastName']?></option>
    <?php endforeach; ?>
</select>
<p><input type="submit" name="submit" value="Save Data"/>&nbsp;<input type="reset" value="Clear Form" />
</form>


<?php 
//Get customer from form values
if($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST['customerID'])){
    $result = $customer->select_customer("*", "customerID", $_POST['customerID']);
    //Do something with result
    echo '<pre>'.print_r($result, true).'</pre>';

}
?>

希望有所帮助