I have a table where the last two columns are populated with true or false - and only two records should be true at any one time. Thus I have set up an interface where all the relevent records are displayed with radio buttons in the final two columns. The radio buttons are arrayed vertically, and within each column all have the same name="" value so that users can not do multiple selections.

I want to allow users to change select a new radio button, thus facilitating the need to UPDATE four records whos unique ids cannot be known beforehand.

I am fairly new to php/mysql, so explicit examples that I can cut and paste would be appreciated.

Thanks in advance,

./empnorton

    • [deleted]

    "I want to allow users to change select a new radio button, thus facilitating the need to UPDATE four records whos unique ids cannot be known beforehand."

    Seems to me that you'll want to UPDATE all the records to set both columns to FALSE, and then run four updates to set the selected rows back to 'true' for the appropriate column (is that many are selected in the first place ofcourse)

      What you say has merrit - and I would only have to set two records to "true" (one for each column).

      I think I know how to set all the records to false, but I would still need to isolate the two new "true" states, and record those. Do you have an example of the code for this?

        • [deleted]

        "I think I know how to set all the records to false, but I would still need to isolate the two new "true" states,"

        Through the value of the checkbox 'ofcourse' 🙂

          Okay, I figured out part of this, and wrote the script below. However, whenever I submit, the script errors out.

          Anyone spot what I have done wrong?

          //radio buttons (these are generated in a loop)
          <input name="spotlight_l" type="radio" value="' . $id . '" checked="checked"/>
          //submit script

          <?php
          if($submit) {
          foreach ($HTTP_POST_VARS as $name=>$value){
          $i=1;

          if ($name == "spotlight_l"){

          $SQL = "UPDATE homepage SET spotlight_l='$i' WHERE hpid='$value'";
          $result = mysql_query($sql) or die("db query failed_1");

          } else {

          if ($name == "spotlight_r"){

          $SQL = "UPDATE homepage SET spotlight_r='$i' WHERE hpid='$value'";
          $result = mysql_query($sql) or die("db query failed_2");}
          }
          }
          }
          ?>

            Write a Reply...