0

I have 9 check boxes in my select.php

<form method="post" action="test.php">
<input type="checkbox" name="g1[]" id="c1" value="c1">
<input type="checkbox" name="g1[]" id="c2" value="c2">
<input type="checkbox" name="g1[]" id="c3" value="c3">

<input type="checkbox" name="g2[]" id="h1" value="h1">
<input type="checkbox" name="g2[]" id="h2" value="h2">
<input type="checkbox" name="g2[]" id="h3" value="h3">

<input type="checkbox" name="g3[]" id="d1" value="d1">
<input type="checkbox" name="g3[]" id="d2" value="d2">
<input type="checkbox" name="g3[]" id="d3" value="d3">
</form>

ok my test.php looks like this now:

error_reporting(E_ALL);

$g1 = $_POST['g1']; 
$g2 = $_POST['g2']; 
$g3 = $_POST['g3'];

//Connect to DB

$ng1 = count($g1);
$ng2 = count($g2);
$ng3 = count($g3);

 $sum = 0; 
 for ($i = 1; $i <= 3; $i++) 
 { 
     $arr = "g$i"; 
     if (!isset($_POST[$arr])) 
         ${$arr} = array(); 
     else 
         ${$arr} = $_POST[$arr]; 
     if (!is_array(${$arr})) 
         die("Error in input parameter $arr"); 
     ${"ng$i"} = count(${$arr}); 
     if (${"ng$i"} < 1) 
         die("At least one $arr checkbox must be checked"); 
     $sum += ${"ng$i"}; 

     ${"g$i"."_sql"} = implode(',', array_map(${$arr}, 'mysql_real_escape')); 
 } 

 $query="INSERT INTO ch_lg (g1, g2, g3) VALUES ('$g1_sql','$g2_sql','$g3_sql')"; 

 mysql_query($query) or die(mysql_error()); 
 mysql_close(); 

//echo message
}

Modifid Question

I need to check that:

  1. The user chose 1 checkbox from each array (your code does this)

  2. He didn't select more than 4 checkboxes (your code does this)

  3. He chose 1 more checkbox from JUST one array (meaning that he has to chose 4 in total and that just 1 of each is not acceptable) - I think this is not happening in your code. Does it?

Thank you

5
  • Shouldn't "action=test.php" be action="test.php"? Commented Jul 10, 2012 at 17:56
  • sorry yes... typing mistake. ty Commented Jul 10, 2012 at 17:57
  • I think your IFs are wrong. "At least one in each array": so if (($ng1 < 1) || ($ng2 < 1) || ($ng3 < 1)) die("At least one in each array"); and "no more than three": if (($ng1+$ng2+$ng3)>3) die("No more than three"). Finally, isn't this condition the same as "ng1, ng2, ng3 must all be EXACTLY EQUAL TO ONE?" Commented Jul 11, 2012 at 6:17
  • @Iserni So I have to change the way of "thinking" to get the same result? I have to use just 1 IF for each of my checks? Commented Jul 11, 2012 at 6:39
  • @lserni I have edited my code. But still have problems. ty Commented Jul 11, 2012 at 7:37

1 Answer 1

2

"No more than 3 in both of them" means that 2 hair + 1 color is OK, but 2 hair + 2 color is not because it would give 4? Then:

$ncolor = count($color);
$nhair   = count($hair);
if (($ncolor >= 1) && ($nhair >=1) && (($ncolorn+$nhair)<=3))
   // OK
else
   // No good.

and then insert my values into db columns COLOR / HAIR.

You have to explain how your DB is structured and how do you want the data. If I get 2 hair and 1 color, do you want:

  • 3 rows, one with color, two with hair
  • 2 rows, one with color and hair, one with hair alone
  • 1 row with colors and hairs coalesced with separators:

    $color_sql = implode(',', array_map($color, 'mysql_real_escape')); $hair_sql = implode(',', array_map($hair, 'mysql_real_escape'));

    INSERT INTO mytable (..., color, hair, ...) VALUES (...,'$color_sql','$hair_sql', ...);

Also, at the beginning of test.php I assume there is something like:

<?php
     error_reporting(E_ALL);
     $g1 = $_POST['g1'];
     $g2 = $_POST['g2'];
     $g3 = $_POST['g3'];

     ...

Verification could be done in a cycle:

<?php
     error_reporting(E_ALL);
     $sum = 0;
     for ($i = 1; $i <= 3; $i++)
     {
         $arr = "g$i";
         if (!isset($_POST[$arr]))
             ${$arr} = array();
         else
             ${$arr} = $_POST[$arr];
         if (!is_array(${$arr}))
             die("Error in input parameter $arr");
         ${"ng$i"} = count(${$arr});
         if (${"ng$i"} < 1)
             die("At least one $arr checkbox must be checked");
         $sum += ${"ng$i"};

         ${"g$i"."_sql"} = implode(',', array_map(${$arr}, 'mysql_real_escape'));
     }

     // NOTICE: 'NULL' between quotes? Should't it be NULL without quotes?
     // If ID is autoincrement, just omit it: (g1, g2, g3) VALUES ('$g1_sql',...)

     $query="INSERT INTO ch_lg (ID, g1, g2, g3) VALUES ('NULL','$g1_sql','$g2_sql','$g3_sql')";

     mysql_query($query) or die(mysql_error());
     mysql_close();
Sign up to request clarification or add additional context in comments.

8 Comments

can I have 1 row in my db and a coloumn HAIR that will take bot balues e.g 1,2
@pavlos1316 $color_string = implode(',', $color) will convert them to such a string, explode(',', $color_string) would convert it back. That may or may not be a good solution; I think there's still a little confusion as to what you're trying to achieve and why.
Yes, but now you are using three cb arrays, not two. This changes the behaviour of the IFs. See comment to the other thread.
@lserni I have {$g1 = $_POST['g1'];} in the beggining of my code. As for the verification code, I need to check for 4 things. 1. If user didn't select at least 1 cb from each array, 2. if he has select exactly 1 from each array, 3. if he has selected 1 from each array and a second one from just one array (here i insert to db) and 4. if he has selected more than one cb in each array. and I understant it better with my code. I don't quite understant what we are checking with yours.
Of course your code seems better and less lines, but how can I check for all 3 options? ty
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.