PHP Insert data into mysql on duplicate key update - add suffix -
want insert duplicate entry mysql suffix. here example:
+-------+--------------+-------+ | id | title | url | +-------+--------------+-------+ +-------+--------------+-------+ | 1 | test | test1 | +-------+--------------+-------+ | 2 | test | test2 | +-------+--------------+-------+ | 3 | test | test3 | +-------+--------------+-------+
means, want add auto number suffix end of each url have duplicate title. titles duplicated url kinda unique.
here query:
$i = 0; $i++; $sql = "insert `content` (title, text, cat, url, tag) values ('".$_post["title"]."', '".$_post["text"]."', '".$_post["category"]."', '".$_post["url"]."', '".$_post["tag"]."') on duplicate key update url=".$_post["url"]."+". $i ."";
note: url made dynamically based on title entry. query not working, wrong code?
update: here error after run query:
error: insert
content
(title, text, cat, url, tag) values ('test', '', '16', 'test', '') on duplicate key update url=test+1 unknown column 'test' in 'field list'
your code has strange things. $i = 0; followed $i++; equivalent doing $i = 1; again, what's purpose on setting variable use once?
what need is:
- insert query forgetting "url" field , removing on duplicate...
get id of last insert. have @ following link see how value: http://www.w3schools.com/php/php_mysql_insert_lastid.asp
update query set value of "url"
$_post["url"].$id_of_last_query
Comments
Post a Comment