繁体   English   中英

将PHP变量细分为HTML表单字段的问题

[英]Issues with subbing PHP variables into a HTML form field

我使用标签中的value属性将存储在PHP变量中的数据输入到HTML表单中时遇到问题。 问题似乎是在空格周围剪切了字符串,只有空格前面的字符串的第一部分才使它成为表格。 不含空格的字符串结尾处带有正斜杠。

该表单的想法是,客户可以在MySQL数据库上编辑关于它们的详细信息存储,然后使用update语句更改值。 因此,将值存储在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>";

?>

您缺少价值周围的引号。 您应该这样添加引号:

<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']);

首先,您必须将value封闭为撇号,如下所示:

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

并且使用htmlspecialchars打印之前还应从字符串中转义撇号和其他在HTML中具有特殊意义的字符:

$Name = htmlspecialchars($Name);

另外,假设CustomerID是唯一的,则在从数据库中获取数据时不需要循环。

使空格成为文本框的结束标签

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

也是电子邮件地址。

否则添加单引号(')

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM