[SOLVED]Errors inserting data from php to mysql

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
mcccy005
Forum Contributor
Posts: 123
Joined: Sun May 28, 2006 7:08 pm

[SOLVED]Errors inserting data from php to mysql

Post by mcccy005 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I get the following errors when trying to insert data into the mysql database:

Notice: Undefined index: null in X:\input_set.php on line 87
Notice: Undefined index: null in X:\input_set.php on line 87
Notice: Undefined index: null in X:\input_set.php on line 87
Notice: Undefined index: null in X:\input_set.php on line 87
Cannot execute query ......(except that my code doesn't output the mysql error even though I have the following code: or die("Cannot execute query ".mysql_error( )) )


The structure of the table I am trying to put data in is as follows:
[syntax="sql"]create table marinas
( marina_id tinyint unsigned not null auto_increment primary key,
  is_active bool not null default 1,
  marina_name char(30) not null,  
  abn int unsigned,
  region_id tinyint unsigned not null,
  index(region_id),
  foreign key(region_id) references regions(region_id),
  contact_first_name varchar(50),
  contact_last_name varchar(50),
  street_address varchar(70),
  suburb varchar(25),
  state char(3) not null,
  index(state),
  foreign key(state) references states(state),
  postcode smallint unsigned,
  postal_address varchar(70),
  postal_state char(3),
  index(postal_state),
  foreign key(postal_state) references states(state),
  postal_postcode smallint unsigned,
  telephone_work int unsigned,
  telephone_fax int unsigned,
  telephone_mobile int unsigned,
  telephone_emergency int unsigned,
  email varchar(80),
  secure_parking bool not null
) type=innodb;
and the actual query I am using is as follows:

Code: Select all

INSERT into marinas (marina_name, abn, region_id, contact_first_name, contact_last_name, street_address, suburb, state, postcode, postal_address, postal_state, postal_postcode, telephone_work, telephone_fax, telephone_mobile, telephone_emergency, email, secure_parking) VALUES ('Kia Marina', '53004085616', '2', 'Geoff', 'Malcovich', '198B Joness St', 'Mannum', 'SA', '5079', 'abc', 'SA', '5089', '088349882', '0212345678', '0712345678', '041898627', 'bookings@kiamarina.com.au', '1')

feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Last edited by mcccy005 on Wed May 16, 2007 5:23 am, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

please post the lines 80 to 90 of input_set.php
mcccy005
Forum Contributor
Posts: 123
Joined: Sun May 28, 2006 7:08 pm

Post by mcccy005 »

Alright, I found one little bug...turns out that I was also trying to get data from areas of $_POST which don't exist (the way I designed my code means that the title of a section of a form is also a type of field and I was trying to get data from this).

Anyways, I still get the following error only (and it still doesnt print the actual error after "Cannot execute query" as it should when I use mysql_error( ) ):

Cannot execute query
INSERT into marinas (marina_name, abn, region_id, contact_first_name, contact_last_name, street_address, suburb, state, postcode, postal_address, postal_state, postal_postcode, telephone_work, telephone_fax, telephone_mobile, telephone_emergency, email, secure_parking) VALUES ('Kia Marina', '53004085616', '2', 'Geoff', 'Malcovich', '198B Joness St', 'Mannum', 'SA', '5079', 'abc', 'SA', '5089', '088349882', '0212345678', '0712345678', '041898627', 'bookings@kiamarina.com.au', '1')

I'm assuming I need a ' on each side of numerical values when I'm inserting into mysql also?? As in '1' even though its numerical (or boolean actually)??
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

mcccy005 wrote:I'm assuming I need a ' on each side of numerical values when I'm inserting into mysql also?? As in '1' even though its numerical (or boolean actually)??
No, that's not necessary.
What's the code again?
mcccy005
Forum Contributor
Posts: 123
Joined: Sun May 28, 2006 7:08 pm

Post by mcccy005 »

Lines 77 - 94 of input_set are as follows:

Code: Select all

//If the next or submit button was pressed, validate current page and add data to the database
if($this->input_pages_array[$this->current_page-1]->is_page_valid( ) && @isset($_POST['next']))
{
	/*Edits out any slashes etc. which user enters into fields to ensure there are no errors when
	the data is parsed over to the mySQL database. */
	if(!get_magic_quotes_gpc( ))
	{
		for($i=0; $i<sizeof($this->input_pages_array[$this->current_page-1]->input_fields_array); $i++)
		{
			if(get_class($this->input_pages_array[$this->current_page-1]->input_fields_array[$i])=="section_field");
			else $this->input_pages_array[$this->current_page-1]->input_fields_array[$i]
/*THIS IS WHERE THE ERROR ORIGINALLY WAS:*/=addslashes(($_POST[$this->input_pages_array[$this->current_page-1]->input_fields_array[$i]->get_field_name( )]));
		}
	}
		
//Adds the validated input_page data into the database using the sql_query parsed to this webpage as well as the $db_connection specified at the top of this page
	$result = $this->db_connection->query($this->input_pages_array[$this->current_page-1]->sql_query)
		or die("Cannot execute query ".mysql_error()."<br>".$this->input_pages_array[$this->current_page-1]->sql_query);

And the code I'm using to create the query is as follows:

Code: Select all

$marina_info_query = "INSERT into marinas (marina_name, abn, region_id, contact_first_name, contact_last_name, street_address,
					suburb, state, postcode, postal_address, postal_state, postal_postcode, telephone_work,
					telephone_fax, telephone_mobile, telephone_emergency, email, secure_parking) 
			VALUES ('".$db_connection->real_escape_string($_POST['marina_name'])."',
				'".$_POST['abn']."',
				'".$region_id."',
				'".$db_connection->real_escape_string($_POST['contact_first_name'])."',
				'".$db_connection->real_escape_string($_POST['contact_last_name'])."',
				'".$db_connection->real_escape_string($_POST['street_address'])."',
				'".$db_connection->real_escape_string($_POST['suburb'])."',
				'".$db_connection->real_escape_string($region_state)."',
				'".$_POST['postcode']."',
				'".$db_connection->real_escape_string($_POST['postal_address'])."',
				'".$db_connection->real_escape_string($_POST['postal_state'])."',
				'".$_POST['postal_postcode']."',
				'".$_POST['telephone_work']."',
				'".$_POST['telephone_fax']."',
				'".$_POST['telephone_mobile']."',
				'".$_POST['telephone_emergency']."',
				'".$db_connection->real_escape_string($_POST['email'])."',
				'".$secure_parking."')";
mcccy005
Forum Contributor
Posts: 123
Joined: Sun May 28, 2006 7:08 pm

Post by mcccy005 »

Turns out that I was using an int type in mysql for abn which turned out to be an 11 digit number; not 10 as is the limit for int!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

A list of limits for numeric types in mysql can be found at http://dev.mysql.com/doc/refman/5.1/en/ ... types.html

p.s: You shouldn't use mysql_error directly but whatever function/property $this->db_connection provides.
Post Reply