gpt4 book ai didi

php - 使用下拉菜单从 MySQL 提取和发布数据

转载 作者:行者123 更新时间:2023-11-29 21:39:35 25 4
gpt4 key购买 nike

我是 php 和 MySQL 的新手基本上我想做的是能够从表中检索数据在 MySQL 数据库中并将其放入下拉菜单中。填写其他字段后,我希望将下拉菜单中的数据写入同一数据库中的另一个表中。

这是我的 insert.php 文件

        <?php

#### INSERTS A SINGLE CUSTOMER IN Company-->Customer Database with UTF8 Change for special German Characters - Cyrilic Doesnt work - Other change then utf8 ???
#### Getting Data from Index.php


$servername = "127.0.0.1";
$username = "root";
$password = "";
$dbname = "company";

//making an array with the data recieved, to use as named placeholders for INSERT by PDO.


#### Getting DAta from Index.php
$data = array('CustomerName' => $_POST['CustomerName'] , 'Address1' => $_POST['Address1'], 'Address2' => $_POST['Address2'], 'City' => $_POST['City'], 'PostCode' => $_POST['PostCode'], 'CountryID' => $_POST['CountryID'], 'ContactName' => $_POST['ContactName'], 'ContactEmail' => $_POST['ContactEmail'], 'ContactPhone' => $_POST['ContactPhone'], 'ContactFax' => $_POST['ContactFax'], 'Website' => $_POST['Website']);

try {
// preparing database handle $dbh
$dbh = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); ### Database Connect with Special characters Change
// set the PDO error mode to exception
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// query with named placeholders to avoid sql injections
$query = "INSERT INTO customer (CustomerName, Address1, Address2, City, PostCode, CountryID, ContactName, ContactEmail, ContactPhone, ContactFax, Website )
VALUES (:CustomerName, :Address1, :Address2, :City, :PostCode, :CountryID, :ContactName, :ContactEmail, :ContactPhone, :ContactFax, :Website )";
//statement handle $sth
$sth = $dbh->prepare($query);
$sth->execute($data);
echo "New record created successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$dbh = null;
?>

这是 html 页面的来源

        <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add New Product</title>
</head>
<body>
<form action="insert.php" method="post">
<p>
<label for="CustomerName">CustomerName:</label>
<input type="text" name="CustomerName" id="CustomerName">
</p>
<p>
<label for="Address1">Address 1:</label>
<input type="text" name="Address1" id="Address1">
</p>
<p>
<label for="Address2">Address 2:</label>
<input type="text" name="Address2" id="Address2">
</p>

<p>
<label for="City">City:</label>
<input type="text" name="City" id="City">
</p>

<p>
<label for="PostCode">Post Code:</label>
<input type="text" name="PostCode" id="PostCode">
</p>

<p>
<label for="CountryID">Country ID:</label>
<input type="text" name="CountryID" id="CountryID">
</p>

<p>
<label for="ContactName">Contact Name:</label>
<input type="text" name="ContactName" id="ContactName">
</p>

<p>
<label for="ContactEmail">Contact Email:</label>
<input type="text" name="ContactEmail" id="ContactEmail">
</p>

<p>
<label for="ContactPhone">Contact Phone:</label>
<input type="text" name="ContactPhone" id="ContactPhone">
</p>

<p>
<label for="ContactFax">Contact Fax:</label>
<input type="text" name="ContactFax" id="ContactFax">
</p>

<p>
<label for="Website">Website:</label>
<input type="text" name="Website" id="Website">
</p>

<input type="submit" value="Add Records">
</form>
</body>
</html>

所以我想添加这个 php 代码,提取国家/地区列表和电话号码前缀,然后当我点击提交按钮时,下拉菜单的实际输出将写入客户表中

                <p>
<label for="CountryID">Country:</label>

<?php
$servername = "localhost";
$username = "root";
$password ="";
$dbname = "company";

$con_qnt = mysqli_connect($servername, $username, $password, $dbname);
if(!mysqli_connect("localhost","root",""))
{
die('oops connection problem ! --> '.mysqli_connect_error());
}
if(!mysqli_select_db($con_qnt, "company"))
{
die('oops database selection problem ! --> '.mysqli_connect_error());
}


$sql = "SELECT * FROM country";
$result = mysqli_query($con_qnt, "SELECT * FROM country" );


echo "<select name='label'>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['CountryName' ] . "'>" . $row['CountryName' ] . " (" .$row['PhonePrefix' ] . ")" . "</option>";

}
echo "</select>";
?>
<name="CountryID" id="CountryID">
</p>

我什至不知道这是否可行 - 我搜索了很长时间但找不到任何我需要的东西。主要是我发现了硬编码的 html 下拉菜单。事实上,硬编码这将适用于国家,但如果我希望它能够在下拉菜单中显示产品,那么它就不起作用。谢谢大家 。

最佳答案

您好,我刚刚制作了一个快速示例来回答您有关如何使用数据库中的数据填充下拉列表的问题。在这个例子中,我使用了

JQuery's AJAX Function

首先,对于后端,(这只是选择数据的快速示例。请勿将其用作 future 代码的模式

<?php
$dbh = new PDO("mysql:host=localhost;dbname=dbhelp", 'root', 'pup');
$stmt = $dbh->prepare("SELECT * from t_country");
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($result);
?>

我再说一遍。这仅供快速引用。不要将其用作代码的模式。因此,使用该代码,我们现在从表国家/地区中选择了数据,并使用json_encode将其设为json格式。在这里阅读更多相关信息。 http://php.net/manual/en/function.json-encode.php .

下一个是前端(HTML部分)

<body>
// empty dropdown button to be filled by data from database
<select id="country">
</select>
//include the jquery library

<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>

<script type="text/javascript">
$(document).ready(function () {
$.ajax ({
url : 'dropdown_database.php', // the url from which the data will be pulled
success: function(data) {
var country = $.parseJSON(data); // parse the json format data
$.each(country, function(i, d) {
$('#country').prepend('<option>'+ d.country +'</option>'); // this one adds the <option></option> tag in the dropdown
});
}
});
});
</script>

关于php - 使用下拉菜单从 MySQL 提取和发布数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34646739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com