简体   繁体   English

html 类更改为 Attributes

[英]html classes are changed to Attributes

i'm trying to send an html code to database and then show the code in index.php this is what i use to send the html code to DB:我正在尝试将 html 代码发送到数据库,然后在 index.php 中显示代码,这是我用来将 html 代码发送到 DB 的内容:

when i quote the attributes i get sql syntax error当我引用属性时,出现 sql 语法错误

$result = "          <li>
                <a href=$Link class=external item-link item-content>
                  <div class=item-media><img src=$f_image class=lazy lazy-fade-in style=border-radius: 10px; width=42px height=42px></div>
                  <div class=item-inner>
                    <div class=item-title>
                      $Name <span class=test-bull>&bull;</span>
                      <div class=item-footer>$Info</div>
                    </div>
                    <div class=item-after></div>
                  </div>
                </a>
              </li>

              ";

$sql = "INSERT INTO table1 (test) VALUES ('$result')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
}else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

and this is what i use to show the code in index.php:这就是我用来在 index.php 中显示代码的内容:

<?php

      $sql = "SELECT test FROM table1";
       $result = $conn->query($sql);

 if ($result->num_rows > 0) {
    // output data of each row
     while($row = $result->fetch_assoc()) {
     echo $row ["test"];
 }
 } else {
    echo "0 results";
}

              ?>

now as you can see there is more than one class in my html code:现在你可以看到我的 html 代码中有不止一个类:

<a href=$Link class=external item-link item-content>

the problem is when i get the html code from database and show it in index.php the class are changed to attributes like this:问题是当我从数据库中获取 html 代码并在 index.php 中显示它时,类被更改为这样的属性:

<li>
                <a href="test" class="external" item-link="" item-content="">
                  <div class="item-media"><img src="img/2ff4e.aa.png" class="lazy" lazy-fade-in="" style="border-radius:" 10px;="" width="42px" height="42px"></div>
                  <div class="item-inner">
                    <div class="item-title">
                      test <span class="test-bull">•</span>
                      <div class="item-footer">test</div>
                    </div>
                    <div class="item-after"></div>
                  </div>
                </a>
              </li>

those two class item-link and item-content are now item-link="" and item-content=""这两个类 item-link 和 item-content 现在是 item-link="" 和 item-content=""

what i did wrong?我做错了什么? what i have to change/do?我必须改变/做什么?

sorry for my english对不起我的英语不好

You need to add quotes.您需要添加引号。 Try this:尝试这个:

$result = '          <li>
            <a href="'.$Link.'" class="external item-link item-content">
              <div class="item-media"><img src="'.$f_image.'" class="lazy lazy-fade-in" style="border-radius: 10px; width=42px height=42px"></div>
              <div class="item-inner">
                <div class="item-title">
                  '.$Name.' <span class="test-bull">&bull;</span>
                  <div class="item-footer">'.$Info.'</div>
                </div>
                <div class="item-after"></div>
              </div>
            </a>
          </li>

          ';

始终将变量括在大括号之间的字符串中,例如href={$Link}以便编译器可以准确确定变量在哪里以及如何将其值转换为字符串,特别是当变量不单独时(意味着任何其他数字、字母或符号直接与变量联系,如$Link之前的等号)

$result = "This is double quotes, I need html class :) simple <div 
class='class_name_under_single_q'> Content </div>";

$result = 'This is Single quotes, I need html class :) simple <div 
class="class_name_under_double_q"> Content </div>';

<?php
   $result = "<a href=".$Link." class=".external item-link item-content.">";
?>

if you use double quotes then use single quotes under double quotes .如果使用双引号,则在双引号下使用单引号。
But if you use single quotes then use double quotes under single quotes .但是,如果您使用单引号,则在单引号下使用双引号。

In HTML5 you always have to use quotes (' or ") around the value of html-attributes when the value contains a space or one of these characters: " ' ` = < >.在 HTML5 中,当 html-attributes 的值包含空格或以下字符之一时,您始终必须使用引号(' 或 ")将其括起来:" ' ` = < >。 To avoid the sql error, you have to mask the characters that may disturb in SQL context (you have always to do this when you inserting values into an SQL string):为了避免 sql 错误,您必须屏蔽在 SQL 上下文中可能会干扰的字符(在将值插入 SQL 字符串时,您必须始终这样做):

$result = '[…]<a href="'.htmlspecialchars($Link).'" class="external item-link item-content">[…]';
$html = $conn->real_escape_string($result);
$sql = "INSERT INTO table1 (test) VALUES ('".$html."')";
if ($conn->query($sql) === true) {
    echo "New record created successfully";
}

(assuming that $conn contains a mysqli-connection) The call of htmlspecialchars() is necessary because you change the context of the string in $Link to html. (假设$conn包含一个 mysqli 连接) htmlspecialchars() ) 的调用是必要的,因为您将$Link字符串的上下文更改为 html。

prepared statements are also possible:准备好的语句也是可能的:

$result = '[…]<a href="'.htmlspecialchars($Link).'" class="external item-link item-content">[…]';
$sql = "INSERT INTO table1 (test) VALUES (?)";
if($stmt = $conn->prepare($sql)) {
    $stmt->bind_param("s", $result);
    $stmt->execute();
}

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

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