如何以html格式从php显示mysql数据。(NOT TABLE)

时间:2017-09-12 17:14:26

标签: php html mysql

我一直在寻找一个解决方案,但它们都属于html表。我有一个简单的表单,并使用phpMyAdmin手动将值添加到数据库中。我在顶部有一个下拉菜单,只要管理员从下拉菜单中选择一个特定名称,然后按下“显示字段”即可。按钮,我希望所有相应的字段都填入值,之后管理员可以对任何特定字段进行更改并更新。如何获得这些值?我尝试了多个代码,但不断收到错误,例如未定义的索引,未定义的变量等。有人可以帮助我吗?

 <!doctype html>

<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db_dealer_track";
$conn = new mysqli($servername, $username, $password, $dbname);

if($conn->connect_error){
die("Connection failed". $conn->connect_error);
}


if(isset($_POST['id1'])){

$sql = "SELECT * FROM tbl_dealer_info ";
$sql .= "WHERE $account_name = 'account_name' ";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_array($result)){


?>

<html>
<head>
<title>iMobile </title>
</head>
<body bgcolor = "#D6DFE3">
<center><b><h2>Please enter the following information: </h2></b></center>

<form action = "dealer_track.php" method = "post"> 
<strong><center> <u>Fields marked with an asterisk(*) are required.</u><br><br>
Name of the dealer:* // This is where the admin selects the user they would like to update

<?php 
$sql = "SELECT account_name FROM tbl_dealer_info ";
$result = mysqli_query($conn, $sql);
echo "<select name = 'account_name' id = 'id'>"; 
echo "<option value = ''>";
while($row = mysqli_fetch_array($result)){
    echo "<option value = '" .$row['account_name'] . "'>" . $row['account_name'] . "</option>";
}
echo "</select>";
?>
<br><br>
<input type = submit id = "id1" name = "id1" value = "Display the fields" /><br>
</center>
<hr>
<br><br>
</form> 

<form action = "dealer_track.php" method = "post">

Email:*<br>     
<input type = "email" name = "email" id = "id3" value = "<?php echo $row['email']?>" Required /><br><br>


RSM:*<br>
<?php
$sql = "SELECT rsm_val FROM tbl_rsm_drop_down ";
$result = mysqli_query($conn, $sql);
echo "<select name = 'rsm_val'>"; 
echo "<option value = ''></option>";
while($row = mysqli_fetch_array($result)){
    echo "<option value = '" .$row['rsm_val'] . "'>" . $row['rsm_val'] . "</option>";
}
echo "</select>";
?>
<br><br>
**// My radio buttons aren't getting checked though**

iPhone Boost Approved: 
<input type = "radio" name = "boost_app" <?php if(isset($boost_app)&& $boost_app =="Yes")?> value = "Yes" />Yes 
<input type = "radio" name = "boost_app" <?php if(isset($boost_app)&& $boost_app =="No")?> value = "No" />No<br><br>
</form>
<?php
}}  // While loop and if loop at the start
?>

</body>
</html> 

3 个答案:

答案 0 :(得分:0)

你的代码有点乱,但这是你需要做的一般。 首次查询唯一记录:

$sqlQuery = "SELECT id, firstname, lastname FROM Table Where id = '$id'";

然后运行查询:

$result = $connection->query($sqlQuery ); //nb: $connection is your connection variable

然后检查是否找到任何结果:

if ($result->num_rows > 0) { ........ }

如果找到任何记录,则将获取的数据放在像这样的变量

while($row = $result->fetch_assoc()) {
  $firstname = $row["firstname"];
  $lastname = $row["lastname"];
  //and so on....
}
// You can display these variables any how you want in here, eg:
echo "<h2>$firstname</h2>";

<input type="text" id="firstname" name="firstname" value="<?php echo $firstname ?>" /> 
//nb: you must close the php tag before using html and re open it after

如果“if($ result-&gt; num_rows&gt; 0){...}为false,只需使用else {...}来显示消息

答案 1 :(得分:0)

我在这里做了一个疯狂的猜测,所以我假设你想从下拉列表中选择一个用户(如果很多人都在所说的数据库中,这可能是一个坏主意),但是你想要做一个简单的事情HTML表单和名称,你会记得。在这个表格下?

<?php
if(isset($_POST['formnamehere'])) {
$sql = "SELECT * FROM (table name) WHERE accountname=" . $accountname;
$result = $conn->query($sql);
$row = $result->fetch_assoc();
echo $row['accountname'];
//put other things here, etc.
}
?>

当然,此代码并不是准确使用。但是要给你一个大致的想法。

答案 2 :(得分:0)

您可以使用活动连接运行查询,以从所需的表中获取各自的信息,以及名称等于给定值的搜索子句。

查询:

$result = mysqli_query($con, "SELECT `data` FROM `table` WHERE `name` = '$name';");

然后您可以通过输出结果在前端显示您的数据。

<?php
    if($row = mysqli_fetch_array($result)) {
        echo $row["data"];
    }
?>