可变网址 PHP 和 MySQL


Variable URL PHP and MySQL

只是尝试在URL上传递一个变量,以便在回显时我可以单击它并根据数据库记录打开它自己的内容。现在这个显示了数据库中的所有记录,但我试图做的是传递一个 URL,以便每个博客 ID 都有自己的 URL,当单击它时将打开单个条目而不是所有条目。

编辑 现在,我可以显示带有 ID 的条目行,其中"ID"末尾有 URL 变量。我是否需要创建另一个查询来回显我的迷你博客上的单个条目?

<?
$db = // connection to db and authentication to connecting to db;
#$postID = $_GET['postID']; // I'm thinking to use a $_GET global variable to work with URL variable
$command = "select * from $table_name"; // I'm thinking to add the Id here or something or create another query to echo the linked URL 'viewblog.php?postID=$data->blogID'
$result = $db->query($command);
while ($data = $result->fetch_object()) {
echo "<TR><TD><a href='viewblog.php?postID=$data->blogID'>".$data->blogID."</a></TD>";
echo "<TD>".$data->author."</TD>";
echo "<TD>".$data->date."</TD>";
echo "<TD>".$data->entry."</TD></TR>'n";
}
$result->free();
$db->close;

为什么这个脚本提供所有条目?

因为发送到数据库的最终查询类似于

select * from TABLE_NAME

这将返回所有条目,因为您在选择之后使用星号 *

如果执行的最终查询包含"blogID",则可以在检索结果并开始获取它们之前获得您所要求的内容。

http://www.w3schools.com/sql/sql_where.asp

您还应该在回显结果中使用获取或发布 ID(以便在单击时,每个博客在链接中都有自己的 ID)。

可能是这样的

$postID = $_GET['postID']; 
//Add filtering by id to select statement
$command = "select * from '$table_name' obj WHERE obj.blogID = '$postID'";
$result = $db->query($command);
while($data = $result->fetch_assoc()){
$data['blogID'] = $postID; 
//Add ID to echoed link 
echo "<TR><TD><a href='viewblog.php?postID='".$data['blogID']."> Some Blog  (ID: ".$data['blogID'].") </a> </TD>"; 
echo "<TD>".$data['author']."</TD>";
echo "<TD>".$data['date']."</TD>";
echo "<TD>".$data['entry']."</TD></TR>'n";
}

注意有关此代码的安全问题。您应该使用更安全的方式来执行此操作。我只是在解释结果。

至于自动增量,可以在您首次创建表时启动。这适用于将新行插入数据库的情况。使用"自动增量"时,无需手动提供 ID。

http://www.w3schools.com/sql/sql_autoincrement.asp

注意:HTML BR 元素不应该在 TABLE 结构中使用。

希望对您有所帮助。

您可以创建一些这样的函数来返回基于 url 的单个帖子

function single_blog($Post_id){
$sql = "SELECT * FROM your_table WHERE post_id = ? LIMIT 1";
$stmt = $this->db->prepare($sql);
$stmt->execute(array($Post_id);
return $stmt->fetch();
}

您正在从表中选择所有条目。使用以下内容:

$db = // connection to db and authentication to connecting to db;
$postID = $_GET['postID']; // ??
$db->real_escape_string(trim($postID));
$command = "select * from $table_name WHERE `postID`=$postID";
$result = $db->query($command);
// Ensure results before outputting
if ($result->num_rows) while($data = $result->fetch_assoc()){
  $data['blogID'] = $postID;
  echo "<TR><TD><a href='viewblog.php?postID='>".$data['blogID']."</a> </TD>"; //??
  echo "<TD>".$data['author']."</TD>";
  echo "<TD>".$data['date']."<BR></TD>";
  echo "<TD>".$data['entry']."</TD></TR>'n";
} else echo "No entry found!";
$result->free();
$db->close;
<?php
//$db connect to database
// Entry form sanitation of $_POST
// Insert PHP file to MySQL
// View all blog posts
$postID = $_GET['postID']; // I guess I should sanitize this as well
if (!empty($postID)) {
  $command = "select * from $table_name where blogID = $postID";
  $result = $db->query($command);
  while ($data = $result->fetch_object()) {
    $postID = $data->blogID;
    echo "<TR><TD>".$postID."</TD>";
    echo "<TD>".$data->author."</TD>";
    echo "<TD>".$data->date."</TD>";
    echo "<TD>".$data->entry."</TD></TR>'n";
  }
  $result->free();  
}
else {
$command = "select * from $table_name"; 
$result = $db->query($command);
while ($data = $result->fetch_object()) {
   $postID = $data->blogID;
   echo "<TR><TD><a href='viewblog.php?postID=$postID'>".$postID."</a></TD>";
  echo "<TD>".$data->author."</TD>";
  echo "<TD>".$data->date."</TD>";
  echo "<TD>".$data->entry."</TD></TR>'n";
}
$result->free();
}

$db->close;
?>