Forum Moderators: coopster

Message Too Old, No Replies

Checkboxes issues!

         

toltec75

10:09 pm on Sep 7, 2004 (gmt 0)

10+ Year Member



I posted a topic a few weeks ago about writing checkbox values into database so that for each checkbox checked a new row is created in table with checbox values and coresponding username!

This is what I got and it works fine:

if (isset($_POST['music'])) {

foreach ($_POST['music'] as $music) {

$sql = "INSERT INTO table VALUES
('$user', '$music')";
$result = mysql_query($sql) or die (mysql_error());

}

I wonder how can a user for instance insert new values (check different checkboxes), so that the old values with his name are deleted and new ones inserted into database the same way I wrote above!

Thank you!

Zipper

10:42 pm on Sep 7, 2004 (gmt 0)

10+ Year Member



If you don't want the old values, it probably makes sense to delete them before inserting the new ones. Otherwise you will have to check each one of them and match it with the new values.
If you're up with the deletion, run the below query before the rest.
DELETE FROM table WHERE user = '$user'

hughie

10:43 pm on Sep 7, 2004 (gmt 0)

10+ Year Member



very simply....

if (isset($_POST['music'])) {

// new bit
$sql=mysql_query("DELETE FROM table WHERE Username='$user'");
// end new bit

foreach ($_POST['music'] as $music) {

$sql = "INSERT INTO table VALUES
('$user', '$music')";
$result = mysql_query($sql) or die (mysql_error());

}

ta,
Hughie

hughie

10:43 pm on Sep 7, 2004 (gmt 0)

10+ Year Member



snap ;-)

toltec75

10:58 pm on Sep 7, 2004 (gmt 0)

10+ Year Member



Thanx guys! :)

SkyDog

3:48 pm on Sep 10, 2004 (gmt 0)

10+ Year Member



The way ideal for handling checkboxes is to make them set values in the database. Handling the data is a bit more complex, but alot less time consuming.