Inserting data into a table

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
lmhart
Forum Newbie
Posts: 15
Joined: Tue Apr 14, 2009 1:05 pm

Inserting data into a table

Post by lmhart »

Will someone please look at this and see if they can show me why two fields are not inserting into my table. I know the information is there because I also send an email out and all the information is present.

This is my insert statement. Everything is inserted except the phone and the date.

Code: Select all

 
<?php
 
include ("dbc.php");
 
 
mysql_query("INSERT INTO printers_table
                    (analyst,
                      crn_id,
                      contact_name,
                      phone,
                      email,
                      cost_center,
                      serial_number,
                      address,
                      reason,
                      date
                    )
                    VALUES
                    ('$_POST[analyst]',
                     '$_POST[crn_id]',
                     '$_POST[contact_name]',
                     '$_Post[phone]',
                     '$_POST[email]',
                     '$_POST[cost_center]',
                     '$_POST[serial_number]',
                     '$_POST[address]',
                     '$_POST[reason]',
                     '$_POST[date]'
                    )
            ")
                or
                die(mysql_error());
 
 
 
 
?>
 
Here is the code for the table

Code: Select all

 
CREATE TABLE `printers_table` (
`id` int(20) NOT NULL AUTO_INCREMENT,
`analyst` varchar(200) collate latin1_general_ci NOT NULL DEFAULT '',
`crn_id` varchar(200) collate latin1_general_ci NOT NULL DEFAULT '',
`contact_name` varchar(200) collate latin1_general_ci NOT NULL DEFAULT '',
`phone` varchar(200) collate latin1_general_ci NOT NULL DEFAULT '',
`email` varchar(200) collate latin1_general_ci NOT NULL DEFAULT '',
`cost_center` varchar(200) collate latin1_general_ci NOT NULL DEFAULT '',
`serial_number` varchar(200) collate latin1_general_ci NOT NULL DEFAULT '',
`address` varchar(200) collate latin1_general_ci NOT NULL DEFAULT '',
`reason` varchar(200) collate latin1_general_ci NOT NULL DEFAULT '',
`date` varchar(200) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
)
 
 
Last edited by Benjamin on Fri May 29, 2009 10:58 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Inserting data into a table

Post by mikemike »

Try some basic debugging such as echoing your query and $_POST. I can tell straight off that phone won't work becuase you're using $_Post[phone] instead of $_POST[phone].

Also, it's good practice to escape everything, and to not let PHP assume it's named keys. For example:
"INSERT INTO table (name, date, phone) VALUES ('$_POST[name]', '$_POST[date]'...
becomes:
"INSERT INTO table (name, date, phone) VALUES ('".$_POST['name']."', '".$_POST['date']."'...

You shoudl also escape these entries with something like mysql_real_escape_string()
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Inserting data into a table

Post by califdon »

Your $_POST array indexes must be enclosed in quotes, like: $_POST['phone'].
lmhart
Forum Newbie
Posts: 15
Joined: Tue Apr 14, 2009 1:05 pm

Re: Inserting data into a table

Post by lmhart »

mikemike wrote:Try some basic debugging such as echoing your query and $_POST. I can tell straight off that phone won't work becuase you're using $_Post[phone] instead of $_POST[phone].

That was the issue for the phone not submitting. However I still can not get the date to submit to the db.
Post Reply