Forum Moderators: open
foreach ($daylist as $day) {
$queryins1="INSERT INTO coursedates (courseid, day, month, year, time) VALUES ('$courseid', '$day', '$monthcal', '$yearcal', '$time')
WHERE NOT EXISTS
(SELECT * FROM coursedates WHERE month = '$monthcal' AND year = '$yearcal' AND day = '".$dayofweek."')";// and not already present
mysql_query($queryins1) or die(mysql_error());
but this wont work, any ideas?
OR.. how could i do an insert into coursedates where the bookingid from coursedates is not in tblBookings?
thanks
//foreach loop here
foreach ($daylist as $day) {//if bookingid is in bookings update
$queryup="UPDATE coursedates SET courseid = '$courseid', day = '$day', month = '$monthcal', year = '$yearcal', time = '$time' WHERE course = $courseid AND month = '$monthcal' AND year = $yearcal' AND bookingid IN
(SELECT bookingid FROM bookings)";
mysql_query($queryup) or die(mysql_error());
//else
$queryins1="INSERT INTO coursedates (courseid, day, month, year, time) VALUES ('$courseid', '$day', '$monthcal', '$yearcal', '$time') WHERE bookingid NOT IN
(SELECT bookingid FROM bookings)";
mysql_query($queryins1) or die(mysql_error());
}
$queryins1="INSERT INTO coursedates (courseid, day, month, year, time) VALUES ('$courseid', '$day', '$monthcal', '$yearcal', '$time') WHERE bookingid NOT IN
(SELECT bookingid FROM bookings)";
mysql_query($queryins1) or die(mysql_error());
on its own without the update :(
and...
alter table coursedates
add unique (courseid, day, month, year, time)
but the unique constraint gives me a duplicate entry error.
try {
'insert ...'
} catch {
'update ...'
}
This assumes that you are more likely to be able to insert the data than not. If you think there will be more updates, you can switch the statement around like this:
try {
'update ...'
} catch {
'insert ...'
}
This assumes however that you have a primary key, and that the record you are trying to insert has that same key. Make sure that you test for the nature of the error, you want to test for the error_type because it could be something like string to long for field varchar(256) or something, don't assume that it will only error when it is a primary key violation.
try:
..insert the record
except:
..if error_value == duplicate key insertion (or whatever your db returns)
....then perform update rather then insert, or skip
[groups.google.com...]