views:

37

answers:

3

i have a submit form that consist of 1 group radio button.

<div id="defectclass">
        <input id="def1" type="radio" class="defect" name="defect" value="1"/>S
        <input id="def2" type="radio" class="defect" name="defect" value="1" />A
        <input id="def3" type="radio" class="defect" name="defect" value="1" />B
        <input id="def4" type="radio" class="defect" name="defect" value="1" />C
</div>

beside that i have 4 fields at DB table, that is :

  1. S
  2. A
  3. B
  4. C

i want after submit:

    - if def1 are checked send value to field "S"
     - if def2 are checked send value to field "A"
     - if def3 are checked send value to field "B"
     - if def4 are checked send value to field "C"
     - if all not checked or null send to all fields value="0"

how do i do that bcoz i've never try this?

A: 

By looking at the possible values of defect , in your table definition,

change defect VARCHAR(1) to defect VARCHAR(1) DEFAULT "0"

This simplifies your code to insert row/update columns

in your php script, after parsing the form, add a switch statement to decide which column to update.



// I assumed the row already exists

mysql_connect (localhost, $username, $password);
@mysql_select_db ($database) or die("Error");

$defect = mysql_real_escape_string($_POST["defect"]); //parse the defect field
$id = mysql_real_escape_string$_POST["id"]); //primary key of the table
$flag = True;

if(!empty($_POST['defect']))
{

   switch($defect)
   {
       case "S" : $var = "S";
                  break;
       case "A" : $var = "A";
                  break;
       case "B" : $var = "B";
                  break;
       case "C" : $var = "C";
                  break;
       default : $flag = False;
   }

   if($flag)
   {
      $query = "UPDATE TABLE_NAME SET" + $var + " = '1' WHERE Id = '$id'";
      mysql_query($query);
   }

   mysql_close(); 
}


Rahul
When down voted, leaving a comment would help me correct myself.
Rahul
Too much to correct. Not to mention this code is a sin against programming art - it repeats huge piece of code to change just ONE LETTER! And SQL injection as well.
Col. Shrapnel
horrible mistake. is the edit any better?
Rahul
No, because you only update one field. Suppose A = 1, now radio button S get selected. After executing your code both S and A will be 1, instead of only A.
captaintokyo
A: 
<div id="defectclass">
        <input id="def1" type="radio" class="defect" name="defect['s']" value="1"/>S
        <input id="def2" type="radio" class="defect" name="defect['a']" value="1" />A
        <input id="def3" type="radio" class="defect" name="defect['b']" value="1" />B
        <input id="def4" type="radio" class="defect" name="defect['c']" value="1" />C
</div>

This will give you $_POST['defect']['s'], &tc. so you'll know which update path to take. See http://php.net/faq.html for more about this syntax.

TML
Any feedback on why the -1? It should be a perfectly acceptable solution to the problem as given?
TML
it's not a solution at all. First of all it is not one radiobutton field but 4.
Col. Shrapnel
A: 
  1. You have to make just 1 field.
  2. If you need numbers to represent these letters make an array.

like this

$defects_arr = array(0,"S","A","B","C");

this array can be used in many tasks:

  • to represent these letters with numbers
  • to validate user rinput
  • to build this input dynamically

So, upon receiving your POST data just compare it against this array:

$defect = '0';
if(!empty($_POST['defect'])) {
  $key = array_search($_POST['defect'],$defect_arr));
}

and you will have a number in the $key variable. This number should be stored in the database.

Col. Shrapnel
whether by doing this can answer my initial question on that link?
klox
Hm, interesting solution, but I don't think this is what he is after. He wants 1 or 0, not 0, 1, 2, 3 or 4.
captaintokyo
@klox this is not a question by the link but just a little bit of a question. A stub. But an array solution still lets to convert letters to numbers.
Col. Shrapnel
@captain he wants, but it's not what he really need
Col. Shrapnel
@Col.:can i use this for answer my link too?
klox
@Col: can you answer at my link question, to prevent me become confuse bcoz of mixing question between this question and link question.
klox
@Col: i have edited my linked question.
klox