{"id":434,"date":"2012-07-17T16:50:46","date_gmt":"2012-07-17T16:50:46","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=434"},"modified":"2022-02-17T07:14:04","modified_gmt":"2022-02-17T07:14:04","slug":"insert","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/mysql\/queries\/insert-queries\/insert","title":{"rendered":".INSERT General"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Object Oriented Style<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>  $stmt = $maindb->prepare(\"\n    INSERT INTO my_table\n    (\n      MyColumn1,\n      MyColumn2\n    ) VALUES (\n      ?,\n      ?\n    )\");\n  $stmt->bind_param(\"si\", $MyColumn1, $MyColumn2);\n  $MyColumn1 = \"abc\";\n  $MyColumn2 = 12;\n  $stmt->execute();\n  $stmt->close();<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Get Auto ID Of New Record<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>  $result = $maindb->query(\"INSERT INTO some_table (list_name, user_id) VALUES ('john', 1000)\");\n  if ($result == TRUE)\n  {\n    $my_variable = $maindb->insert_id;\t\t\/\/Get the auto increment ID value that was assigned\n    \n  }<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Procedural&nbsp;Style<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\t$result = mysqli_query($dblink, \"INSERT INTO my_table (\n\t\t\tsome_id,\n\t\t\temail_address\n\t\t) VALUES (\n\t\t\t$some_id,\n\t\t\t'$email'\n\t\t)\");\n\tif ($result != TRUE)\n\t\tdie(\"ERROR - Can't add to database\");<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Get Auto ID Of New Record<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\t$result = mysqli_query($dblink, \"INSERT INTO some_table (list_name, user_id) VALUES ('$list_name', $user_id)\");\n\tif ($result == TRUE)\n\t{\n\t\t$my_variable = mysqli_insert_id($dblink);\t\t\/\/Get the auto increment ID value that was assigned\n\t\t\n\t}<\/code><\/pre>\n\n\n\n<p>The mysqli_insert_id() function returns the ID generated by a query on a table with a column having the AUTO_INCREMENT attribute. If the last query wasn&#8217;t an INSERT or UPDATE statement or if the modified table does not have a column with the AUTO_INCREMENT attribute, this function will return zero.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Object Oriented Style Get Auto ID Of New Record Procedural&nbsp;Style Get Auto ID Of New Record The mysqli_insert_id() function returns the ID generated by a query on a table with a column having the AUTO_INCREMENT attribute. If the last query wasn&#8217;t an INSERT or UPDATE statement or if the modified table does not have a [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[262],"tags":[],"class_list":["post-434","post","type-post","status-publish","format-standard","hentry","category-insert-queries"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/434","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=434"}],"version-history":[{"count":17,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/434\/revisions"}],"predecessor-version":[{"id":5294,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/434\/revisions\/5294"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=434"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=434"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}