【问题标题】:Fetching data from MySQL database using PHP, Displaying it in a form for editing使用 PHP 从 MySQL 数据库中获取数据,以表格形式显示以进行编辑
【发布时间】:2014-06-04 03:49:06
【问题描述】:

我是这方面的新手,我编写了下面的代码来从 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>

【问题讨论】:

  • 什么不起作用?你得到什么错误?您采取了哪些措施来解决此问题?
  • 尽可能使用mysqli而不是mysql
  • 你没有关闭你的表单标签,那么它应该如何知道它应该发布什么表单

标签: php mysql forms


【解决方案1】:
<?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>

【讨论】:

  • 您能否添加说明,说明您提出的解决方案实际上是如何回答原始问题的?
【解决方案2】:

请尝试这些

<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>

【讨论】:

    【解决方案3】:
    <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>
    

    调查一下

    【讨论】:

      【解决方案4】:

      玩转这段代码。专注于概念,在必要时进行编辑,使其能够

      <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>
      

      【讨论】:

      • 感谢所有帮助的人,抱歉我没有发布错误。会尝试你的建议,让你知道什么是有效的。再次感谢!
      【解决方案5】:
      <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>
      

      您一开始并没有关闭您的打开表单,而且您的代码非常混乱。我不会进入“使用 pdo 或 mysqli 语句,而不是 mysql”,这是你自己找出来的。此外,您在其下方打开和关闭了一个 php 标签,不确定那里需要什么。另一件事是您的代码引用了您没有发布的外部页面,因此如果那里没有工作,也可以方便地发布它。

      还请注意,表格中的 $row 数组变量之间有空格。您必须通过删除空格将它们链接在一起(请参阅我的编辑部分)。 PHP 不会原谅这些错误。

      然后是你的 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'];
          }
      ?>
      

      另外,尽量具体。 “它不起作用”对我们没有多大帮助,特定的错误类型通常很有帮助,加上代码应该做什么的任何指示(嗯,这里有点明显,因为它在这里是登录/注册编辑,但对于更大的代码块应该总是解释)

      不管怎样,欢迎来到 Stack Overflow

      【讨论】:

      • 谢谢 Dorvalla,我修复了代码中的基本错误(谢谢),但仍然收到错误“注意:未定义变量:D:\wamp\www\Swift_Registration\Delegate_edit.php 中的用户名在第 17 行"。再一次,我是 PHP/MySQL 的新手,所以请原谅我的基础知识。
      • mysql_connect($host,$usernm,$passwd); 是你的第 17 行,如果我计算正确的话。显然,您的用户名不是定义的变量。您是否编辑了您未在原始帖子中发布的内容? (否则进行编辑左右),对我来说,没有名为“用户名”的变量
      • 抱歉,我在您的旧帖子中计数,而不是在新帖子中,因此如果您接受我的帖子,它确实是一个不同的名称。我在那里提供的用于查找您的用户名的变量应该与您从上一页拍摄的 id 相对应。因此,要么将 id 转为名称(搜索该名称),要么简单地使用 id 而不是用户名作为用户 ID。 Oracle 在他的代码中有一部分是定义的,检查一下。
      最近更新 更多