INSERT from and HTML form

<!----- ADD TO DATABASE FORM ----->
<h2>Add To Database</h2>
if ( (isset($_POST['action'])) && ($_POST['action'] == "add_to_database") )
	$field1 = mysqli_real_escape_string($dblink, $_POST['field1']);
	$field2 = mysqli_real_escape_string($dblink, $_POST['field2']);
	//$field1 = trim($field1);
	//$field1 = ucwords($field1);
	$field_checkbox = 0;
	if(isset($_POST['field_checkbox']) && $_POST['field_checkbox'] == 'Yes')
		$field_checkbox = 1;

	$query1 = mysqli_query("SELECT * FROM my_table WHERE field1 = '$field1'");
	if (mysqli_num_rows($query1))
		echo "VALUE ALREADY EXISTS!<br />";
	else if ( ($field1 == "") || ($field2 == "") )
		echo "INVALID FORM CONTENTS!<br />";
		$result = mysqli_query("INSERT INTO my_table (
														) VALUES (

    //Reload page
    header('Location: '.$_SERVER['PHP_SELF']);
<form action="myfile.php" method="POST">
  <input type="hidden" name="action" value="add_to_database" />
  <input type="hidden" name="some_id" value="<?php echo "$some_id";?>" />

  <label>Field 1:</label><br />
  <input type="text" name="field1" value="<?php echo "$field1";?>" size="80"  /><br />

  <label>Field 2:</label><br />
  <input type="text" name="field2" value="<?php echo "$field2";?>" size="80"  /><br />
  <label>FieldCheckbox:</label><br />
  <input type="checkbox" name="field_checkbox" value="Yes" /><br />

  <input type="submit" value="Store" />
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.


Your email address will not be published.