Mysql insert code not working.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
drayarms
Forum Contributor
Posts: 134
Joined: Fri Dec 31, 2010 5:11 pm

Mysql insert code not working.

Post by drayarms »

Hello everybody, I can't seem to figure out why this insert code isn't working. I'm trying to create a database of US zip codes. I created this user interface (form) with nothing but a submit button to execute the insert query

Code: Select all


				<div id="right_content" class="">    


					<h3> Insert Zips </h3>

					
					<form action = "insertzip1.php" method = "post">
					
						<input type = "submit" name = "submit" value = "submit"/>


					</form>

					
				</div> <!--closes right content-->

Well here is the insert query which is supposed to accomplish the task. I have just included a tiny subsets of all the zipcodes (the insertzip1.php page which is the value of the action attribute of the form).

Code: Select all

<php?



if (isset($_POST['submit'])) {


require ('config.php');




$query = "INSERT INTO zips (zip, lat, lon, city, state, county, z_type, xaxis, yaxis, zaxis, z_primary, worldregion, country, locationtext, location, population, housingunits, income, landarea, waterarea, decommisioned, militaryrestrictioncodes, decommisionedplace) VALUES


('00501', 40.81, -73.04, 'HOLTSVILLE', 'NY', 'SUFFOLK', 'UNIQUE', 0.22, -0.72, 0.65, 'Yes', 'NA', 'US', 'Holtsville, NY', 'NA-US-NY-HOLTSVILLE', '', 0, 0, '', '', 'No', '', ''),
('00501', 40.81, -73.04, 'I R S SERVICE CENTER', 'NY', 'SUFFOLK', 'UNIQUE', 0.22, -0.72, 0.65, 'No', 'NA', 'US', 'I R S Service Center, NY', 'NA-US-NY-I R S SERVICE CENTER', '', 0, 0, '', '', 'No', '', ''),
('00544', 40.81, -73.04, 'HOLTSVILLE', 'NY', 'SUFFOLK', 'UNIQUE', 0.22, -0.72, 0.65, 'Yes', 'NA', 'US', 'Holtsville, NY', 'NA-US-NY-HOLTSVILLE', '', 0, 0, '', '', 'No', '', ''),
('00544', 40.81, -73.04, 'IRS SERVICE CENTER', 'NY', 'SUFFOLK', 'UNIQUE', 0.22, -0.72, 0.65, 'No', 'NA', 'US', 'Irs Service Center, NY', 'NA-US-NY-IRS SERVICE CENTER', '', 0, 0, '', '', 'No', '', '')
";

$result = mysql_query($query);

header("Location: insertzipsuccess.php");

}else{ die ("Could not insert data because" . mysql_error());}

?>
The insertzipsuccess.php page is simply a page that prints out a success message if the query is successfully executed. Well when I hit the submit button, I just get redirected to a blank insertzip1.php page
Can anyone show me what I'm not doing right here?

PS I already created the table with fields that correspond to all the fields I'm trying insert.
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Mysql insert code not working.

Post by phphelpme »

My first advice to you would be to double check your single quote marks in your script because it would appear you have more than one missing. :)

Second would be check your open php statement although I think that was a typing error just for the purpose of this post entry.

I would also check that the amount of values you stipulate in your first sql query string have the same amount of values in the next string statement such as you declare 23 fields/columns yet you state 24 values.

Best wishes
Last edited by phphelpme on Sat Aug 20, 2011 4:55 pm, edited 3 times in total.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Mysql insert code not working.

Post by twinedev »

You open the PHP code with <php? instead of <?php, therefore, PHP code isn't actually executed (and if you did a VIEW SOURCE, you would probably see it all)

-Greg
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Mysql insert code not working.

Post by social_experiment »

You can make your troubleshooting a bit easier if you allow the script to die gracefully. Change your $result statement to the one below and it should show the problem (or any problem) if it's related to the sql query. Once your script is working, simply remove the mysql_error() function and add some quirky message about why the query failed (if it fails).

Code: Select all

<?php
 $result = mysql_query($query);
 // try this in future
 $result = mysql_query($query) or die(mysql_error());
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Mysql insert code not working.

Post by phphelpme »

Good advice Social. Should have put that one myself.. lol I just saw right away that there were things missing and too many things compared to others... lol

Best wishes
Post Reply