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? Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment 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"); } ?> Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.