Page 1 of 1

Mysql insert code not working.

Posted: Sat Aug 20, 2011 4:15 pm
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.

Re: Mysql insert code not working.

Posted: Sat Aug 20, 2011 4:51 pm
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

Re: Mysql insert code not working.

Posted: Sat Aug 20, 2011 4:53 pm
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

Re: Mysql insert code not working.

Posted: Sun Aug 21, 2011 9:09 am
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());
?>

Re: Mysql insert code not working.

Posted: Sun Aug 21, 2011 10:19 am
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