Hi. I'm a new guy, and I'm having trouble with inserting into a database table. I'm designing a backend for a the website of a church camp that my wife is involved in. They have member churches, and I want her to be able to input data into the database without having to go through phpmyadmin. Here is my code:

<?php
require ("dbinfo.php");

$user = $_COOKIE["fname"];

$mysqli = mysqli_connect("$host", "$username", "$password", "$database");

$query = "SELECT * FROM member_churches WHERE church_name = '".$_POST['churchname']."' AND
minister_name = '".$_POST['minister']."'";
$res = mysqli_query($mysqli, $query);

//Test to make sure that the entry doesn't already exist
if (mysqli_num_rows($res) == 0) {

 $sql = "INSERT INTO member_churches 
	 (church_name, address_line1, address_line2, city, state, zip, 
mailing_address_line1, mailing_address_line2, mailing_city, mailing_state, mailing_zip, 
phone, fax, email, website, minister_name, minister_phone, minister_email, 
	minister_address_line1, minister_address_line2, minister_city, minister_state,
	minister_zip, camp_rep1, camp_rep1_phone, camp_rep1_email,
	 camp_rep1_address_line1, camp_rep1_address_line2, camp_rep1_city, 
	 camp_rep1_state, camp_rep1_zip, camp_rep2, camp_rep2_phone, camp_rep2_email, 
	 camp_rep2_address_line1, camp_rep2_address_line2, 
	 camp_rep2_city, camp_rep2_state, camp_rep2_zip) VALUES 
	 ('".mysqli_real_escape_string($mysqli, $POST['churchname'])."', 
	  '".mysqli_real_escape_string($mysqli, $_POST['churchaddress1'])."', 
	  '".mysqli_real_escape_string($mysqli, $_POST['churchaddress2'])."', 
	  '".mysqli_real_escape_string($mysqli, $_POST['churchcity'])."', 
	  '".mysqli_real_escape_string($mysqli, $_POST['churchstate'])."', 
	  '".mysqli_real_escape_string($mysqli, $_POST['churchzip'])."', 
	  '".mysqli_real_escape_string($mysqli, $_POST['mailingaddress1'])."', 
	  '".mysqli_real_escape_string($mysqli, $_POST['mailingaddress2'])."', 
	  '".mysqli_real_escape_string($mysqli, $_POST['mailingcity'])."', 
	  '".mysqli_real_escape_string($mysqli, $_POST['mailingstate'])."', 
	  '".mysqli_real_escape_string($mysqli, $_POST['mailingzip'])."', 
	  '".mysqli_real_escape_string($mysqli, $_POST['churchphone'])."', 
	  '".mysqli_real_escape_string($mysqli, $_POST['churchfax'])."', 
	  '".mysqli_real_escape_string($mysqli, $_POST['churchemail'])."', 
	  '".mysqli_real_escape_string($mysqli, $_POST['website'])."', 
	  '".mysqli_real_escape_string($mysqli, $_POST['minister'])."', 
	  '".mysqli_real_escape_string($mysqli, $_POST['ministerphone'])."', 
	  '".mysqli_real_escape_string($mysqli, $_POST['ministeremail'])."', 
	  '".mysqli_real_escape_string($mysqli, $_POST['ministeraddress1'])."', 
	  '".mysqli_real_escape_string($mysqli, $_POST['ministeraddress2'])."', 
	  '".mysqli_real_escape_string($mysqli, $_POST['ministercity'])."', 
	  '".mysqli_real_escape_string($mysqli, $_POST['ministerstate'])."', 
	  '".mysqli_real_escape_string($mysqli, $_POST['ministerzip'])."', 
	  '".mysqli_real_escape_string($mysqli, $_POST['camprep1'])."', 
	  '".mysqli_real_escape_string($mysqli, $_POST['camprep1phone'])."', 
	  '".mysqli_real_escape_string($mysqli, $_POST['camprep1email'])."', 
	  '".mysqli_real_escape_string($mysqli, $_POST['camprep1address1'])."',
	  '".mysqli_real_escape_string($mysqli, $_POST['camprep1address2'])."', 
	  '".mysqli_real_escape_string($mysqli, $_POST['camprep1city'])."', 
	  '".mysqli_real_escape_string($mysqli, $_POST['camprep1state'])."',
	  '".mysqli_real_escape_string($mysqli, $_POST['camprep1zip'])."', 
	  '".mysqli_real_escape_string($mysqli, $_POST['camprep2'])."', 
	  '".mysqli_real_escape_string($mysqli, $_POST['camprep2phone'])."',
	  '".mysqli_real_escape_string($mysqli, $_POST['camprep2email'])."', 
	  '".mysqli_real_escape_string($mysqli, $_POST['camprep2address1'])."', 
	  '".mysqli_real_escape_string($mysqli, $_POST['camprep2address2'])."', 
	  '".mysqli_real_escape_string($mysqli, $_POST['camprep2city'])."', 
	  '".mysqli_real_escape_string($mysqli, $_POST['camprep2state'])."', 
	  '".mysqli_real_escape_string($mysqli, $_POST['camprep2zip'])."')"
	  ;
	 
$result = mysqli_query($mysqli, $sql);
//If successful, use display block
if ($result === TRUE) {
	 $display_block = "<p>Thank you "
	 .$user.
	 ". The record for "
	 .$_POST['churchname'].
	 " was added to the database.</p>
	 <p>Would you like to:<ul>
	 <li>Add another new entry</li>
	 <li>Edit an entry</li>
	 <li>Return to the main menu</li></ul></p>";
} else {
printf("Could not insert record: %s\n", mysqli_error($mysqli));
}

} else {
//If the record already exists, tell me.	
echo "A record already exists for ".$_POST['churchname'].". Please check your data and try again.";
}

