使用 PHP 从 MySQL 数据库中获取数据,以表格形式显示以供编辑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23174380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fetching data from MySQL database using PHP, Displaying it in a form for editing
提问by user3552442
I'm a newbie to this and wrote the code below to fetch user data from a MySQL Database and display it in a form for editing and saving. Problem is, it does not work. Any help will be appreciated.
我是新手,编写了下面的代码来从 MySQL 数据库中获取用户数据并将其显示在表单中以进行编辑和保存。问题是,它不起作用。任何帮助将不胜感激。
<html>
<head>
<title> Delegate edit form</title>
</head>
<body>
Delegate update form <p>
<?php
$usernm = "root";
$passwd = "";
$host = "localhost";
$database = "swift";
//$Name=$_POST['Name'];
//$Username=$_POST['User_name'];
//$Password=$_POST['Password'];
mysql_connect($host,$usernm,$passwd);
mysql_select_db($database);
$sql = "SELECT * FROM usermaster WHERE User_name='$Username'";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
$Name = $row['Name'];
$Username = $row['User_name'];
$Password = $row['User_password'];
}
?>
<form action="Delegate_update.php" method="post">
Name
<input type="text" name= "Name" value= "<?php echo $row ['Name']; ?> "size=10>
Username
<input type="text" name= "Username" value= "<?php echo $row ['Username']; ?> "size=10>
Password
<input type="text" name= "Password" value= "<?php echo $row ['Password']; ?>" size=17>
<input type="submit" name= "submit" value="Update">
</form>
</body>
</html>
回答by The Oracle
Play around this piece of code. Focus on the concept, edit where necessary so that it can
玩转这段代码。专注于概念,在必要时进行编辑,以便它可以
<html>
<head>
<title> Delegate edit form</title>
</head>
<body>
Delegate update form </p>
<meta name="viewport" content="width=device-width; initial-scale=1.0">
<link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon" />
<link href='http://fonts.googleapis.com/css?family=Droid+Serif|Ubuntu' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="js/flexslider/flexslider.css" />
<link rel="stylesheet" href="css/basic-style.css">
<script src="js/libs/modernizr-2.6.2.min.js"></script>
</head>
<body id="home">
<header class="wrapper clearfix">
<nav id="topnav" role="navigation">
<div class="menu-toggle">Menu</div>
<ul class="srt-menu" id="menu-main-navigation">
<li><a href="Swift_Landing.html">Home page</a></li>
</header>
</section>
<style>
form label {
display: inline-block;
width: 100px;
font-weight: bold;
}
</style>
</ul>
<?php
session_start();
$usernm="root";
$passwd="";
$host="localhost";
$database="swift";
$Username=$_SESSION['myssession'];
mysql_connect($host,$usernm,$passwd);
mysql_select_db($database);
$sql = "SELECT * FROM usermaster WHERE User_name='$Username'";
$result = mysql_query ($sql) or die (mysql_error ());
while ($row = mysql_fetch_array ($result)){
?>
<form action="Delegate_update.php" method="post">
Name
<input type="text" name="Namex" value="<?php echo $row ['Name']; ?> " size=10>
Username
<input type="text" name="Username" value="<?php echo $row ['User_name']; ?> " size=10>
Password
<input type="text" name="Password" value="<?php echo $row ['User_password']; ?>" size=17>
<input type="submit" name="submit" value="Update">
</form>
<?php
}
?>
</p>
</body>
</html>
回答by Dorvalla
<form action="Delegate_update.php" method="post">
Name
<input type="text" name= "Name" value= "<?php echo $row['Name']; ?> "size=10>
Username
<input type="text" name= "Username" value= "<?php echo $row['Username']; ?> "size=10>
Password
<input type="text" name= "Password" value= "<?php echo $row['Password']; ?>" size=17>
<input type="submit" name= "submit" value="Update">
</form>
You didnt closed your opening Form in the first place, plus your code is very very messy. I wont go into the "use pdo or mysqli statements, instead of mysql" thats for you to find out on yourself. Also you have a php tag open and close below it, not sure what is needed there. Something else is that your code refers to an external page, which you didnt post, so if something isnt working there, might be handy to post it too.
您一开始没有关闭打开的表单,而且您的代码非常非常混乱。我不会进入“使用 pdo 或 mysqli 语句,而不是 mysql”的内容,您可以自行了解。另外你有一个 php 标签在它下面打开和关闭,不确定那里需要什么。另一件事是您的代码引用了您没有发布的外部页面,因此如果某些内容在那里不起作用,也可以方便地发布它。
Please also note that you had spaces between your $row array variables in the form. You have to link those up together by removing the space (see edited section from me). PHP isn't forgiving when it comes to those mistakes.
另请注意,表单中的 $row 数组变量之间有空格。您必须通过删除空格将它们链接在一起(请参阅我的编辑部分)。PHP 不会容忍这些错误。
Then your HTML. I took the liberty to correct that too
然后是你的 HTML。我也冒昧纠正了
<html>
<head>
<title> Delegate edit form</title>
</head>
<body>
<p>Delegate update form</p>
<?php
$usernm="root";
$passwd="";
$host="localhost";
$database="swift";
mysql_connect($host,$usernm,$passwd);
mysql_select_db($database);
$sql = "SELECT * FROM usermaster WHERE User_name='".$Username."'"; // Please look at this too.
$result = mysql_query($sql) or die (mysql_error()); // dont put spaces in between it, else your code wont recognize it the query that needs to be executed
while ($row = mysql_fetch_array($result)){ // here too, you put a space between it
$Name=$row['Name'];
$Username=$row['User_name'];
$Password=$row['User_password'];
}
?>
Also, try to be specific. "It doesnt work" doesnt help us much, a specific error type is commonly helpful, plus any indication what the code should do (well, it was kinda obvious here, since its a login/register edit here, but for larger chunks of code it should always be explained)
另外,尽量具体。“它不起作用”对我们没有多大帮助,特定的错误类型通常很有帮助,加上代码应该做什么的任何指示(好吧,这里有点明显,因为它在这里进行登录/注册编辑,但对于更大的代码块它应该总是被解释)
Anyway, welcome to Stack Overflow
无论如何,欢迎来到 Stack Overflow
回答by aaama
<form action="Delegate_update.php" method="post">
Name
<input type="text" name= "Name" value= "<?php echo $row['Name']; ?> "size=10>
Username
<input type="text" name= "Username" value= "<?php echo $row['Username']; ?> "size=10>
Password
<input type="text" name= "Password" value= "<?php echo $row['Password']; ?>" size=17>
<input type="submit" name= "submit" value="Update">
</form>
look into this
看看这个
回答by Vennyvenance
please try these
请试试这些
<form action="Delegate_update.php" method="post">
Name
<input type="text" name= "Name" value= "<?php echo $row['Name']; ?> "size=10>
Username
<input type="text" name= "User_name" value= "<?php echo $row['User_name']; ?> "size=10>
Password
<input type="text" name= "User_password" value= "<?php echo $row['User_password']; ?>" size=17>
<input type="submit" name= "submit" value="Update">
</form>
回答by aman
<?php
include 'cdb.php';
$show=mysqli_query( $conn,"SELECT *FROM 'reg'");
while($row1= mysqli_fetch_array($show))
{
$id=$row1['id'];
$Name= $row1['name'];
$email = $row1['email'];
$username = $row1['username'];
$password= $row1['password'];
$birthm = $row1['bmonth'];
$birthd= $row1['bday'];
$birthy= $row1['byear'];
$gernder = $row1['gender'];
$phone= $row1['phone'];
$image=$row1['image'];
}
?>
<html>
<head><title>hey</head></title></head>
<body>
<form>
<table border="-2" bgcolor="pink" style="width: 12px; height: 100px;" >
<th>
id<input type="text" name="" style="width: 30px;" value= "<?php echo $row1['id']; ?>" >
</th>
<br>
<br>
<th>
name <input type="text" name="" style="width: 60px;" value= "<?php echo $row1['Name']; ?>" >
</th>
<th>
email<input type="text" name="" style="width: 60px;" value= "<?php echo $row1['email']; ?>" >
</th>
<th>
username<input type="hidden" name="" style="width: 60px;" value= "<?php echo $username['email']; ?>" >
</th>
<th>
password<input type="hidden" name="" style="width: 60px;" value= "<?php echo $row1['password']; ?>">
</ths>
<th>
birthday month<input type="text" name="" style="width: 60px;" value= "<?php echo $row1['birthm']; ?>">
</th>
<th>
birthday day<input type="text" name="" style="width: 60px;" value= "<?php echo $row1['birthd']; ?>">
</th>
<th>
birthday year<input type="text" name="" style="width: 60px;" value= "<?php echo $row1['birthy']; ?>" >
</th>
<th>
gender<input type="text" name="" style="width: 60px;" value= "<?php echo $row1['gender']; ?>">
</th>
<th>
phone number<input type="text" name="" style="width: 60px;" value= "<?php echo $row1['phone']; ?>">
</th>
<th>
<th>
image<input type="text" name="" style="width: 60px;" value= "<?php echo $row1['image']; ?>">
</th>
<th>
<font color="pink"> <a href="update.php">update</a></font>
</th>
</table>
</body>
</form>
</body>
</html>