草庐IT

php - 关于重复键更新语法 php/mysql 错误

coder 2023-10-13 原文

我正在尝试执行一个查询,如果“ID”存在则更新,如果不存在则插入。当尝试运行以下查询时,我可以看到插入了如此多的重复记录。我不知道我做错了什么。我的查询如下。 Id 是'Autoincrement' 和'KEY'。我没有收到任何查询错误。数据库是Mysql

    while ($i < $size) {
        $sl= $_POST['sl'][$i];
        $item_id= $_POST['item_id'][$i];
        $item_name= $_POST['item_name'][$i];
        $prod_description=$_POST['prod_description'][$i];
        $prod_description= mysql_real_escape_string($prod_description);
        $item_quantity= $_POST['item_quantity'][$i];
        $item_units= $_POST['item_units'][$i];
        $unitprice= $_POST['unitprice'][$i];
        $total=$_POST['total'][$i];
        $currency_selected=$_POST['currency_change'][$i];
        $total_inr= $_POST['total_inr'][$i];
            $id = $_POST['id'][$i];
        $item_quantity_sup= $_POST['item_quantity_sup'][$i];
        $slab_range= $_POST['slab_range'][$i];
        $item_units_sup= $_POST['item_units_sup'][$i];
        $item_partno= $_POST['item_partno'][$i];
        $ifmain= $_POST['ifmain'][$i];
        $sup_itempartno = $_POST['sup_itempartno'][$i];

$query = "INSERT INTO comparitive_st_sup (
id, 
tender_id, 
item_id, 
ifmain, 
slno, 
item_name, 
item_partno, 
prod_description, 
sup_itempartno, 
currency, 
slab_range, 
qty, 
total_inr, 
qty_sup, 
item_units, 
item_units_sup, 
unitprice, 
total, 
supplier_name
) 
VALUES (
$id, 
'$tender_id', 
'$item_id',
'$ifmain', 
'$sl', 
'$item_name', 
'$item_partno',
'$prod_description', 
'$sup_itempartno', 
'$currency_selected', 
'$slab_range', 
'$item_quantity', 
'$total_inr', 
'$item_quantity_sup', 
'$item_units', 
'$item_units_sup', 
'$unitprice', 
'$total', 
'$supplier_name2'
)
ON DUPLICATE KEY UPDATE 
ifmain='$ifmain', 
slno = '$sl',
item_name = '$item_name',
item_partno = '$item_partno',
prod_description = '$prod_description',
sup_itempartno = '$sup_itempartno',
currency = '$currency_selected',
slab_range= '$slab_range',
qty = '$item_quantity',
qty_sup = '$item_quantity_sup',
item_units = '$item_units',
item_units_sup = '$item_units_sup',
unitprice = '$unitprice',
total = '$total', 
total_inr='$total_inr'";

        mysql_query($query) or die ("Error in query: $query");
    ++$i;
    }

实际场景是。我的数据库中有一些记录。我在编辑模式下调用记录。在这种模式下,用户可以添加一些行(动态行)并且他们可以更新。因此,应更新存在的记录,并将新添加的行插入到表中。

最佳答案

INSERT ON DUPLICATE KEY UPDATE 查询将在更新之前插入,因此如果您的唯一键是自动递增的,它将首先插入一条新记录,然后查找更新,但因为它永远不会相同,它永远不会更新。

关于php - 关于重复键更新语法 php/mysql 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36150582/

有关php - 关于重复键更新语法 php/mysql 错误的更多相关文章

  1. ruby-on-rails - 如何验证 update_all 是否实际在 Rails 中更新 - 2

    给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru

  2. ruby - 树顶语法无限循环 - 2

    我脑子里浮现出一些关于一种新编程语言的想法,所以我想我会尝试实现它。一位friend建议我尝试使用Treetop(Rubygem)来创建一个解析器。Treetop的文档很少,我以前从未做过这种事情。我的解析器表现得好像有一个无限循环,但没有堆栈跟踪;事实证明很难追踪到。有人可以指出入门级解析/AST指南的方向吗?我真的需要一些列出规则、常见用法等的东西来使用像Treetop这样的工具。我的语法分析器在GitHub上,以防有人希望帮助我改进它。class{initialize=lambda(name){receiver.name=name}greet=lambda{IO.puts("He

  3. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  4. ruby-on-rails - 使用 rails 4 设计而不更新用户 - 2

    我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它​​不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数

  5. ruby-on-rails - 使用 Sublime Text 3 突出显示 HTML 背景语法中的 ERB? - 2

    所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择

  6. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  7. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  8. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  9. ruby-on-rails - 错误 : Error installing pg: ERROR: Failed to build gem native extension - 2

    我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby​​'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe

  10. ruby - #之间? Cooper 的 *Beginning Ruby* 中的错误或异常 - 2

    在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee

随机推荐