简体   繁体   English

PHP使用mySQL-db中的下拉值

[英]PHP work with dropdown values from mySQL-db

so I've finally figured out how I can get values from my mySQL-db into a dropdown form in PHP. 因此,我终于弄清楚了如何从mySQL-db获取值并将其转换为PHP的下拉表单。 My problem is, how can I use/work with the option that was selected by the user? 我的问题是,如何使用/使用用户选择的选项? My goal is a functioning email-signature generator. 我的目标是运行正常的电子邮件签名生成器。 I want the user to be able to insert unique data like their name, and select the office where they work from the dropdown form. 我希望用户能够像他们的名字一样插入唯一的数据,并从下拉列表中选择他们工作的办公室。 After they hit the submit button, they should then be lead to the next site that displays their name with the signature for the selected office. 当他们点击提交按钮后,他们应该被带到下一个站点,该站点显示其姓名以及所选办公室的签名。 The name is no problem, that is only a simple html-form with a post method, I know how I can retrieve and access that. 名称没问题,那只是一个带有post方法的简单html表单,我知道我可以检索和访问它。 But how can I work with the option that was selected by the user in the dropdown? 但是,如何使用用户在下拉菜单中选择的选项? After selecting an option and hitting submit, the whole "row" in the mySQL-db should be displayed, in formated form. 选择一个选项并单击Submit之后,应以格式化形式显示mySQL-db中的整个“行”。

My current situation is that the dropdown shows the correct values from the mySQL-db, and the two name fields, that are also working as they should. 我当前的情况是,该下拉列表显示了来自mySQL-db的正确值以及两个名称字段,它们也可以正常工作。 The name of the mySQL database is "firmen", the name of the column I use is "niederlassung". mySQL数据库的名称为“ firmen”,我使用的列的名称为“ niederlassung”。

I'm happy to provide more information if needed. 如果需要,我很乐意提供更多信息。 My code: 我的代码:

[...]
<form action="php-verarbeitung.php" method="post">
<div>
  <label for="1">Vorname: </label>
  <input type="text" name="vorname" id="1" value=""><br><br>
</div> 
<div>
  <label for="2">Nachname: </label>
  <input type="text" name="name" id="2" value=""><br><br>
</div> 

<input type = "submit" name = "button" value = "Senden"><br><br>


<?php
// Establish mySQL PDO Connection
$server='mysql:host=*****.com.mysql;dbname=*****'; // Host and DB-Name
$user="******";       // Username
$password="*****";          // Password

$pdo=new PDO($server, $user, $password);

$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// Write out the query
$query = "SELECT niederlassung FROM firmen";

// Execute it, or let it throw an error message if there's a problem
$stmt = $pdo->query($query);

$dropdown = "<SELECT name='firmen'>";
foreach ($stmt as $row) {
  $dropdown .= "\r\n<option value='{$row['niederlassung']}'>{$row['niederlassung']}</option>";
}
$dropdown .= "\r\n</select>";
echo $dropdown;
?>
</form>
</body>
</html>

Thanks for any help in advance! 感谢您的任何帮助!

you are passing the variables through the form to php-verarbeitung.php. 您通过表单将变量传递给php-verarbeitung.php。 On that page you can load the content based on the $_POST variables that are sent through the form. 在该页面上,您可以基于通过表单发送的$ _POST变量加载内容。 so you can query the DB again on the second page to get the info you need, and present formated html using the values from the form passed through the post array, in your case $_POST['vorname'], $_POST['name'] and $_POST['firmen'] should all be accessible when the php-verarbeitung.php page is called using your script above. 因此您可以在第二页上再次查询数据库,以获取所需的信息,并使用通过post数组传递的表单中的值(在您的情况下为$ _POST ['vorname'],$ _ POST ['name)中显示格式的html使用上面的脚本调用php-verarbeitung.php页面时,应该都可以访问']和$ _POST ['firmen']。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM