これは私のphpコードです:
//Code for fetching content (view-me.php)
<?php
include('connect.php');
$head_title=$_GET['title'];
//echo $head_title;
$head_title=$mysqli->real_escape_string($head_title);
//echo $head_title;
$query="select title,content,pub_date from news where title='$head_title'";
$result=$mysqli->query($query);
if($result){
while(list($title,$content,$date)=$result->fetch_row())
{
echo '<div id="post_my_head">';
echo "<h1>".$title."</h1></div><br />";
echo '<div id="posted_date_time">'.$date.'</div>';
echo '<div id="post_my_content"><p>'.$content.'</p></div>';
}
$mysqli->close();
}
else
{
echo "Query failed";
}
?>
//Code for printing content:(news.php)
function print_content()
{
include('connect.php');
$query="select title,content,url,pub_date from news order by pub_date desc";
$result=$mysqli->query($query);
while(list($title,$content,$url,$date)=$result->fetch_row())
{
echo '<div id="post_my_head">';
echo "<h1><a href='view-me.php?title=".addslashes($title)."' id='my_post_link'>".$title."</a></h1></div><br />";
//$new_title=str_replace(" ","-",$title);
//echo "localhost/".$new_title."/";
echo '<div id="posted_date_time">Published:'.$date.'</div><br /><br />';
echo '<div id="post_my_content"><p>'.$content.'</p></div>';
}
$mysqli->close();
}
print_content();
$head_title に一重引用符または二重引用符があると失敗します。news.php で addslashes を使用しようとしましたが、機能しません。
$head_title が "America's Freedom" のような場合、コードは失敗します。この場合、$title は "America" になり、残りの文字列は自動的に切り捨てられます。したがって、URL は "localhost/?title=America" のようになります。 「localhost?title=America's Freedom」。mysql で使用されるデータ型はテキストです。コードは、一重引用符または二重引用符を使用しないすべてのクエリで完璧に機能します。