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 8783177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:45:46+00:00 2026-06-13T20:45:46+00:00

Im trying to write a function that updates a mysql table if it excist,

  • 0

Im trying to write a function that updates a mysql table if it excist, or creates it if it does not.

My script still creates a new entry instead of updating.

Here is my sql structure:

CREATE TABLE IF NOT EXISTS `loan` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `bank_id` int(11) DEFAULT NULL,
  `name` longtext COLLATE utf8_unicode_ci,
  `rate` decimal(8,3) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `loan_0b0af02d` (`bank_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

Here is my php script:

$item_insert_sql = "INSERT INTO loan(id, bank_id, name, rate) VALUES ('" . $id . "', '" . $bank_id . "', '" . $name . "', '" . $rate . "'), primary key (id), ON DUPLICATE KEY UPDATE (name='$name', rate='$rate')";
$insert_item = mysql_query($item_insert_sql, $db); 

What do I need to do, to fix this?

  • 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-13T20:45:47+00:00Added an answer on June 13, 2026 at 8:45 pm

    Your insert statement is incorrect. You can’t define primary keys in an insert, and your on duplicate syntax is bad as well:

    INSERT INTO table (... fields ...) VALUES (... values ...)
    ON DUPLICATE KEY SET field1=value1, field2=value2, etc...
    

    If you had even minimal error handling on the query() call, you’d have gotten the error messages:

    $insert_item = mysql_query($item_insert_sql, $db) or die(mysql_error());
                                                     ^^^^^^^^^^^^^^^^^^^^^^
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a function that does something based on Emacs's current window
I'm trying to write a function that updates 2 tables in my database. I'm
Trying to write a small function that updates a paragraph with an image's alt
I am trying write a function that generates simulated data but if the simulated
I am trying to write a function that will allow me to browse for
I am trying to write a function that can take three parameters: an SqlDataReader,
I'm trying to write a function that will: If there is no region selected,
I am trying to write a function that takes two containers of the same
i am trying to write a function that will make DataRow[column] return nullable typed
I'm trying to write a function that will let me red-shift or blue-shift a

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.