【问题标题】:Display apostrophe from mysql table on webpage in image title attribute在图像标题属性中显示网页上 mysql 表中的撇号
【发布时间】:2015-05-20 23:24:13
【问题描述】:

我有一个表单,允许用户在 mysql 表中输入收缩(例如拉里的狗)> 代码如下:

<?php


<?php
if(isset($_POST['submit'])){

$title = mysqli_real_escape_string($conn,$_POST['title']);


$insertImageQ = "INSERT INTO images (title, name, keyword1, keyword2, keyword3, enter_date) VALUES('$title', '$name', '$keyword1','$keyword2','$keyword3',now())"; 


if(!mysqli_query($conn, $insertImageQ)){
die('error inserting new image');
}
$newImage = "one new image added";
}
}
?>

当我在网页上回显“标题”字段时,它可以正常工作,除非它在图像标题属性中回显。例如,

<?php
$slideShowQ = "SELECT * FROM slideshow";
$result= mysqli_query($connection, $slideShowQ) or die("error getting connected");

while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){

echo "<img class='lgImg'";
echo "id='";
echo $row['Id'];
echo "'";
echo "title='";
echo htmlentities($row['title']);
echo "'";
echo "alt='";
echo htmlentities($row['title']};
echo "'";
echo "src='_images/";
echo $row['name'];
echo "'/>";

}
?>
<?php
//release returned data
mysqli_free_result($result);
?>

在 javascript 文件中,我调用了一个函数,当鼠标悬停在图像上时,div id="topLeft" 将其 html 设置为标题。例如,

$('.lgImg').mouseover(function(){

var title =$(this).attr('title');
$('#topLeft').html(title);

当标题字段出现收缩时,撇号中的所有内容都会被切断。

另外,在另一个页面上,title 字段包含在一个从 mysql 表中绘制图像的 fancybox 应用程序中。标题字段在fancybox 应用程序中应有的显示,除非标题中有缩略,在这种情况下,图像甚至不会像在fancybox 应用程序中那样弹出。

另外,如果我用这段代码更新同一张图片,

<?php


if(isset($_POST['submit'])){



$title= mysqli_real_escape_string($conn,$_POST['title']);




$updateQ = "UPDATE images SET title='$title'"; 

if(!mysqli_query($conn, $updateQ)){
die('error inserting');
}



}
?>

表格中的字段变为空白,fancybox 不起作用。

总而言之,只要我不在图像标题属性中回显它,我就可以插入一个缩略词并回显该缩略词,但我无法使用缩略词更新该字段。

谁能帮我理解这个矛盾?

【问题讨论】:

    标签: php mysqli fancybox


    【解决方案1】:

    htmlentities() 默认情况下会留下单引号,当您在 html 属性中使用它们时,这将导致无效的 html。

    您可以像这样强制对单引号进行编码:

    htmlentities("'", ENT_QUOTES);
    

    更多信息the manual page

    【讨论】:

    • 谢谢丹杰姆。第一句话让我意识到我做错了什么。所以我从输出 title='Larry's dogs' 的 echo "title='" 更改为输出 title="Larry's dogs" 的 echo "title=\"" 。这非常有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-24
    • 2019-07-08
    • 2011-09-16
    • 2020-04-11
    相关资源
    最近更新 更多