PHP - Moved: How To Create Foreign Key In Phpmyadmin?
This topic has been moved to PHP Applications.
http://www.phpfreaks.com/forums/index.php?topic=359282.0 Similar TutorialsThis topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=355885.0 Hi, I cannot find where to add foreign key constraints in phpmyadmin! Any help is appreciated! This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=355485.0 This topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=352409.0 This topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=314698.0 This topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=309461.0 This topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=323073.0 This topic has been moved to mod_rewrite. http://www.phpfreaks.com/forums/index.php?topic=352032.0 This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=334031.0 This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=313625.0 This topic has been moved to Application Design. http://www.phpfreaks.com/forums/index.php?topic=314453.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=321735.0 This topic has been moved to Other Libraries and Frameworks. http://www.phpfreaks.com/forums/index.php?topic=355753.0 This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=349578.0 This topic has been moved to Linux. http://www.phpfreaks.com/forums/index.php?topic=307024.0 This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=318572.0 Hi, I'm having following problem when I try to add records in two tables, one (password) has primary key and student has a foreign key contraint with password. Anyone please clarify what Im doing wrong. Thanks ! SQL CODE: INSERT INTO student(id, firstname, surname,dob, class,nationality,houseno,line1,line2,city) VALUES('00010', 'test','test','1980-01-01','test','test','test','test','test','test'); INSERT INTO password (id, password,salt) VALUES('00010','abc','abcde'); ERROR: #1452 - Cannot add or update a child row: a foreign key constraint fails (`chr_fyp/student`, CONSTRAINT `student_ibfk_1` FOREIGN KEY (`id`) REFERENCES `password` (`id`) ON DELETE CASCADE ON UPDATE CASCADE) Hello, I posted here as although it's in regards to mysql the thing i want to ask is in regards to php itself. I have two tables below as follows: Code: [Select] CREATE TABLE `users` ( `uid` int(11) NOT NULL AUTO_INCREMENT, `status` char(10) NOT NULL, `username` varchar(15) NOT NULL, `email` varchar(50) NOT NULL, `password` char(32) NOT NULL, `reg_date` int(11) NOT NULL, `ip` varchar(39) DEFAULT NULL, PRIMARY KEY (`uid`), UNIQUE KEY `username` (`username`,`email`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `users_profiles` ( `uid` int(11) NOT NULL, `first_name` varchar(40) DEFAULT NULL, `last_name` varchar(50) DEFAULT NULL, `gender` char(6) DEFAULT NULL, `website` varchar(100) DEFAULT NULL, `msn` varchar(60) DEFAULT NULL, `aim` varchar(60) DEFAULT NULL, `yim` varchar(60) DEFAULT NULL, `twitter` varchar(15) DEFAULT NULL, UNIQUE KEY `uid` (`uid`), CONSTRAINT `users_profiles_ibfk_1` FOREIGN KEY (`uid`) REFERENCES `users` (`uid`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8; From the above you can see the uid in the users_profiles table is a foreign key and references the uid in the users table to link both tables together. My scenario is this: A user fills out a registration form that asks for username, email and a password. After validating the form data etc it is inserted into the users table. Now at this stage they have no profile in the users_profiles table as they only just signed up. They login and xxx data is stored in a session along with there uid from users table. They now visit there profile, they fill in the profile form fields and submit the form. Now can someone tell me if what i think i should be doing is correct, if not please tell me what i should do or a better way: After validating the data that was submitted on profile form etc i could do something like a simple if else statement which will first check to see if the uid in session which is from users table matched a uid in the users_profiles table. If there's no match which in this case it won't be as they are a new user and don't have a profile record in users_profiles table then do an INSERT query into users_profiles table which would insert the submitted values from the form and the uid from session, on success that would now link the user from users table to there newly created profile in the users_profiles table. But if the uid in session matched a uid in user_profiles then obviously this means the user already has a profile ie.. submitted profile info previously so do an UPDATE query instead. Am i rite in thinking this is how i would do it? example below, note it's not valid code just to try an illustrate what i am saying; Hopefully! Code: [Select] <?php if($_SESSION['uid'] == user_profiles.uid) { /* If the uid in session which is the uid from users table * (we got it on successful login) * matches a users uid in user_profiles table then profile already exists * for user so do an UPDATE query here */ } else { /* $_SESSION['uid'] does not match a uid in users_profiles table * so instead do a INSERT QUERY here. This means no profile exists for user. * The uid from session is inserted into users_profiles table (column uid) * which links the profile to user. */ } ?> I know if i delete a user it will also delete there profile if they have one like it should do to obviously not leave redundant data in the database and to ensure data intergity (sorry i think that's what it's called) Thanks for any help. PHPFAN I want to allow users to post entries to a NEWS table and, if they wish, to post an accompanying image to an IMAGES table. Tables are like this: NEWS id // if there'll be an accompanying image, this id to be sent to IMAGES table title subtitle created news_entry category IMAGES image_id f_news_id // the foreign id of the associated post in NEWS table filename caption description So, the user comes to the insert_entry.php page and creates a post. If the user clicks an "Upload accompanying image" link, the news post id must be inserted in the f_news_id field when the image is uploaded. Code excerpt from insert_entry.php: Code: [Select] // Insert the news_entry in the database... // Make the query: $q = "INSERT INTO news (title, subtitle, news_entry, category) VALUES ('$title', '$subtitle', '$news_entry', '$category') "; $r = @mysqli_query ($dbc, $q); // Run the query. if ($r) { // If it ran OK. // Print a message: echo "<h1>Thank you!</h1> <p>You have successfully inserted the News Entry below.</p>"; echo "<h1>" . stripslashes($title) . "</h1><h2>" . stripslashes($subtitle) . "</h2><p>" . stripslashes($news_entry) . "</p>"; // get id of record just created $q = "SELECT id FROM news ORDER BY created DESC LIMIT 1"; $r = mysqli_query($dbc, $q); while ($row = mysqli_fetch_assoc($r)) { // pass the id via GET in the URL echo "<a href='upload_image.php?=" . $row['id'] . "'>Upload image</a>"; } mysqli_close($dbc); ?> Code excerpt from upload_image.php: Code: [Select] // insert news post id into images table if user came via insert_entry.php page // Make the query: require_once ('includes/mysqli_connect.php'); // Connect to the db. $description = mysqli_real_escape_string($dbc, trim($_POST['description'])); $caption = mysqli_real_escape_string($dbc, trim($_POST['caption'])); if (isset($_GET['id'])) { // if there's a NEWS post id $q = "INSERT INTO images (f_news_id, filename, caption, description) VALUES ('$_GET['id']', '{$_FILES['upload']['name']}', '$caption', '$description')"; } else { // if user arrived at upload_image.php otherwise and there's *not* a NEWS post id $q = "INSERT INTO images (filename, caption, description) VALUES ('{$_FILES['upload']['name']}', '$caption', '$description') "; } $r = @mysqli_query ($dbc, $q); // Run the query. if ($r) { // If it ran OK. // Print a message: echo "<p>Info entered in images table.</p>"; Am I going about this the wrong way? Am new to php... so any advice much appreciated... |