如何使用PHP和MySQL处理数据库信息与链接的变化


How to handle database information changes with links using PHP and MySQL?

假设我有一个带有产品页面的网站,该网站显示有关产品的信息,该信息由product.php使用对productname的GET操作生成,并从MySQL数据库获取信息。现在,产品名称偶尔会改变。这种情况很少发生,但确实会发生。

名称更改后,旧的链接到www.website.com/product.php?productname=Toaster400将不再工作明显。但是,我希望这些链接重定向到正确的页面。我还希望允许人们搜索"Toaster400",即使正确的名称是"Toaster400a"。

我只想到一个解决方案,那就是在MySQL中有一个"旧名字"的表。如果在product表中没有找到产品,那么我可以检查'old names'表,查看该旧名称与哪个产品相关。

这是最好的处理方法吗?谢谢你!

谢谢。编辑:

代码如下:

//This just initializes the database connection
require __DIR__ . "/resources/database.php";
//checks to see if they searched for anything
if(isset($_GET['search_text'])) {
    $name = $_GET['search_text'];
    $db = getDbConnection();
    //This is a stored procedure which is literally just a select for % . ? . %
    $stmt = $db->prepare("CALL get_products(?)");
    $stmt->bindParam(1, $name, PDO::PARAM_STR, 150);
    $isQueryOk = $stmt->execute();
    $results = array();

代码然后检查返回了多少行并相应地重定向。

您可以考虑将表中的'tags'列用于产品。例如:

表列
id->1
Product name-> test
tags->test

更新后,列的值将为

id->1
product name->testupdate
tags->test, testupdate

,通过这样做,您可以搜索重命名的产品。

表product: product_id, product_name, description等

表历史:product_id, product_name

url: example.com/../sample_name

select * from product where product_name='sample_name'

如果没有找到

select * from history h
left join product p on h.product_id=p.product_id
where h.product_name='sample_name'