简体   繁体   English

以 HTML 形式显示数据

[英]Displaying data in HTML form

I am learning to put data in my database using php mysqli prepared statements.我正在学习使用 php mysqli 准备好的语句将数据放入我的数据库中。 I have the data going into the data base by using this code.我使用此代码将数据输入数据库。

$FirstName=ucwords($_POST['fname']);
$LastName=ucwords($_POST['lname'], "-'");
$Customer=$LastName."  ".$FirstName;

$conn = new mysqli($host,$user,$password,$db);
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
$stmt = $conn->prepare("INSERT INTO customers (FirstName, LastName, Customer) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $FirstName, $LastName, $Customer);
$stmt->execute();
$conn->close();

This is working very well.这工作得很好。 Especially with hyphenated names or names with an apostrophy such as Pete O'Brian.尤其是连字符名称或带有撇号的名称,例如 Pete O'Brian。

Now then while trying to retrieve the information back out of the database I am using the following code.现在,在尝试从数据库中检索信息时,我正在使用以下代码。

$conn = new mysqli($host,$user,$password,$db);

if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
$stmt = $conn -> prepare("SELECT Customer, Instrument1 FROM tblinvoice WHERE InvID = ?");

$stmt->bind_param("i", $tempid);
$stmt->execute();
$stmt -> bind_result($cust, $inst);
$stmt -> fetch();
$cust = mysqli_real_escape_string($conn, $cust);
$stmt -> close();
$conn -> close();

BUT the above output O\\ for a last name of O'Brian.但是上面的输出 O\\ 是 O'Brian 的姓氏。 If I remove the mysqli_real_escape_string($conn, $cust) and just use the bound value of $cust I simply get O instead of O'Brian.如果我删除mysqli_real_escape_string($conn, $cust)并只使用$cust的绑定值,我只会得到 O 而不是 O'Brian。

Can anyone tell me what I am not doing or what I am doing wrong here?谁能告诉我我没有做什么或我在这里做错了什么?

always use htmlspecialchars() in content from db that are going to show in html.始终使用htmlspecialchars()在来自 db 的内容中,这些内容将在 html 中显示。

echo htmlspecialchars($yourresult['yourfield'], ENT_QUOTES);

We should always use htmlspecialchars when filling HTML form input fields values.在填写 HTML 表单输入字段值时,我们应该始终使用 htmlspecialchars。

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

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