ec Posted March 26, 2008 Share Posted March 26, 2008 you know the way in access, you can link tables to ensure that a value cannot be entered in table 2 unless it exists in table one? how would you do that with php and mysql? Link to comment https://forums.phpfreaks.com/topic/97977-linking/ Share on other sites More sharing options...
trq Posted March 26, 2008 Share Posted March 26, 2008 Check if the value exists in table1 before inserting it into table2. Link to comment https://forums.phpfreaks.com/topic/97977-linking/#findComment-501318 Share on other sites More sharing options...
ec Posted March 26, 2008 Author Share Posted March 26, 2008 I think i've got it working now using this //Making sure that pupil number exists //Check for duplicate pupil no $qry = "SELECT count(*) AS c FROM pupil WHERE pupilno='$pupilno'"; $result = mysql_query($qry); if($result) { $result_array = mysql_fetch_assoc($result); if($result_array['c'] < 1) { $errmsg_arr[] = 'Pupil number is not in use'; $errflag = true; } @mysql_free_result($result); } else { die("Query failed"); } is there any eaiser way of doing it? Link to comment https://forums.phpfreaks.com/topic/97977-linking/#findComment-501333 Share on other sites More sharing options...
trq Posted March 26, 2008 Share Posted March 26, 2008 <?php $qry = "SELECT pupilno FROM pupil WHERE pupilno='$pupilno'"; if ($result = mysql_query($qry)) { if (mysql_num_rows($result)) { $result_array = mysql_fetch_assoc($result); } else { $errmsg_arr[] = 'Pupil number is not in use'; $errflag = true; } } else { die("Query failed"); } ?> Link to comment https://forums.phpfreaks.com/topic/97977-linking/#findComment-501347 Share on other sites More sharing options...
aschk Posted March 27, 2008 Share Posted March 27, 2008 You can use foreign key constraints, i.e. you need to insert a valid foreign key into table 2 for it to be a valid entry. Otherwise it fails. Link to comment https://forums.phpfreaks.com/topic/97977-linking/#findComment-502054 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.