[英]What is the correct validation to check a input is exists or not in database
Example my MySQL table 我的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
Update 更新
content_slug
is a unique key. 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'])}'
");
A bit long :) Here I want to know, what is the correct validation should I use if I want 有点长:)在这里我想知道,如果我需要,我应该使用什么是正确的验证
hello-world
=
current content_user
use the first if
hello-world
=
current content_user
使用第一个if
hello-world
<>
current content_user
and hello-world
already exists in database use the } else {
hello-world
<>
当前content_user
和hello-world
已存在于数据库中,请使用} else {
Let me know.. 让我知道..
I really don't think you want what you're asking for, but this does what you are asking for in one update query. 我真的不认为你想要的是什么,但这会在一次更新查询中满足你的要求。
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)}'
ADDITION 加成
I imagine you want to insert a new row in your table for the different user, in which case, you would need an insert statement. 我想你想在你的表中为不同的用户插入一个新行,在这种情况下,你需要一个insert语句。 If you want to insert a new row no matter what, then this should work for you:
如果你想插入一个新行,无论如何,那么这应该适合你:
$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)");
If, however, for whatever reason you only want to insert a new row if you don't have the same content_user
as before, then you can go with the ugliness here: 但是,如果由于某种原因你只想插入一个新行,如果你没有和以前一样的
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>
I think it would be something like this: 我想会是这样的:
$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)}'
");
<> means not equal, != should do as well The extra where will only update if content_slug is not equal to hello-world <>表示不相等,!=也应该这样做额外的地方只有在content_slug不等于hello-world时才会更新
I think your db-logic is flawed. 我认为你的db-logic存在缺陷。 but to answer your precise question: just append a UUID and it, whatever it is, will be unique.
但要回答你的确切问题:只需附加一个UUID即可,无论它是什么,它都是独一无二的。 but what is content_slug and why should it be unique?
但是content_slug是什么?为什么它应该是唯一的? that is your question for yourself.
这是你自己的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.