php 尝试在 html 文本字段中显示 mysql 数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18443661/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:35:12  来源:igfitidea点击:

trying to display mysql data in an html text field

phphtmlmysql

提问by user2717967

I would like to be able to view and edit information contained within a table from my web browser however I can't for the life in me get the current values to pull though to an html text field.

我希望能够从我的 Web 浏览器查看和编辑表格中包含的信息,但是我一生都无法将当前值拉到 html 文本字段中。

Can anyone shed any light as im quite new to php?

任何人都可以说我对 php 很陌生吗?

Table name: request_details

表名:request_details

Column Names: id, name, email_address

列名:id、name、email_address

My PHP code is:

我的PHP代码是:

<?
$order = "SELECT * FROM request_details WHERE id='$id'";
$result = mysql_query($order);
$row = mysql_fetch_array($result);
?>

HTML Code

HTML代码

<form method="post" action="edit_data.php">
  <input type="hidden" name="id" value="<?php echo "$row[id]"?>">
    <tr>        
      <td>Name</td>
      <td>
        <input type="text" name="name" size="20" value="<?php echo "$row[name]"?>">
      </td>
    </tr>
    <tr>
      <td>Email Address</td>
      <td>
        <input type="text" name="email_address" size="40" value="<?php echo "$row[email_address]"?>">
      </td>
    </tr>
    <tr>
      <td align="right">
        <input type="submit" 
      name="submit value" value="Edit">
      </td>
    </tr>
  </form>

Right to make it a bit easier, when I use an actual ID not a variable it works, so for example

让它更容易一点是正确的,当我使用实际 ID 而不是变量时,它可以工作,例如

<?
  include "db.inc.php";//database connection
  $order = "SELECT * FROM request_details WHERE id='19'";
  $result = mysql_query($order);
  $row = mysql_fetch_array($result,MYSQL_ASSOC);
  ?>

The above displays, yet the below does not

上面显示,下面不显示

<?
  include "db.inc.php";//database connection
  $order = "SELECT * FROM request_details WHERE id='$id'";
  $result = mysql_query($order);
  $row = mysql_fetch_array($result,MYSQL_ASSOC);
  ?>

Ok, so the premise is I have two web pages, the first displays all the information I require

好吧,前提是我有两个网页,第一个显示我需要的所有信息

code is:

代码是:

<table>
    <tr>
        <td>
            <table border="1">
                <tr>
                    <td>Name</td>
                    <td>Telephone Number</td>
                    <td>Email Address</td>
                    <td>Venue</td>
                    <td>Event Date</td>
                    <td>Guests</td>
                    <td>Chair Cover Colour</td>
                    <td>Sash Colour</td>
                    <td>Price</td>
                    <td>Damage Deposit</td>
                    <td>Status</td>
                    <td>Notes</td>
                </tr>
                <tr>      

                    <?
                    $order = "SELECT * FROM request_details";
                    $result = mysql_query($order);
                    while ($row=mysql_fetch_array($result)){

                    echo ("<tr><td>$row[name]</td>");
                    echo ("<td>$row[telephone_number]</td>");
                    echo ("<td>$row[venue]</td>");
                    echo ("<td>$row[event_date]</td>");
                    echo ("<td>$row[guests]</td>");
                    echo ("<td>$row[cover_colour]</td>");
                    echo ("<td>$row[sash_colour]</td>");
                    echo ("<td>$row[price]</td>");
                    echo ("<td>$row[damage_deposit]</td>");
                    echo ("<td>$row[notes]</td>");      
                    echo ("<td><a href=\"edit_form.php?id=$row[id]\">View</a></td></tr>");
                    }
                    ?>
            </table>
        </td>
    </tr>
</table>

When i click on view it takes me to the following url:

当我单击查看时,它会将我带到以下网址:

edit_form.php?id=1

edit_form.php?id=1

for example

例如

edit_form.php?id=19

edit_form.php?id=19

