【发布时间】:2020-12-20 15:51:30
【问题描述】:
我对 PHP PDO 世界还很陌生,我现在已经知道,与数据库的连接应该在另一个 PHP 文件中,但是其余的都不起作用:(。我已经尝试了一整天,我知道我太累了,我不得不在这里问。如何摆脱重复项?我认为由于数据库连接位于同一文件夹中,因此存在重复项,因为它不是本地主机而是网站。所以如何将文件写入多个文件?
In the Display.php file, I have the dropdown with HTML, followed by javascript that should trigger the table when a value from the dropdown has been chosen. Action.php 是创建表的位置,我尝试为数据库连接创建一个单独的文件,但是我无法将“uid”javascript 值连接到 Action.php 文件。
我希望你能帮助我摆脱下拉列表的重复。
告诉我是否可以添加更多信息以提供帮助。
最好的问候
这是我从下拉列表中选择一个值时复制的图像,它复制了下拉列表,并将其添加到下面。它仅用于显示下表。
Display.php
<?php
ini_set("display_errors", "On");
error_reporting(E_ALL);
$host='database.*****.us-east.rds.amazonaws.com';
$db = '*****';
$port = 5432;
$username = '*****';
$password = '*****';
try {
$conn = new PDO("pgsql:host=$host;port=$port;dbname=$db;user=$username;password=$password");
} catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$sql ="select * from public.category";
//Prepare the select statement.
$stmt = $conn->prepare($sql);
//Execute the statement.
$stmt->execute();
//Retrieve the rows using fetchAll.
$users = $stmt->fetchAll();
?>
// show menu dropdown
<!Doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container" style="margin-top: 50px;">
<h2 class="text-center">Væg en kategori </h2>
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-4" style="margin-top:20px; margin-bottom:20px;">
<form id="submitForm">
<div class="form-group">
<select class="form-control Investment" name="Investment" id="Investment">
<option value="">Vælg Kategori</option>
<?php foreach($users as $user): ?>
<option value="<?= $user['id']; ?>"><?= $user['name']; ?></option>
<?php endforeach; ?>
</select>
</div>
</form>
</div>
</div>
<div class="col-md-12">
<div id="show-invest">
</div>
</div>
</div>
</body>
</html>
<!---jQuery ajax load rcords using select box --->
<script type="text/javascript">
$(document).ready(function(){
$(".Investment").on("change", function(){
var InvestmentName = $(this).val();
if (InvestmentName !== "") {
$.ajax({
url : "Action.php",
type:"POST",
cache:false,
data:{InvestmentName:InvestmentName},
success:function(data){
$("#show-invest").html(data);
}
});
}else{
$("#show-invest").html(" ");
}
})
});
</script>
Action.php
<?php
// include database connection file
include_once "testSide.php";
// include_once "DBController.php";
if(isset($_POST['InvestmentName']))
{
$uid = $_POST['InvestmentName'];
}
// load records using select box jquery ajax in PHP
$qu = "select Prod.tocon as name, one.con as yield_one, five.con as yield_two, ten.con as yield_three, twenty.con as yield_four, one.st as st_one, five.st as st_two, ten.st as st_three, twenty.st as st_four, one.top as top_one, five.top as top_two, ten.top as top_three, twenty.top as top_four;";
$result = $conn->query($qu);
if (!$result) {
trigger_error('Invalid query:' . $conn->error);
}
$output = '';
if ($result->rowCount() > 0) {
$output .= "<table class='table table-hover table-border'>
<thead>
<tr>
<th>Name</th>
<th>Afkast sidste år</th>
<th>Genst sidste 5 år</th>
<th>Genst stidste 10 år</th>
<th>Genst sidste 20 år</th>
<th>SR sidste år</th>
<th>SR 5 år</th>
<th>SR 10 år</th>
<th>SR 20 år</th>
<th>DD sidste år</th>
<th>DD 5 år</th>
<th>DD 10 år</th>
<th>DD 20 år</th>
</tr>
</thead>";
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$output .= "<tbody>
<tr>
<td>{$row["name"]}</td>
<td>{$row["yield_one"]}</td>
<td>{$row["yield_two"]}</td>
<td>{$row["yield_three"]}</td>
<td>{$row["yield_four"]}</td>
<td>{$row["st_one"]}</td>
<td>{$row["st_two"]}</td>
<td>{$row["st_three"]}</td>
<td>{$row["st_four"]}</td>
<td>{$row["top_one"]}</td>
<td>{$row["top_two"]}</td>
<td>{$row["top_three"]}</td>
<td>{$row["top_four"]}</td>
</tr>";
}
"</tbody>";
$output .= "</tbody></table>";
echo $output;
}else{
echo "No records found";
}
?>
【问题讨论】:
-
再次检查您的代码,因为我在这里进行了测试,它没有复制选择,并且看起来与图像有点不同。
-
要获取每个条目一次,请查找
SELECT DISTINCT。 -
我认为它重复,因为数据库在同一个文件中
-
添加到您页面的任何内容都是由 javascript 完成的,但不是由数据库、pdo 或 PHP 完成的
-
嗨,
console.log(data)在 ajax 的成功函数中,看看它带来了什么。
标签: javascript php ajax