简体   繁体   English

如何在phpMyAdmin中调用和显示数据库

[英]How to call and display a database in phpMyAdmin

My assignment is to create two tables in phpMyAdmin, and then to create a simple form where the user can click a button and have the two tables displayed. 我的任务是在phpMyAdmin中创建两个表,然后创建一个简单的表单,用户可以单击一个按钮并显示两个表。 I have the landing page for the assignment finished with header and search bar and button, but I'm having trouble figuring out how to bring the two tables in from the database and display them once the button is clicked. 我的任务登陆页面以标题,搜索栏和按钮完成,但是我很难弄清楚如何从数据库中引入两个表并在单击按钮后显示它们。

I have had a tutor help with some of the code but I haven't been able to get it to work properly and would love any further help. 我有一些导师帮助解决了一些代码,但我无法让它正常工作,并希望得到任何进一步的帮助。

Here is the code I have now (the two separate php code chunks are two ways people tried to help me do it but I don't know which works or how to implement it): 这是我现在拥有的代码(两个单独的php代码块是人们试图帮助我完成该任务的两种方式,但我不知道哪种可行或如何实现):

<!DOCTYPE html>
<?php
$servername = "localhost";
$username = "username";
$password = "password";

$conn = new mysqli($servername, $username, $password);

if ($conn->connect_error) {die("Connection Failed: " . $conn->connect_error);
}
echo "Connected Successfully";
?>
<html>
<head>
</head>
<body>
<h1>Health Club Patron and Class Information</h1>
<form name="contact-form" action="" method="post" id="contact-form">
<div class="form-group">
<label for="Search">Search</label>
<input type="text" class="form-control" search="your_name" placeholder="Search" required>
</div>
<button type="print" class="btn btn-primary" name="print" value="Print" id="submit_form">Print</button>
</form>
</body>
</html>



$connection = mysql_connect('localhost', 'root', ''); //The Blank string is the password

mysql_select_db('hrmwaitrose');

$query = "SELECT * FROM employee"; //You don't need a ; like you do in SQL

$result = mysql_query($query);

echo "<table>"; // start a table tag in the HTML

while($row = mysql_fetch_array($result)){   //Creates a loop to loop through results

echo "<tr><td>" . $row['name'] . "</td><td>" . $row['age'] . "</td></tr>";  //$row['index'] the index here is a field name

}

echo "</table>"; //Close the table in HTML

mysql_close(); //Make sure to close out the database connection

Try looking at examples on this website, good example's taken from W3 schools to learn with some simple examples. 试着看看这个网站上的例子,从W3学校那里学习一些简单的例子。 https://www.w3schools.com/php/php_mysql_select.asp https://www.w3schools.com/php/php_mysql_select.asp

And a form handling example in PHP https://www.w3schools.com/php/php_forms.asp PHP中的表单处理示例https://www.w3schools.com/php/php_forms.asp

You setup a php file such as post.php then your form will post to that endpoint. 您设置了一个php文件,如post.php,然后您的表单将发布到该端点。 Or if you are posting a PHP form to itself you use <?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?> 或者,如果您要向自己发布PHP表单,请使用<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?> <?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?> for the post address in the form. <?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>表示表格中的邮寄地址。 When learning, try taking these simpler like the example's below than building from that, so you get the basic idea. 在学习时,尝试将这些更简单,如下面的示例,而不是从中构建,这样您就可以获得基本的想法。

Here's a few examples, this one just grabs the rows from the database. 这里有一些例子,这个例子只是从数据库中获取行。

<?php
$servername = "localhost"; // Server host
$username = "username"; // Database username
$password = "password"; // Database password
$dbname = "hrmwaitrose"; // Your database name

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
// Select the rows name and age from employee
$sql = "SELECT name, age FROM employee";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo " - Name: " . $row["name"]. " " . $row["age"]. "<br>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>

Simple example post a form to PHP file a different URL, not posting to itself. 一个简单的示例将表单发布到PHP文件的其他URL,而不是发布到自身。 Filename: index.php 文件名: index.php

<html>
<body>

<form action="results.php" method="get">
Name: <input type="text" name="name"><br>
Age: <input type="text" name="age"><br>
<input type="submit">
</form>

</body>
</html>

Then your results.php contains Filename: results.php 然后你的results.php包含Filename: results.php

<html>
<body>

Welcome <?php echo $_GET["name"]; ?><br>
Your email address is: <?php echo $_GET["age"]; ?>

</body>
</html>

This is an example using a form posting to itself, instead of the example's above which post to another file. 这是一个使用表单发布到自身的示例,而不是上面发布到另一个文件的示例。 Using $_GET["name"] you are grabbing the post variable's from the URL and then you can query your database, unless you are using _POST . 使用$_GET["name"]你从URL中获取post变量,然后你可以查询你的数据库,除非你使用_POST If your posting directly to the same file you would do something like this, which I think your question is asking. 如果你直接发布到同一个文件,你会做这样的事情,我想你的问题就是这样。

<?php if (!empty($_POST)): ?>
    Welcome, <?php echo htmlspecialchars($_POST["name"]); ?>!<br>
    Your age is <?php echo htmlspecialchars($_POST["age"]); ?>.<br>
<?php else: ?>
    <form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?> method="post">
        Name: <input type="text" name="name"><br>
        Age: <input type="text" name="age"><br>
        <input type="submit">
    </form>
<?php endif; ?>

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

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