首页 \ 问答 \ 如果PHP,PDO插入和更新stetement(PDO insert and update stetement with if PHP)

如果PHP,PDO插入和更新stetement(PDO insert and update stetement with if PHP)

我的疑问是:

   $select_userid_query = execute_pdo_query("SELECT user_id FROM users WHERE username='" . $_COOKIE['username'] . "'");

   if ($select_userid_query == 0){

    execute_pdo_query("INSERT INTO user_status
    (user_id,last_activity_time) VALUES
    ((SELECT user_id FROM users WHERE username='" . $_COOKIE['username'] . "'),NOW())"
     , array($user_id, $last_activity_time));

   }
    else{
        execute_pdo_query("UPDATE user_status SET user_id = (SELECT user_id FROM users WHERE username='" . $_COOKIE['username'] . "'),last_activity_time= NOW()", array($user_id, $last_activity_time));
        }

我想验证数据库中是否有记录,如果没有插入,如果只是用新的NOW()更新时间NOW()

任何想法我的语法错误?


my queries are :

   $select_userid_query = execute_pdo_query("SELECT user_id FROM users WHERE username='" . $_COOKIE['username'] . "'");

   if ($select_userid_query == 0){

    execute_pdo_query("INSERT INTO user_status
    (user_id,last_activity_time) VALUES
    ((SELECT user_id FROM users WHERE username='" . $_COOKIE['username'] . "'),NOW())"
     , array($user_id, $last_activity_time));

   }
    else{
        execute_pdo_query("UPDATE user_status SET user_id = (SELECT user_id FROM users WHERE username='" . $_COOKIE['username'] . "'),last_activity_time= NOW()", array($user_id, $last_activity_time));
        }

i want to verify if there is a record in the database and if not insert and if there's just update the time of that with the new NOW()

any ideas where's my error in the syntax?


原文:
更新时间:2022-10-29 13:10

最满意答案

我有一个相当复杂的应用程序,我已经做了两次(中止)尝试移植。 您的应用程序有几个部分需要更改,但我真正的问题是宝石兼容性。

简短的回答 - 如果你使用了很多第三方宝石,那么你应该等待。 如果你的应用程序相对纯净,那么它现在可能值得升级。

编辑:还有一件事要补充。 在开始移植之前,请确保您拥有一组强大的测试。 这将立即告诉你什么是有效的,什么不是,让你对你的港口充满信心。


I have a fairly complicated app that I've made two (aborted) attempts to port. There are several parts of your app that will need to be changed, although the real problem for me was gem compatibility.

Short answer - if you're using a lot of third party gems, then you should wait. If you're app is relatively pure, then its probably worth upgrading now.

Edit: one more thing to add. make sure you have a robust set of tests before you start porting. That will tell you instantly what's working and what's not, and give you confidence in your port.

相关问答

更多
  • OP的问题(和我的)是关于原型轨道宝石中的不兼容问题。 这可以通过直接从github使用原型轨道的“4.2”分支来解决。 修改您的Gemfile条目,如下所示: gem 'prototype-rails', github: 'rails/prototype-rails', branch: '4.2' 请注意,从Rails 5开始不支持prototype-rails gem,因此现在可以像删除此依赖项一样好。 The OP's issue (and mine) was to do with an incom ...
  • 升级到Rails 4.1.1后,我不确定为什么停止工作,但正如@steel建议的那样,它与辅助方法没有包含在我的特定控制器中有关。 添加include ApplicationHelper到我的控制器的顶部将起作用,我可能已经更进一步将它添加到ApplicationController类,因为我需要所有控制器可用的方法。 最后我选择了另一种解决方案: 首先,我将last_page_url从ApplicationHelper到ApplicationController类,以便我的所有控制器都可以访问它。 然后我 ...
  • 其中一颗宝石正在使用它。 或者你,在其他控制器中。 将before_filter所有用法替换为before_action并祈祷所有的宝石都保持足够的能力,以便在出现错误之前进行相同的操作。 One of the gems is using it. Or you, in other controllers. Replace all your usages of before_filter to before_action and pray that all your gems are maintained e ...
  • 从rails -T您可以看到可用的任务。 这个问题的两个相关问题是 rails test # Runs all tests in test folder except system ones rails test:system # Run system tests only 从中我们可以看到rails test不会运行系统测试。 为此,您需要运行rails test:system ,iirc,您可以使用rails test:system test运行所有rails test:syst ...
  • vestal_versions gem依赖于ActiveRecord 3.0 。 您需要在vestal_versions未指定vestal_versions gem的版本,或指定为2.0.0 ,然后: bundle update rails vestal_versions vestal_versions gem has dependency to ActiveRecord 3.0. You need to unspecify version of vestal_versions gem in your G ...
  • 通过以下命令gem uninstall rails系统中安装的所有版本的rails : gem uninstall rails 它应该提示你下面的东西 选择gem卸载: 1. rails-4.2.6 2. rails-5.1.4 3.所有版本 选择选项“所有版本”并全部卸载 安装所需的导轨版本: gem install rails --version=5.1.4 rails -v 5.1.4 更新 :由于您提到使用bundle install ,请检查Gemfile中是否使用5.1.4指定了rails 。 ...
  • 这些轨道剧的情节对我帮助很大。 http://railscasts.com/episodes/279-understanding-the-asset-pipeline http://railscasts.com/episodes/282-upgrading-to-rails-3-1 http://railscasts.com/episodes/318-upgrading-to-rails-3-2 摘要: 设置你的Gemfile group :assets do gem 'sass-rails' ge ...
  • 问题是铁路还原为2.3.5。 我不得不卸载rails并设置一个gemset,如下所述: 每次我关闭终端轨道默认从3.0.3恢复到2.3.5? The issue was that rails reverted back to 2.3.5. I had to uninstall rails and set up a gemset as described here: Every time I close terminal rails defaults back to 2.3.5 from 3.0.3?
  • Bootstrap Sprockets提供单独的Bootstrap组件,以便于调试。 再次阅读bootstrap-rubygem中的说明 ,你会发现没有假设是bootstrap-sprockets的导入。 // app/assets/stylesheets/application.scss @import "bootstrap-sprockets"; // Remove this line 然而,假设它是一个javascript需要它。 这可以替换为仅用于引导的需求,以便更快地进行编译(您目前正在进行编译) ...
  • 我有一个相当复杂的应用程序,我已经做了两次(中止)尝试移植。 您的应用程序有几个部分需要更改,但我真正的问题是宝石兼容性。 简短的回答 - 如果你使用了很多第三方宝石,那么你应该等待。 如果你的应用程序相对纯净,那么它现在可能值得升级。 编辑:还有一件事要补充。 在开始移植之前,请确保您拥有一组强大的测试。 这将立即告诉你什么是有效的,什么不是,让你对你的港口充满信心。 I have a fairly complicated app that I've made two (aborted) attempts ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)