无法通过#,",&在$ _GET中

时间:2015-11-04 21:34:38

标签: php mysql

我提供了一个非常简单的测试数据集和我的代码来尝试解决此问题。如果在我找不到之前已经问过这个问题。我从书本和网络上自学成才,所以这不是流畅的编程。我希望我能提供足够的信息。

数据集包含一个表'comments',其中包含字段id(自动增量),名称(文本),注释(textarea),日期,显示(枚举) mysql连接排序规则以及所有文本和文本区域字段都设置为utf8_general_ci

The <head> includes 
<meta http-equiv="Content-Type" content="text/html" charset="UTF-8" /> 

所有post和get语句都按如下方式转义

$fieldname =  mysqli_real_escape_string($dbc, $_POST['fieldname']);

我的代码成功地将问题字符输入到数据库中,如果我只是想显示它们就会成功检索它们,但在现实世界中,我需要审核用户条目以确保它们适合公众查看,我还需要能够编辑它们。当我打开我的评论编辑页面时,在&amp;在一个字段中消失,如果有#或&#34;在任何领域都没有任何东西被检索。

首先是comments-admin.php页面:

   <?php
    $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
    or die('Error connecting to MySQL server.');
    ?>
        <table width="100%"><tr><td>
        <b>ID:</b></td><td>
        <b>Name:</b></td><td align="left">
        <b>Comments:</b></td><td>
        <b>Date Posted:</b></td><td>
        <b>Display:</b></td></tr><tr><td>
    <?php
        $query = "SELECT * FROM comments ORDER BY date DESC";
        $result = mysqli_query($dbc,$query);
        while ($row=mysqli_fetch_array($result)){
        echo '<tr valign="top" ><td>';
        echo  $row['id'] . '</td><td>';
        echo  $row['name'] . '</td><td>'; 
        echo  $row['comments'] . '</td><td>'; 
        echo  $row['date'] . '</td><td>'; 
        echo  $row['display'] . '</td><td>'; 
        echo  '<td><a href="comments-edit.php?id=' . $row['id'] . 
              '&amp;name=' . $row['name'] . 
              '&amp;comments=' . $row['comments'] . 
              '&amp;date=' . $row['date'] . 
              '&amp;display=' . $row['display'] . 
              '">Edit</a></td></tr>';
          }
          ?>
          </table>

这会产生一个看起来像这样的表(没有装饰)。

ID: Name:     Comments:                    Date Posted:  Display:
12  Fred    Just wanted to say "Thank You"  2015-11-04    No        Edit
11  Ricky   I live at #2, Anywhere Street.  2015-11-04    No        Edit
9   Ethel   Why can't I make this work!     2015-10-24   Yes        Edit
4   Lucy    Went to A&W today               2015-04-23   Yes        Edit

&#34;编辑打开我的comments-edit.php表单。在这个例子中,唯一一个将正确打开的是#9。 #11&amp; 12根本不会检索任何东西,#4在&amp;后面没有任何东西。

评论编辑的代码如下:

<?php
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
  or die('Error connecting to MySQL server.');

 if (isset($_GET['id']) &&
   isset($_GET['name']) && 
   isset($_GET['comments']) && 
   isset($_GET['date'])) 
{

// Grab the data from the GET
    $id = ( $_GET['id']);
    $name =  mysqli_real_escape_string($dbc, $_GET['name']);
    $comments =  mysqli_real_escape_string($dbc, $_GET['comments']);
    $date =  ( $_GET['date']);
}
else if (isset($_POST['id']) && 
    isset($_POST['name']) && 
    isset($_POST['comments']) && 
    isset($_POST['date']) && 
    isset($_POST['display']))  
{
// Grab the data from the POST
    $id =  ( $_POST['id']);
    $name =  mysqli_real_escape_string($dbc, $_POST['name']);
    $comments =  mysqli_real_escape_string($dbc, $_POST['comments']);
    $date =  ( $_POST['date']);
    $display =  ( $_POST['display']);
    }

   <form class="gap" method="post" action="comments-update.php">
  <input type="hidden" name="ud_id" value="<?php echo "$id;" ?>" />
