-1

I have created an HTML form for check box:

<td>   
    <input type="checkbox" name="mode[]" id="mode" value="sweet"/>Sweet<br />
    <input type="checkbox" name="mode[]" id="mode" value="sour"/>Sour<br />
    <input type="checkbox" name="mode[]" id="mode" value="creamy"/>Creamy<br />
    <input type="checkbox" name="mode[]" id="mode" value="bland"/>Bland<br />                
</td>

This is action code for php page:

for ($i=0; $i<sizeof($mode); $i++)
{
    $mode[$i] = mysql_real_escape_string($mode[$i]);

    $query  = "INSERT INTO frutesdetails(fruitname,fruitcolor,seasonfrom,seasonto,fruitetaste,fruitbenefit) VALUES ('{$fruitname}', '{$category}', '{$startdate}', '{$enddate}','.$mode[$i].','{$fruitbenefit}')";   
}
$insertresults = mysql_query($query) or die(mysql_error());
    ?>

I am getting only one value of check box not all.

4444
  • 3,541
  • 10
  • 32
  • 43
Niks Niks
  • 11
  • 1
  • 5
  • checkboxes only transmit value `on` as per my experience. Do you get the value defined by the value attribute instead? – DevZer0 Jul 17 '13 at 13:19
  • 1
    @DevZer0 — They only send `on` if you fail to provide a value. – Quentin Jul 17 '13 at 13:20
  • 1
    You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Jul 17 '13 at 13:20
  • why don't you use foreach? it is faster than your code. check [PHP benchmark (Reading loop)](http://www.phpbench.com/) for it. it causes in that your loop will check in every step the size of your array. – Michael Walter Jul 17 '13 at 13:23

2 Answers2

5

You have to pass the query to the database each time you go around the loop.

At the moment, you construct a query for each submitted item, but only pass the last one to the database.

Move the mysql_query call so it is inside the loop.


That said, you would be better off moving to a modern API (like PDO), constructing your query as a prepared statement before the loop beings, and then executing it with your data in the loop.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • if i use mysql_query in side the loop i get all the record but not as one record – Niks Niks Jul 17 '13 at 13:22
  • i got all the record in to different different row not in one row – Niks Niks Jul 17 '13 at 13:32
  • Then insert all the non-colour data into the table, get the row id back, then use that row id to loop over your data and create new entries in your frutesdetails_colours [junction table](http://en.wikipedia.org/wiki/Junction_table). – Quentin Jul 17 '13 at 13:35
0

Update: This has been edited to answer the question better

From your answers it appears you want to join all the mode responses together and execute a single insert query.

Incidentally:

  • it is not good practise to wriite escaped values back into the same array as the original
  • look into binding your input into mySQL statements instead of physically inserting them

Revised code:

foreach ($mode as $m)
{
   $emodes[] = mysql_real_escape_string($m); 
}

$emode = join( ',' , $emodes);


$query  =<<<EOF
INSERT INTO frutesdetails(fruitname,fruitcolor,seasonfrom,seasonto,fruitetaste,fruitbenefit) 
VALUES ('{$fruitname}', '{$category}',
        '{$startdate}','{$enddate}','{$emode}','{$fruitbenefit}')
EOF;
$insertresults = mysql_query($query) or die(mysql_error()); 


?>
vogomatix
  • 4,856
  • 2
  • 23
  • 46