Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Update Foreach Loop Multi Records

Hi there, hope have some help. I have a form where a user select time and days of the week (monday, tuesday..) that he is available. The insert script works fine, it insert and the days of week he checked in the table, the only problme is when i try to update each of them.

I used this code above, but is not working, instead of updating/inserting days that is selected is inserting multi-records with the same day of the week value.

here it is:


foreach($weekDay as $DaysofWeek){

$sql_days = "UPDATE available SET day_time = '$day_time', week_day = '$DaysofWeek' WHERE user_id = '$id_user'";

$update_availability = mysql_query($sql_days);

}


1 Answer

Found Solution, was so obvious, cant understand how i didnt thought about it...

$SQL = "DELETE FROM disponibilidade WHERE aluno_id = ".$id_aluno;

$query = mysql_query($SQL);
foreach($diaSemana as $diaSemanas){

$sql_disponibilidade = "INSERT INTO disponibilidade 
                    (aluno_id,tempo_dia, dia_semana) 
                    VALUES 
                    ('$id_aluno','$tempoDia','$diaSemanas')";
                    $inserir_disponibilidade = mysql_query($sql_disponibilidade);
                    }