簡體   English   中英

在數據庫中檢查輸入是否存在的正確驗證是什么

[英]What is the correct validation to check a input is exists or not in database

我的MySQL表示例

content_id  content_user  content_title        content_slug
---------------------------------------------------------------------
1           1             Hello World          hello-world
2           1             Hello Stackoverflow  hello-stackoverflow
3           2             Fix me               fix-me
4           3             Testing              testing

更新


content_slug是一個唯一的密鑰。

$input = 'Hello World';
$slug  = function_slug($input); // this will be hello-world

/* begin the validation */
$query = $db->query("SELECT * 
                    FROM tbl_content 
                    WHERE content_slug='{$slug}'
                    ");
$data  = $db->fetch($query);
$check = $db->num_rows($query);

if($check == 1) {
$seo = $slug;
 } else {
$seo  = $slug.'-'.time();
}
/* end the validation */

$db->query("UPDATE tbl_content 
            SET content_slug= '{$db->escape($seo)}'
            WHERE content_id ='{$db->escape($id)}'
            AND content_user ='{$db->escape($_SESSION['user_id'])}'
           ");

有點長:)在這里我想知道,如果我需要,我應該使用什么是正確的驗證

  • 如果hello-world = current content_user使用第一個if
  • 如果hello-world <>當前content_userhello-world已存在於數據庫中,請使用} else {

讓我知道..

我真的不認為你想要的是什么,但這會在一次更新查詢中滿足你的要求。

UPDATE tbl_content 
            SET content_slug= IF(content_user = '{$db->escape($_SESSION['user_id'])}',
                                 content_slug,
                                 CONCAT(content_slug, '-',  DATE_FORMAT(now(), '%Y%m%d%H%i%s%f')))
            WHERE content_id ='{$db->escape($id)}'

加成

我想你想在你的表中為不同的用戶插入一個新行,在這種情況下,你需要一個insert語句。 如果你想插入一個新行,無論如何,那么這應該適合你:

$slug = "'$db->escape($slug)'";
$db->query("INSERT INTO tbl_content (content_user, content_title, content_slug)
            SELECT '{$db->escape($_SESSION['user_id'])}', '{$db->escape($title)}',
                   IF(EXISTS(SELECT content_id FROM tbl_content WHERE content_slug = $slug),
                      CONCAT($slug, DATE_FORMAT(now(), '-%Y%m%d%H%i%s%f')), $slug)");

但是,如果由於某種原因你只想插入一個新行,如果你沒有和以前一樣的content_user ,那么你可以在這里找到丑陋的地方:

$slug = "'{$db->escape($slug)}'";
$user = "'{$db->escape($_SESSION['user_id'])}'";
$db->query("INSERT INTO tbl_content (content_user, content_title, content_slug)
            SELECT $user, '{$db->escape($title)}',
                   IF(EXISTS(SELECT content_id FROM tbl_content WHERE content_slug = $slug),
                      CONCAT($slug, DATE_FORMAT(now(), '-%Y%m%d%H%i%s%f')), $slug)
            FROM tbl_content
            WHERE NOT EXISTS(SELECT content_id FROM tbl_content
                             WHERE content_slug = $slug AND content_user = $user)
            LIMIT 1"));
 UPDATE <table> SET <field> = <new value> **WHERE** <current User> != <hello world>

我想會是這樣的:

$input = 'Hello World';
$slug1  = function_slug($input); // this will be hello-world

$query = $db->query("SELECT * 
                    FROM tbl_content 
                    WHERE content_slug='{$slug1}'
                    ");
$data  = $db->fetch($query);
$check = $db->num_rows($query);

if($check == 1)
$slug2  = $slug1.'-'.time();
$db->query("UPDATE tbl_content 
            SET content_slug= '{$db->escape($slug2)}'
            WHERE content_id ='{$db->escape($id)}'
            AND content_user ='{$db->escape($_SESSION['user_id'])}'
            AND content_slug <> '{$db->escape($slug1)}'
           ");

<>表示不相等,!=也應該這樣做額外的地方只有在content_slug不等於hello-world時才會更新

我認為你的db-logic存在缺陷。 但要回答你的確切問題:只需附加一個UUID即可,無論它是什么,它都是獨一無二的。 但是content_slug是什么?為什么它應該是唯一的? 這是你自己的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM