Php 检查数据库中是否存在输入的正确验证是什么

Php 检查数据库中是否存在输入的正确验证是什么,php,mysql,Php,Mysql,示例my MySQL表 content_id content_user content_title content_slug --------------------------------------------------------------------- 1 1 Hello World hello-world 2 1 Hello Stackoverflow he

示例my 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\u 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=当前 内容\用户使用第一个if 如果您好,世界电流 内容\用户和hello world 已存在于数据库中,请使用 }否则{
让我知道..

我想应该是这样的:

 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)}'
           ");
表示不相等,!=也应如此
额外的where将只在content\u slug不等于hello world时更新。我认为您的数据库逻辑有缺陷。但要回答您的确切问题:只需附加一个UUID,它将是唯一的。但是content\u slug是什么,为什么它应该是唯一的?这是您自己的问题。

我真的不想知道您想要什么请求,但这会满足您在一个更新查询中的请求

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)");
但是,如果出于任何原因,如果您没有与以前相同的内容用户,那么您只想插入一个新行,那么您可以在这里使用丑陋:

$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"));

您的当前内容用户在哪里?它是在另一个表中还是在php变量中?@jswolf19它是一个当前用户id。比如说$\u SESSION['user\u id']对于2,你的意思是在这种情况下插入一个新行吗?假设用户2有一个slug“hello world”,你希望表的结尾是什么样子?@jswolf19我做了一个示例,仍然保留slug,但在结尾处替换为time$slug=$slug.-”。time;因此该行仍然有旧的content\u user,只有一个新的slug?嗯……好吧,让我们说content\u user=3使用相同的slug hello world?@wow-然后它会更新,因为基于您发布的内容的表内容,用户3正在测试而不是hello world。假设$input中存储的内容不是动态的,则仅计算行数的选择*有点贵。我认为最好使用SELECT COUNTid或其他什么。实际上,我使用slug是为了URL domain.com/hello-world/所以我需要保持它的唯一性。