Saturday, 15 June 2013

PHP Insert data into mysql on duplicate key update - add suffix -


Enter duplicate entry in mysql with a suffix. Here's an example:

  + ------- + -------------- + ------- + | ID | Title | Url | + ------- + -------------- + ------- + + ------- + --------- ----- + ------- + | 1 | Test | Test1 | + ------- + -------------- + ------- + | 2 | Test | Test2 | + ------- + -------------- + ------- + | 3 | Test | Test3 | + ------- + -------------- + ------- +  

I mean, I add auto The number of headings is replicated as a suffix for the end of each URL, which is a duplicate title, but URLs are slightly unique.

Here's the query:

  $ i = 0; $ I ++; Enter the $ Sql ​​= "` content` (title, text, cat, url, tag) values ​​(''. $ _ POST ["title"]. '', '". $ _ POST [" text " ]. '', ''. $ _ POST ["category"]. '' $ _ POST ["url"]. '', ''. $ _ POST ["tag"]. '') Duplicate key update Url = ". $ _POST ["uRL"]. "+". $ I. " 

Note: The URL is dynamically based on the title entry. This query is not working, what is wrong with my code?

< Strong> Update: There is an error after query:

Error: INSERT content (title, text, cat, url, tag) value 'Field Listing' in the list of 'Exam', '', '16', 'Examination', '') Duplicate Key Update URL = Examination + 1 Unknown column ''

P>

There are some weird things in your code $ I = 0; After $ I ++; $ i = 1 tax is equal to;

  1. INSERT query forgetting about "URL", again, field and current duplicate Delete
  2. Get last insert ID.

  3. Update query to set the value of "url"

      $ _POST ["url"]. $ Id_of_last_query  

No comments:

Post a Comment