Im relativity new to PHP and MySQL and i have come up against a problem. i have 2 tables that are linked via foreign key constraints. Code: -- -- Table structure for table `rides` -- CREATE TABLE rides ( ride_date TIMESTAMP, ride_name VARCHAR(50) NOT NULL, planned_distance_travelled INT(5), actual_distance_travelled INT(5), planned_time_taken INT(5), actual_time_taken INT(5), average_heart_rate INT(5), calories_burned INT(5), weather_conditions VARCHAR(100), feelings_about_ride VARCHAR(100), PRIMARY KEY (ride_date) )ENGINE=InnoDB DEFAULT CHARSET=utf8;
As you can see they are linked by the ride_date, which is a timestamp. This means that inorder to successfully insert data into the tables, the data must be inserted at the same time to the second.
This is the PHP script i am using to insert the data into the 2 tables: Code: <?php
//add the date into the rides table $sql="INSERT INTO rides (ride_name, planned_distance_travelled, actual_distance_travelled, planned_time_taken, actual_time_taken, average_heart_rate, calories_burned, weather_conditions, feelings_about_ride) VALUES ('$ride_name','$planned_dist_trav','$actual_dist_trav','$planned_time_tak','$actual_time_tak','$avg_heart_rate','$calories','$weather_cond','$feelings')"; $result=mysql_query($sql);
if (!$result) { die('Error: ' . mysql_error()); }elseif ($result) { echo "1 Record Added to the rides table"; echo "<BR>"; }
//add the date into the usersrides table $sql1="INSERT INTO usersrides (userid, weight) VALUES ('$userid','$weight')"; $result1=mysql_query($sql1);
if (!$result1) { die('Error: ' . mysql_error()); }elseif ($result1) { echo "1 Record Added to the usersrides table"; //header("Refresh: 2; url=home.php"); } mysql_close()
?>
I realise this is alot of reading, but i didnt know how to explain this situation any other way. At the moment, data is being inserted into the 'rides' table, but when it gets to the usersrides table a foriegn key error comes up.
What i would like to know is....Is what im trying to do possible?