在过去的几天里,我一直在努力学习如何搜索mysql数据库。到目前为止,我有以下代码。由于某种原因,它没有搜索并给我结果。我的数据库被命名为得分,表格都是得分。有人请帮帮我。
它应该搜索我的数据库,但它没有结果。我已经确定一切都是正确的。
此文件为searching.php
<?php
if(isset($_POST['search']))
{
$id = $_POST['id'];
$connect = mysqli_connect("localhost", "root", "root","score");
$query = "SELECT `name` FROM `all_scores` WHERE `id` = $id LIMIT 1";
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
while ($row = mysqli_fetch_array($result))
{
$name = $row['name'];
}
}
else {
echo "Undifined ID";
$gameid = "";
}
mysqli_free_result($result);
mysqli_close($connect);
}
else{
$gameid = "";
}
?>
这是search.php
<head>
<title> PHP FIND DATA </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="searching.php" method="post">
Id:<input type="text" name="id"><br><br>
<input type="submit" name="search" value="Find">
</form>
</body>
我真的希望我能让这个工作。在此先感谢您的帮助。
答案 0 :(得分:1)
要获取php文件中的表单值,您需要使用$_POST。以下是使用PDO的示例。您只需检索一行,因此您不需要while循环。
<强> searching.php 强>
$(window).resize(function () {
// Debounce our resizing
var debounce = setTimeout(function() {
// Only set attribute viewbox once
function setView(c, w, h) {
if (!c.getAttribute("viewBox")) {
var viewBox = "20 0 " + w + " " + h + "";
c.setAttribute("viewBox", viewBox);
c.setAttribute("preserveAspectRatio", "xMidYMid meet");
}
}
// Reset previous timers
clearTimeout(reset);
clearTimeout(debounce);
// Set variables
var cal = d3.select("svg.RdYlGn")[0][0],
parent = cal.parentElement,
aspect = Math.round(parent.offsetWidth / parent.offsetHeight);
var newHeight = (parent.offsetWidth / aspect),
newWidth = parent.offsetWidth,
oldHeight = cal.getAttribute("height") || cal.offsetHeight,
oldWidth = cal.getAttribute("width") || cal.offsetWidth;
// Set the viewbox
setView(cal,oldWidth,oldHeight);
// Set new dimensions for calendar
cal.setAttribute("height", newWidth / aspect);
cal.setAttribute("width", newWidth);
// Make sure parent <div> behaves and follows with calendar
var reset = setTimeout(function() {
parent.setAttribute("width", cal.getAttribute("width"));
parent.setAttribute("height", cal.getAttribute("width")/aspect);
}, 500);
}, 300);
});
<强> HTML 强>
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
try{
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$q = $conn->prepare("SELECT `name` FROM `all_scores` WHERE `id` = :id LIMIT 1");
$q->bindValue(':id', $_POST['id'], PDO::PARAM_STR, 50);
$q->execute();
if ($q->rowCount() > 0){
$check = $q->fetch(PDO::FETCH_ASSOC);
$row_id = $check['id'];
// do something
}
}catch(PDOException $e)
{
echo $e->getMessage();
}
?>
花点时间看一下several other examples