php - 将 PHP 变量代入 HTML 表单字段的问题

标签 php html mysql

我在使用标记中的值属性将存储在 PHP 变量中的数据输入到 HTML 表单时遇到问题。问题似乎是字符串在空格周围被切割,其中只有字符串的第一部分在空格之前进入表格。没有空格的字符串在末尾带有正斜杠。

该表单的想法是,客户可以编辑 MySQL 数据库中关于他们的详细信息存储,然后使用更新语句更改值。因此,值在 PHP 变量中的存储方式非常重要。

例子:

http://i.imgur.com/pvfYApy.png

<?php
//session start//
session_start();

//Connect to the server and database//
$con=mysqli_connect("127.0.0.1","root","","mia");

//Generate the sql statement for retrieval of customer details//
$customerdetails="SELECT Name, BusinessName, Address, Postcode, TelNo, Email
                  FROM customer
                  WHERE CustomerID = " . $_SESSION['CustomerID'] . "";

//execute the 'Customer details retrieve' sql statement//
$result= mysqli_query($con, $customerdetails);

while ($row = mysqli_fetch_array($result))
    {
        $Name='' . $row['Name'] . '';
        $BusinessName=$row['BusinessName'];
        $Addresss=$row['Address'];
        $Postcode=$row['Postcode'];
        $TelNo=$row['TelNo'];
        $Email=$row['Email'];
    }

//Write the web page//
echo "<!Doctype html>
<html>
<head>
<title>
    Edit Details
</title>
</head>

<body>
<h1>Edit Details</h1>
<p>Please change values below where apropriate</p>
<form name='input'
          action='../php/update_customer.php'
          method='post'>
        Name:           <input type='string'
                               name='Name' value=" . $Name ."/> </br>
        Business Name:  <input type='text'
                               name='BusinessName' value=" . $BusinessName ."/> </br>
        Address:        <input type='text'
                               name='Address' value=" . $Addresss ."/> </br>                       
        Post Code:      <input type='text'
                               name='Postcode' value=" . $Postcode ."/> </br>
        Telephone No:   <input type='text'
                               name='TelNo' value=" . $TelNo ."/> </br>
        Email Address:  <input type='text'
                               name='Email' value=" . $Email ."/> </br>
        <input type='submit'   value='Update'/>
    </form>

<p>" . $Name . "</p>
<p>" . $_SESSION['CustomerID'] . "</p>
</body>
</html>";

?>

最佳答案

您的值(value)周围缺少引号。你应该像这样添加引号:

<input type='text' name='Name' value='" . $Name ."'/>

请注意第一个双引号之前和第二个双引号之后的单引号。

另一个问题是您使用的类型无效。 string 不是有效的输入类型,它应该是文本。

您最大的问题可能是您正在获取一个数组,然后对其进行循环,并在每次迭代中覆盖您稍后在表单中使用的所有变量。因此,您将始终获得您在数据库中找到的最后一条记录的名称、地址等。

除非您在将值插入数据库之前对其进行转义,否则您还应该确保它们不包含任何特殊的 html 字符。您可以像这样转义特殊字符:

$Name = htmlspecialchars($row['Name']);
$BusinessName = htmlspecialchars($row['BusinessName']);
$Addresss = htmlspecialchars($row['Address']);
$Postcode = htmlspecialchars($row['Postcode']);
$TelNo = htmlspecialchars($row['TelNo']);
$Email = htmlspecialchars($row['Email']);

关于php - 将 PHP 变量代入 HTML 表单字段的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18292238/

相关文章:

php - 未定义偏移量: 0 Error in preg_match

php - Symfony ExceptionHandler 抛出关于自身的错误

javascript - Amature javascript 引用文本文件

Mysql子查询返回多行

Mysql查询求和十进制数

php - Symfony 网页包 : Could not find the entrypoints file from Webpack

php - 无法确定哪个查询实际上更快

javascript - 允许子 iframe(跨站点)访问 window.top

php - 如何使用 Laravel 将附件文件发送到电子邮件

mysql - 如果表中不存在则添加列