{"id":2064,"date":"2019-06-23T12:05:34","date_gmt":"2019-06-23T11:05:34","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=2064"},"modified":"2022-02-17T07:13:49","modified_gmt":"2022-02-17T07:13:49","slug":"html-forms-update-database","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/mysql\/examples-mysql\/html-forms-update-database","title":{"rendered":"HTML Forms Update  Database"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">INSERT from and HTML form<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!-------------------------------->\n&lt;!-------------------------------->\n&lt;!----- ADD TO DATABASE FORM ----->\n&lt;!-------------------------------->\n&lt;!-------------------------------->\n&lt;h2>Add To Database&lt;\/h2>\n&lt;?php\nif ( (isset($_POST&#91;'action'])) &amp;&amp; ($_POST&#91;'action'] == \"add_to_database\") )\n{\n\t$field1 = mysqli_real_escape_string($dblink, $_POST&#91;'field1']);\n\t$field2 = mysqli_real_escape_string($dblink, $_POST&#91;'field2']);\n\t\n\t\/\/$field1 = trim($field1);\n\t\/\/$field1 = ucwords($field1);\n\t\n\t$field_checkbox = 0;\n\tif(isset($_POST&#91;'field_checkbox']) &amp;&amp; $_POST&#91;'field_checkbox'] == 'Yes')\n\t\t$field_checkbox = 1;\n\t\n\n\t$query1 = mysqli_query(\"SELECT * FROM my_table WHERE field1 = '$field1'\");\n\tif (mysqli_num_rows($query1))\n\t{\n\t\techo \"VALUE ALREADY EXISTS!&lt;br \/>\";\n\t}\n\telse if ( ($field1 == \"\") || ($field2 == \"\") )\n\t{\n\t\techo \"INVALID FORM CONTENTS!&lt;br \/>\";\n\t}\n\telse\n\t{\n\t\t$result = mysqli_query(\"INSERT INTO my_table (\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tfield1,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tfield2,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tfield_checkbox\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t) VALUES (\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t'$field1',\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t'$field2',\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t$field_checkbox\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t)\");\n\n\t\n    \/\/Reload page\n    unset($_POST);\n    header('Location: '.$_SERVER&#91;'PHP_SELF']);\n    die;\n  }\n}\n?>\n&lt;form action=\"myfile.php\" method=\"POST\">\n  &lt;input type=\"hidden\" name=\"action\" value=\"add_to_database\" \/>\n  &lt;input type=\"hidden\" name=\"some_id\" value=\"&lt;?php echo \"$some_id\";?>\" \/>\n\n  &lt;label>Field 1:&lt;\/label>&lt;br \/>\n  &lt;input type=\"text\" name=\"field1\" value=\"&lt;?php echo \"$field1\";?>\" size=\"80\"  \/>&lt;br \/>\n\n  &lt;label>Field 2:&lt;\/label>&lt;br \/>\n  &lt;input type=\"text\" name=\"field2\" value=\"&lt;?php echo \"$field2\";?>\" size=\"80\"  \/>&lt;br \/>\n  \n  &lt;label>FieldCheckbox:&lt;\/label>&lt;br \/>\n  &lt;input type=\"checkbox\" name=\"field_checkbox\" value=\"Yes\" \/>&lt;br \/>\n\n  &lt;input type=\"submit\" value=\"Store\" \/>\n&lt;\/form><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>INSERT from and HTML form<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[148],"tags":[],"class_list":["post-2064","post","type-post","status-publish","format-standard","hentry","category-examples-mysql"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2064","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=2064"}],"version-history":[{"count":7,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2064\/revisions"}],"predecessor-version":[{"id":2927,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2064\/revisions\/2927"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=2064"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=2064"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=2064"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}