The code for this page (where the information isn't displaying in the text field):

此页面的代码(信息未显示在文本字段中):

<table border=1>
    <tr>
        <td>
            <table>

                <?
                $order = "SELECT * FROM request_details WHERE id='$id'";
                $result = mysql_query($order);
                $row = mysql_fetch_array($result,MYSQL_ASSOC);
                ?>

                <form method="post" action="edit_data.php">
                    <tr>
                    <input type="hidden" name="id" value="<?php echo $row['id']; ?>" />
                    <tr>        
                        <td>Name</td>
                        <td>
                            <input type="text" name="name" size="20" value="<?php echo $row['name']; ?>">
                        </td>
                    </tr>
                    <tr>
                        <td>Email Address</td>
                        <td>
                            <input type="text" name="email_address" size="40" 
                                   value="<?php echo $row['email_address']; ?>">
                        </td>
                    </tr>
                    <tr>
                        <td align="right">
                            <input type="submit" name="submit value" value="Edit">
                        </td>
                    </tr>
                </form>
            </table>      
        </td>
    </tr>
</table>

回答by echo_Me

do them like that

那样做

   <input type="hidden" name="id" value="<?php echo $row['id'] ;?>" />

  <input type="text" name="name" size="20" value="<?php echo $row['name']; ?>" />

  <input type="text" name="email_address" size="40" value="<?php echo $row['email_address']; ?>" />

edit;

编辑;

sorry to say that your code is catastroph and messed up . many errors there i cant fix all. any way change your line to this line.

很抱歉,您的代码是灾难性的并且一团糟。那里有很多错误,我无法全部修复。以任何方式将您的线路更改为这条线路。

     echo "<td><a href=\"edit_form.php?id=$row['id']\">View</a></td></tr>"; 

and add in the file where you want display edit page , before your query this line

并在您要显示编辑页面的文件中添加,在您查询此行之前

  $id = $_GET['id'] ;

回答by Jordan57

You can do like this :

你可以这样做:

<input type="text" name="email_address" size="40" value="<?=$row['email_address']?>">

回答by Pranab Sharma

Change your php code to

将您的 php 代码更改为

$row = mysql_fetch_array($result,MYSQL_ASSOC);

$row = mysql_fetch_array($result,MYSQL_ASSOC);

Also change the HTML code like the following line

还要像下面这样更改 HTML 代码

    <input type="hidden" name="id" value="<?php echo $row['id'] ;?>" />

回答by Dorvalla

Your code is incomplete, it misses a definition within the call of the row. It doesnt know $row[name] because the variable should be defined by quotations.

您的代码不完整,它缺少行调用中的定义。它不知道 $row[name] 因为变量应该由引号定义。

<input type="text" name="name" size="20" value="<? echo $row['name']; ?>">
<input type="text" name="email_address" size="40" value="<? echo $row['email_address']; ?>">

Next to that, you are quoting on a variable, which is not needed. A variable doesnt have to be quoted at all. You quote the value type correctly, the double quote doesnt make sense.

接下来,您引用了一个不需要的变量。变量根本不需要被引用。您正确引用了值类型,双引号没有意义。

Your code is really messy btw. Try putting it correctly down with the appropiate syntax. (dont forget ; behind the variable)

顺便说一句,你的代码真的很乱。尝试使用适当的语法正确放置它。(不要忘记 ; 在变量后面)

回答by Pranab Sharma

If your URL is edit_form.php?id=19

如果您的网址是 edit_form.php?id=19

You need to write your query like:

您需要编写如下查询:

$order = "SELECT * FROM request_details WHERE id='$_GET[id]'";

回答by user3524093

i made corrections for the hole code and it works

我对孔代码进行了更正,并且可以正常工作

<html>
<head>
<title>PAGE Edit</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<table>
  <tr>
    <td align="center">EDIT DATA</td>
  </tr>
  <tr>
    <td>
      <table border="1">
      <?php
      include"db.inc.php";//database connection
      $order = "SELECT * FROM data_employees";
      $result = mysql_query($order);
      while ($row=mysql_fetch_array($result)){
      // Print "<th>Name:</th> <td>".$data['user'] . "</td> "; 
        echo ("<tr><td>".$row['name']."</td>");
        echo ("<td>".$row['employees_number']."</td>");
        echo ("<td>".$row['address']."</td>");
        echo ("<td><a href=edit_form.php?id=".$row['employees_number'].">Edit</a></td></tr>"); 
      }
      ?>
      </table>
    </td>
   </tr>
</table>
</body>
</html>

Second PART : Calling edit_form.php to edit

第二部分:调用 edit_form.php 进行编辑

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Form Edit Data</title>
</head>

<body>
<table border=1>
  <tr>
    <td align=center>Form Edit Employees Data</td>
  </tr>
  <tr>
    <td>
      <table>
      <?php
      $id=$_GET['id']; 

      include "db.inc.php";//database connection
      $order = "SELECT * FROM data_employees 
where employees_number='$id'";
      $result = mysql_query($order);
      $row = mysql_fetch_array($result);
      ?>
      <form method="post" action="edit_data.php">
      <input type="hidden" name="id" value="<?php echo "$row[employees_number]"?>">
        <tr>        
          <td>Name</td>
          <td>
            <input type="text" name="name" 
        size="20" value="<?php echo "$row[name]"?>">
          </td>
        </tr>
        <tr>
          <td>Address</td>
          <td>
            <input type="text" name="address" size="40" 
          value="<?php echo "$row[address]"?>">
          </td>
        </tr>
        <tr>
          <td align="right">
            <input type="submit" 
          name="submit value" value="Edit">
          </td>
        </tr>
      </form>
      </table>
    </td>
  </tr>
</table>
</body>
</html>

Finally calling edit_Data.php to update the DATA TABLE

最后调用edit_Data.php来更新DATA TABLE

<?php
//edit_data.php
$name=$_POST['name'];
$address=$_POST['address'];
$id=$_POST['id'];
include "db.inc.php";
$order = "UPDATE data_employees 
          SET name='$name', 
              address='$address' 
          WHERE 
          employees_number='$id'";
mysql_query($order);
header("location:edit.php");
?>

I hope that it's clear now ! :D

我希望现在清楚了!:D