[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.3

PHP MySQL  Form Validation

Asked by karl_0090 in PHP and Databases, PHP Scripting Language

Tags: PHP, MySQL

Hello,

I have a form that inserts data into a MySQL database using PHP and a HTML form, I have applied some basic validation but would like to add another type.

I would like to check a column of data and if there is a duplicate then I would like it to error out.

I have attempted to apply this but I know am doing it wrong somewhere but was worth a go.

Lines 10  33 are the code that I have been trying out.

Many Thanks for any help.

Karl
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
<?php
	
	if (isset($_POST['form_sub']))
		{
	$first1 = $_POST["first"];
	$last1 = $_POST["last"];
	$company1 = $_POST["company"];
 
		require ($_SERVER['DOCUMENT_ROOT']."cmstesting/config/db_config.php");
		$connection = @mysql_connect ($db_host, $db_user, $db_pass) or die ("Error with Connection");
		mysql_select_db ($db_name, $connection);
		
		$query1 = "SELECT company FROM contacts";
		$result1 = mysql_query ($query1, $connection);
		
		if ($company1 == $result1)
			{
			echo 'Error: Duplicate Company';
			}
			else
			{
			$title1 = $_POST["title"];
			$suffix1 = $_POST["suffix"];
			$address11 = $_POST["address1"];
			$address21 = $_POST["address2"];
			$city1 = $_POST["city"];
			$town1 = $_POST["town"];
			$country1 = $_POST["country"];
			$postcode1 = $_POST["postcode"];
			$website1 = $_POST["website"];
			$email1 = $_POST["email"];
			}
			
		$hp11 = $_POST["hp1"];
		$hp21 = $_POST["hp2"];
		$hp31 = $_POST["hp3"];
		
		$len3 = strlen($hp11);
		$len4 = strlen($hp21);
		$len5 = strlen($hp31);
		
	if (($len3 > 0) && ($len4 > 0) && ($len5 > 0))
			{
		$mp11 = $_POST["mp1"];
		$mp21 = $_POST["mp2"];
		$mp31 = $_POST["mp3"];
		$notes1 = $_POST["notes"];		
			}
			else
			{
			echo 'Error: Enter Phone Number';
			}
	
		if (($len3 > 0) && ($len4 > 0) && ($len5 > 0))
			{
		require ($_SERVER['DOCUMENT_ROOT']."cmstesting/config/db_config.php");
		$connection = @mysql_connect ($db_host, $db_user, $db_pass) or die ("Error with Connection");
		mysql_select_db ($db_name, $connection);
	
		$query = "INSERT INTO contacts (conid, title, first, last, suffix, company, address1, address2, city, town, country, postcode, website, email, hp1, hp2, hp3, mp1, mp2, mp3, notes) VALUES (NULL, '$title1', '$first1', '$last1', '$suffix1', '$company1', '$address11', '$address21', '$city1', '$town1', '$country1', '$postcode1', '$website1', '$email1', '$hp11', '$hp21', '$hp31', '$mp11', '$mp21', '$mp31', '$notes1')";
	
		mysql_query($query, $connection) or die("MySQL Query Error");
		header ('Location: contact_view.php');
		exit;
			}
	}
 
?>
 
 
<title>New Contact</title>
 
<!-- Meta Tags -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 
<!-- CSS -->
<link rel="stylesheet" href="css/structure.css" type="text/css" />
<link rel="stylesheet" href="css/form.css" type="text/css" />
<!-- <link rel="stylesheet" href="css/theme.css" type="text/css" /> -->
 
</head>
 
<body id="public">
	
<img id="top" src="images/top.png" alt="" />
<div id="container">
 
 
 
<form id="contact" name="contact" class="" autocomplete="off"
	enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
 
<div class="info">
	<h2>New Contact</h2>
	<div></div>
</div>
 
<ul>
		
	
<li id="foli0" 		class="   ">
	<label class="desc" id="title0" for="Field0">
		Name			</label>
	<span>
		<input id="title" 	name="title" 	type="text" 	class="field text" 	value="" 	size="2" 	tabindex="1" 	/>
		<label for="Field0">Title</label>
	</span>
	<span>
	<input id="first" 	name="first" 	type="text" 	class="field text" 	value="" 	size="8" 	tabindex="2" 	/>
	<label for="Field1">First</label>
	</span>
	<span>
		<input id="last" 	name="last" 	type="text" 	class="field text" 	value="" 	size="12" 	tabindex="3" 	/>
		<label for="Field2">Last</label>
	</span>
	<span>
		<input id="suffix" 	name="suffix" 	type="text" 	class="field text" 	value="" 	size="3" 	tabindex="4" 	/>
		<label for="Field3">Suffix</label>
	</span>	</li>
 
 
<li id="foli" 		class="    ">
  <label class="desc" id="title02" for="label">Comany </label>
  <div>
    <input id="company" 	name="company" 	type="text" 	class="field text large" 	value="" 	maxlength="255" 	tabindex="5" />
  </div>
</li>
<li id="foli4" 		class="   ">
	<label class="desc" id="title4" for="Field4">
		Address			</label>
	<div class="column">
		<span class="full">
		<input id="address1" 	name="address1" 	type="text" 	class="field text addr" 	value="" 	tabindex="6" 	/>
		<label for="Field4">Street Address</label>
		</span>
		<span class="full">
		<input id="address2" 	name="address2" 	type="text" 	class="field text addr" 	value="" 	tabindex="7" 	/>
		<label for="Field5">Address Line 2</label>
		</span>
		<span class="left">
		<input id="city" 	name="city" 	type="text" 	class="field text addr" 	value="" 	tabindex="8" 	/>
		<label for="Field6">City</label>
		</span>
		<span class="right">
		<input id="town" 	name="town" 	type="text" 	class="field text addr" 	value="" 	tabindex="9" 	/>
		<label for="Field7">State / Province / Region</label>
		</span>
		<span class="left">
		<input id="postcode" 	name="postcode" type="text" class="field text addr" value="" maxlength="15" tabindex="10" 	/>
		<label for="Field8">Postal / Zip Code</label>
		</span>
		<span class="right">
		<select id="country" 		name="country" 		class="field select addr" 		tabindex="11" 		>
			<option value="" selected="selected"></option>
<option value="United Kingdom">United Kingdom</option>
<option value="Australia">Australia</option>
<option value="Austria">Austria</option>
<option value="Belgium">Belgium</option>
<option value="Belize">Belize</option>
<option value="China">China</option>
<option value="Canada">Canada</option>
<option value="Colombia">Colombia</option>
<option value="Finland">Finland</option>
<option value="France">France</option>
<option value="Germany">Germany</option>
<option value="Ghana">Ghana</option>
<option value="Greece">Greece</option>
<option value="Grenada">Grenada</option>
<option value="Guatemala">Guatemala</option>
<option value="Guinea">Guinea</option>
<option value="Guinea-Bissau">Guinea-Bissau</option>
<option value="Guyana">Guyana</option>
<option value="Haiti">Haiti</option>
<option value="Honduras">Honduras</option>
<option value="Hong Kong">Hong Kong</option>
<option value="Ireland">Ireland</option>
<option value="Israel">Israel</option>
<option value="Italy">Italy</option>
<option value="New Zealand">New Zealand</option>
<option value="Norway">Norway</option>
<option value="Poland">Poland</option>
<option value="Russia">Russia</option>
<option value="Saint Lucia">Saint Lucia</option>
<option value="Spain">Spain</option>
<option value="Turkey">Turkey</option>
<option value="United States">United States</option>
 
		</select>
		<label for="country">Country</label>
		</span>	</div>
	</li>
 
 
<li id="foli30" 		class="    ">
	<label class="desc" id="title30" for="Field30">
		Web Site			</label>
	<div>
	  <input id="website" 	name="website" 	type="text" 	class="field text large" 	value="" 	maxlength="255" tabindex="12"/>
	</div>
	</li>
 
 
<li id="foli12" 		class="   ">
	<label class="desc" id="title12" for="Field12">
		Email Address			</label>
	<div>
		<input id="email" 	name="email" 	type="text" 	class="field text medium" 	value="" 	maxlength="255" 	tabindex="13"/> 
	</div>
	</li>
 
 
<li id="foli11" 		class="   ">
	<label class="desc" id="title11" for="Field11">
		Home Phone			</label>
	<span>
		<input id="hp1" 	name="hp1" 	type="text" 	class="field text" 	value="" 	size="3" maxlength="4" 	tabindex="14" 			/> - 
		<label for="Field11">(####)</label>
	</span>
	<span>
		<input id="hp2" 	name="hp2" 	type="text" 	class="field text" 	value="" 	size="3" 	maxlength="3" 	tabindex="15" 			/> - 
		<label for="Field11-1">###</label>
	</span>
	<span>
	 	<input id="hp3" 	name="hp3" 	type="text" 	class="field text" 	value="" 	size="4" 	maxlength="4" 	tabindex="16" 			/>
		<label for="Field11-2">####</label>
	</span>	</li>
 
 
<li id="foli10" 		class="   ">
	<label class="desc" id="title10" for="Field10">
		Mobile Phone			</label>
	<span>
		<input id="mp1" 	name="mp1" 	type="text" 	class="field text" 	value="" 	size="3" 	maxlength="3" tabindex="17" 			/> - 
		<label for="Field10">(###)</label>
	</span>
	<span>
		<input id="mp2" 	name="mp2" 	type="text" 	class="field text" 	value="" 	size="3" 	maxlength="3" 	tabindex="18" 			/> - 
		<label for="Field10-1">###</label>
	</span>
	<span>
	 	<input id="mp3" 	name="mp3" 	type="text" 	class="field text" 		value="" 	size="4" 	maxlength="5" 	tabindex="19" 			/>
		<label for="Field10-2">#####</label>
	</span>	</li>
 
 
<li id="foli21" 		class="   ">
	<label class="desc" id="title21" for="Field21">
		Birthday			</label>
	<span>
		<input id="Field21-1" 			name="Field21-1" 			type="text" 			class="field text" 			value="" 			size="2" 			maxlength="2" 			tabindex="20" 			/> /
		<label for="Field21-1">DD</label>
	</span>
	<span>
		<input id="Field21-2" 			name="Field21-2" 			type="text" 			class="field text" 			value="" 			size="2" 			maxlength="2" 			tabindex="21" 			/> /
		<label for="Field21-2">MM</label>
	</span>
	<span>
	 	<input id="Field21" 			name="Field21" 			type="text" 			class="field text" 			value="" 			size="4" 			maxlength="4" 			tabindex="22" 			/>
		<label for="Field21">YYYY</label>
	</span>
<li id="foli22" 
		class="   ">
	
		
	<label class="desc" id="title22" for="Field22">
		Notes			</label>
	
	<div>
				<textarea id="notes" 
			name="notes" 
			class="field textarea medium" 
			rows="10" cols="50"
			tabindex="23"
						 ></textarea>
			</div>
	</li>
 
	<li class="buttons">
				<input name="form_sub" type="submit" class="btTxt" id="saveForm" value="Submit" />
	  </li>
 
	<li style="display:none">
		<label for="comment">Do Not Fill This Out</label>
		<textarea name="comment" id="comment" rows="1" cols="1"></textarea>
	</li>
</ul>
</form>
 
</div><!--container-->
<img id="bottom" src="images/bottom.png" alt="" />
 
    
</body>
</html>
 
Related Solutions
 
Loading Advertisement...
 
[+][-]03/04/09 11:51 AM, ID: 23798624Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: PHP and Databases, PHP Scripting Language
Tags: PHP, MySQL
Sign Up Now!
Solution Provided By: Michael701
Participating Experts: 1
Solution Grade: A
 
 
Loading Advertisement...
20091118-EE-VQP-93 - Hierarchy / EE_QW_3_20080625