Pregunta

This is the SQL code I am using:

For some reason it deletes all records for the UserID but does not take into account the NOT IN array. I need it to delete only if the user ID = the current user AND the courseID of that user is not in the array..

mysql_query("DELETE FROM tblLinkUserCourse WHERE ((UserID=$CurrentUserID) AND (CourseID NOT IN ({$new_array})))");
¿Fue útil?

Solución

1) Remove all unnecessary () from query.

2) Check $new_array contains all your course_id or not and constructed as string. If not constructed as string and comma separated use this code.

$new_array = implode(',', $new_array);
mysql_query("DELETE FROM tblLinkUserCourse WHERE UserID=$CurrentUserID AND CourseID NOT IN ({$new_array})");

Otros consejos

Is $new_array PHP array or string prepared for query? If it is array you should use:

$new_array = implod(',', $new_array);
mysql_query("DELETE FROM tblLinkUserCourse WHERE ((UserID=$CurrentUserID) AND (CourseID NOT IN ({$new_array})))");

Try:

$new_array  = "'".implode("', ", $new_array)."'";
$sql        = "DELETE FROM tblLinkUserCourse WHERE ((UserID=$CurrentUserID) AND (CourseID NOT IN ({$new_array})))";
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top