<p class="gap"><b>Select "Yes" before clicking "Edit Data" to confirm any changes</b></p>
<input type="radio" name="confirm" value="Yes" /> Yes 
<input type="radio" name="confirm" value="No" checked="checked" /> No 
<input type="submit" value="Edit Data" name="update" /><br />   

<label class="appsm">Comment Date:</label>
<input type="text" size="30" name="ud_date" value="<?php echo $date; ?>" 

<label class="appsm">ID:</label>
<input type="text" size="5" name="ud_id" value="<?php echo $id; ?>" />

<label class="appsm">Display:</label>
      <select id="ud_display" name="ud_display">
        <option value="yes" <?php if (!empty($display) && $display == 'yes') echo 'selected = "selected"'; ?>>Yes</option>
        <option value="no" <?php if (!empty($display) && $display == 'no') echo 'selected = "selected"'; ?>>No</option>
      </select><br />

<label class="appsm">Name:</label>
<input type="text" size="50" name="ud_name" value="<?php echo $name; ?>" />
<br />
<label class="appsm">comments:</label>
<textarea name="ud_comments" wrap="physical" class="left" cols="105" rows="2">
<?php echo stripslashes($comments); ?></textarea>

</form>

我确定我错过了一些非常简单的事情并且我已经搜索了我的书籍和网络,但我还没有找到这个具体问题的答案。我真的希望能够删除所有警告,不要在我的真实网站上使用这些字符,特别是因为用户很少阅读说明。

1 个答案:

答案 0 :(得分:0)

如上面的评论中所述,您需要对在URL中传递的内容进行编码。

但我的问题是,为什么在将数据从数据库中删除后,将所有信息传递给第二页?还更改了代码以使用heredoc block而不是所有echo()语句。这是使代码更具可读性的便捷方式。最后,将数据输出到HTML要求您使用htmlspecialchars()转义它。我使用array_map()快速转义整个数据库记录。

<?php
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die('Error connecting to MySQL server.');
?>
    <table width="100%"><tr><td>
    <b>ID:</b></td><td>
    <b>Name:</b></td><td align="left">
    <b>Comments:</b></td><td>
    <b>Date Posted:</b></td><td>
    <b>Display:</b></td></tr><tr><td>
<?php
    $query = "SELECT * FROM comments ORDER BY date DESC";
    $result = mysqli_query($dbc,$query);
    while ($row=mysqli_fetch_array($result)){
        $row = array_map('htmlspecialchars', $row);
        echo <<< HTML
<tr valign="top">
    <td>$row[id]</td>
    <td>$row[name]</td>
    <td>$row[comments]</td>
    <td>$row[date]</td>
    <td>$row[display]</td> 
    <td><a href="comments-edit.php?id=$row[id]">Edit</a></td>
</tr>

HTML;
      }
      ?>
      </table>

你的第二个档案。请注意,$ _POST和$ _GET都合并到超全局$ _REQUEST中,但您应该知道自己的页面将如何发送数据。 HTML输出再次被转义。

<?php
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die('Error connecting to MySQL server.');

if (isset($_REQUEST['id'])) {
    $id = (int)$_REQUEST['id'];
    $query = "SELECT * FROM comments WHERE id=$id";
    $result = mysqli_query($dbc,$query);
    $row=mysqli_fetch_array($result);
}
if (!isset($_REQUEST['id']) || !$row) {
    // you should do something in this case
    return false;
}
$id = htmlspecialchars($row['id']);
$name = htmlspecialchars($row['name']);
$comments = htmlspecialchars($row['comments']);
$date = htmlspecialchars($row['date']);
$display = htmlspecialchars($row['display']);
?>

<form class="gap" method="post" action="comments-update.php">
<input type="hidden" name="ud_id" value="<?php echo $id ?>" />
...