mysqli_close($mysqli);

?>
<html>
<head>
<title>Record Added</title>
</head>
<body>
<?php echo "$display_block";?>
</body>
<html>

If I enter new data, the display reads: "Thank you [user] The record for was added to the database" with [user] replaces by the username. In other words, the name of the church does not come up. When I go to the actual table to see if it updated, I see that an auto-increment occurred with my id primary key and a new row was added, but every field is blank. It's almost like the insert statement is working but it doesn't see anything to insert, so it just creates a blank row.

If it helps, here is the code for my form:

<?php
		if ($_COOKIE["auth"] != "1") 
		header("Location: loginform.html");

$fname = $_COOKIE["fname"];
		?>		
<html>
<head>
<title>New Entry Form</title>
</head>
<body>
<?php echo "Hello <strong>".$fname."</strong>! <br />";?>
<br />
<p>Enter your new database entry below.</p>
<br />
		<p><form action="dbentry.php" type="POST">
		<p><strong>Church Name: </strong><br />
		<input name="churchname" type="text" /></p>
		<p><strong>Address: </strong><br />
		<input name="churchaddress1" type="text" /></p>
		<p><strong>Address Line 2:</strong><br />
		<input name="churchaddress2" type="text" /></p>
		<p><strong>City/State/Zip </strong><br />
		<input name="churchcity" type="text" />
		<input name="churchstate" type="text" size="4" />
		<input name="churchzip" type="text" size="13" /></p>
		<p><strong>Phone: </strong><br />
		<input type="text" name="churchphone" size="18" /></p>
		<p><strong>Fax: </strong><br />
		<input type="text" name="churchfax" size="18" /></p>
		<p><strong>Email: </strong><br />
		<input type="text" name="churchemail" /></p>
		<p><strong>Website: </strong><br />
		<input type="text" name="website" /></p>
		<p><strong>Church Mailing Address: </strong><br />
		<input name="mailingaddress1" type="text" /></p>
		<p><strong>Church Mailing Address Line 2: </strong><br />
		<input name="mailingaddress2" type="text" /></p>
		<p><strong>City/State/Zip: </strong><br />
		<input name="mailingcity" type="text" />
		<input name="mailingstate" type="text" size="4" />
		<input name="mailingzip" type="text" size="13" /></p>
		<p><strong>Minister: </strong><br />
		<input name="minister" type="text" /></p>
		<p><strong>Minister Phone Number: </strong><br />
		<input name="ministerphone" type="text" size="18" /></p>
		<p><strong>Minister Email Address:</strong><br />
		<input name="ministeremail" type="text" /></p>
		<p><strong>Minister's Address: </strong><br />
		<input name="ministeraddress1" type="text" /></p>
		<p><strong>Monister's Address Line 2:</strong><br />
		<input name="ministeraddress2: type="text"</p>
		<p><strong>Minister's City/State/Zip: </strong><br />
		<input name="ministercity" type="text" />
		<input name="ministerstate" type="text" size="4" />
		<input name="ministerzip" type="text" size="13" /></p>
		<p><strong>Camp Representative 1: </strong><br />
		<input name="camprep1" type="text" /></p>
		<p><strong>Camp Representative 1-Phone Number: </strong><br />
		<input name="camprep1phone" type="text" size="18" /></p>
		<p><strong>Camp Representative 1-Email: </strong><br />
		<input name="camprep1email" type="text" /></p>
		<p><strong>Camp Representative 1- Address: </strong><br />
		<input name="camprep1address1" type="text" /></p>
		<p><strong>Camp Representative 1- Address Line 2:</strong><br />
		<input name="camprep1address2: type="text"</p>
		<p><strong>Camp Representative 1- City/State/Zip: </strong><br />
		<input name="camprep1city" type="text" />
		<input name="camprep1state" type="text" size="4" />
		<input name="camprep1zip" type="text" size="13" /></p>
		<p><strong>Camp Representative 2: </strong><br />
		<input name="camprep2" type="text" /></p>
		<p><strong>Camp Representative 2-Phone Number: </strong><br />
		<input name="camprep2phone" type="text" size="18" /></p>
		<p><strong>Camp Representative 2-Email: </strong><br />
		<input name="camprep2email" type="text" /></p>
		<p><strong>Camp Representative 2- Address: </strong><br />
		<input name="camprep2address1" type="text" /></p>
		<p><strong>Camp Representative 2- Address Line 2:</strong><br />
		<input name="camprep2address2: type="text"</p>
		<p><strong>Camp Representative 2- City/State/Zip: </strong><br />
		<input name="camprep2city" type="text" />
		<input name="camprep2state" type="text" size="4" />
		<input name="camprep2zip" type="text" size="13" /></p>
		<p><input type="submit" name="submit" value="Add Entry" /><p/>
</form>
</body>
</html>

I'm very new, and I'm sure I've just overlooked something. Can anyone please help?

Danny McCaslin

Recommended Answers

All 3 Replies

For starters, you have an error on line 25. You used $POST instead of $_POST. That probably explains the insert not working correctly but maybe not everything. Suggest that you fix that, run it again and see what errors are left.

Thanks for the catch, but there's no change. It's still just inserting a blank row.

For starters, you have an error on line 25. You used $POST instead of $_POST. That probably explains the insert not working correctly but maybe not everything. Suggest that you fix that, run it again and see what errors are left.

Okay, I poked around a bit and found that a couple of my <input> tags in the form document weren't closed. I also added an error message to the php and ran it all again, and it worked like a charm. Thanks for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.