Forum Moderators: coopster

Message Too Old, No Replies

Inserting data into a table with foreign keys

foreign keys

         

djupiter

9:50 am on Dec 11, 2008 (gmt 0)

10+ Year Member



I have a table that have foreign keys that references another table.The tables are

CREATE TABLE `company` (
`c_id` bigint(20) NOT NULL auto_increment,
`c_name` varchar(255) NOT NULL,
`cat_id` bigint(20) NOT NULL,
PRIMARY KEY (`c_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

CREATE TABLE `message` (
`m_id` bigint(20) NOT NULL auto_increment,
`mobile_no` varchar(100) NOT NULL,
`s_message` longtext NOT NULL,
`p_id` bigint(255) NOT NULL,
`c_id` bigint(20) NOT NULL,
`list_time` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY KEY (`m_id`),
KEY `c_id` (`c_id`),
KEY `p_id` (`p_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

CREATE TABLE `prefix` (
`p_id` bigint(20) NOT NULL auto_increment,
`c_abbrev` varchar(100) NOT NULL,
`c_id` bigint(20) NOT NULL default '0',
PRIMARY KEY (`p_id`),
UNIQUE KEY `c_abbrev` (`c_abbrev`),
KEY `c_id` (`c_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

I want to insert into the message table but still be able to store the two foreign keys c_id and p_id into the table

The code am using is

s_message = $_POST['s_message'];
$message_prefix = $_POST['message_prefix'];
$mobile_no = $_POST['mobile_no'];
$message_id = $_POST['message_id'];


$result = mysql_query ("INSERT INTO `message` ( `m_id` ,`list_time`,`mobile_no`,`p_id`,`s_message` ) VALUES ('',current_timestamp,'$mobile_no','$p_id','$s_message')
");


if (!$result)
die ('Invalid query: ' . mysql_error());
echo "<strong>Success! Thank you Your Message has been received.</strong>";
echo "<strong>Thank you and Welcome Again </strong>";
}

Please help

coopster

1:38 pm on Dec 23, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, djupiter.

I'm assuming you would know the company and prefix before you enter the message? It seems you would or should by the time you go to add the message entry. What error message is being returned?