如何自定义 joomla db sql 错误消息以及如何防止重复条目

发布于 2024-12-19 15:45:48 字数 551 浏览 4 评论 0原文

我的演出时间表包含以下字段,

id,名称,演出时间,其中id是一个int类型自动递增字段(pK)。 showtime时间类型唯一字段。

当我尝试将 showtime(重复值)添加到 showtime 字段($row->store())时,它显示以下 joomla 错误消息。(我使用 $row->getError()方法)

    TableShowTime: :store failed
    Duplicate entry '10:30:00' for key 'showtime' SQL=INSERT INTO `jos_myextension_showtime` (`id`,`name`) VALUES ('0','evening')

我想知道有什么方法可以只显示数据库错误消息而不显示sql查询。 我有一个想法在插入之前使用查询检查重复值,这是一个很好的做法吗?请帮助。

my showtime table contains following fields,

id, name, showtime where id is a int type auto-increment field(pK). showtime time type unique field .

when I try to add a showtime(duplicate value) to showtime field($row->store()) it shows the following joomla error message.(I used $row->getError() method)

    TableShowTime: :store failed
    Duplicate entry '10:30:00' for key 'showtime' SQL=INSERT INTO `jos_myextension_showtime` (`id`,`name`) VALUES ('0','evening')

I want to know is there any way to show only the db error message without showing sql query.
I have an idea to check the duplicate values using a query before insert, is it a good practice? Plz Help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里寻她 2024-12-26 15:45:48

我认为如果您不想显示查询,那么您也不应该显示错误消息的其他部分(不要告诉用户您正在尝试插入重复的键)。对此的简单修复可能是这样的:

if ( $row>getError() ) {
   echo "Could not store [...]";
}

如果这是自定义组件,您还可以修改表类来自定义这些错误消息,甚至根据错误号显示不同的错误消息。

我希望它有帮助!

I think that if you don't want to display the query, then you shouldn't show the other part of the error message neither (don't tell the user you're trying to insert a duplicate key). A simple fix to this might be something like this:

if ( $row>getError() ) {
   echo "Could not store [...]";
}

If this is a custom component, you could also modify your table class to customize these error message, or even show distinct error messages depending on the error number.

I hope it helped!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文