Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8397287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:44:56+00:00 2026-06-09T20:44:56+00:00

Trying to make insert or update sql using the prepared statements from php’s pdo.

  • 0

Trying to make insert or update sql using the prepared statements from php’s pdo. First I thought of using REPLACE INTO command,but it gives me an error because I have a foreign key on my index. Read that I must use INSERT...ON DUPLICATE KEY UPDATE syntax to make it working, but it’s not clear for me how to do that with prepared statements. Any solution for this? Thanks.

The sql is :

$sql="REPLACE INTO fn_currencies(id,short,name,buy,sell,date) VALUES (:id,:short,:name,:buy,:sell,:update)";

UPD: I am making this query in Yii that uses personal wrapper over the PDO.
When I use unnamed parameters I get this type of error:

CDbCommand failed to execute the SQL statement: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens. The SQL statement executed was: INSERT INTO `fn_currencies` (id,short,name,buy,sell,date) VALUES (?,?,?,?,?,?) ON DUPLICATE KEY UPDATE id=?,short=?,name=?,buy=?,sell=?,date=? 

When I use the named parameters with differed names for Insert and Update as was mentioned..I get no errors and neither data is inserted in my DB.
Here is the schema for the DB:

CREATE TABLE IF NOT EXISTS `fn_currencies` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `short` varchar(4) NOT NULL,
  `name` varchar(200) NOT NULL,
  `buy` decimal(10,4) NOT NULL,
  `sell` decimal(10,4) NOT NULL,
  `date` date NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

--
ALTER TABLE `fn_currencies`
  ADD CONSTRAINT `FK_fn_currencies` FOREIGN KEY (`id`) REFERENCES `fn_operations` (`currency_id`);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:44:58+00:00Added an answer on June 9, 2026 at 8:44 pm

    Thanks to DavaRandom, he pointed out an error in my code, but this should do the trick. Replace the named parameter with ? and use an array merge to make the SQL on the fly like this:

    $sql="
        insert INTO fn_currencies(id,short,name,buy,sell,date) 
        VALUES (?,?,?,?,?,?)
        on duplicate key update currencies set 
            short=?, name=?, buy=?, sell=?, update=?";
    $values=array("id"=>1, "short"=>36, "name"=>'Bazinga', "sell"=>3.67, "date"=>'2012-08-08');
    $db->query($sql, array_merge(array_values($values),array_values($values)));
    

    Apparently this will also work (See comments all over the page about yes/no/maybe) but the above will certainly work:

    $sql="
        insert INTO fn_currencies(id,short,name,buy,sell,date) 
        VALUES (:id,:short,:name,:buy,:sell,:update)
        on duplicate key update currencies set 
            short=:short, name=:name, buy=:buy, sell=:Sell, update=:update";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a query that will insert multiple values from my
I'm using folding-mode in emacs and was trying to make a function to insert
i'm trying to make a TRIGGER in ORACLE using ORACLE SQL DEVELOPER, I would
I am trying to resolve t-sql exercise I need to update first table with
I'm having problems trying to make a reactive form. When I insert data into
Trying to make the infamous checkall checkbox for dynamically created rows from a MySQL
I'm trying to make simple many-to-one association, using NHibernate.. I have class Recruit with
Hey, I am new to Oracle SQL and I am trying to make a
Someone here asked: Linq-To-Sql enables calling SPs. If this SP executes an update/delete/insert, do
I am trying to restore a DB using an SQL script, but things foreign

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.