I have written the below to read in data from a radio button (var1) and some checkboxes (var2)

   require('config.php');

  $con = mysql_connect("localhost", $db_username, $db_password);
    if (!$con)
    {
      die('Could not connect: ' . mysql_error());
    }
    mysql_select_db($db_name, $con);

  $var1 = $_GET['var1'];
  $var1 = mysql_real_escape_string( $var1 );
  $var2 = $_GET['var2'];
  $var2 = mysql_real_escape_string( $var2 );  

  $running_total = 0;
  $last_update = "SELECT * FROM $db_tablename ORDER BY id DESC LIMIT 1";
  $result = mysql_query($last_update);

 $insert_query = "INSERT INTO $db_tablename (var1, var2) ";
  $insert_query .= sprintf( "VALUES('%s', '%s')", $var1, $var2);

  $insert_result = mysql_query($insert_query);
  if(!$insert_result) {
    die('insert query failed' . mysql_error());
  }

  mysql_close($con)

The input html looks like...

<input type="radio" name="var1" value="value1">Test 1</input>
<input type="radio" name="var1" value="value2">Test 2</input>
<input type="radio" name="var1" value="value3">Test 3</input>
<input type="radio" name="var1" value="value4">Test 4</input>

<input type="checkbox" name="var2" value="checkbox1">CB 1</input>
<input type="checkbox" name="var2" value="checkbox2">CB 1</input>
<input type="checkbox" name="var2" value="checkbox3">CB 1</input>
<input type="checkbox" name="var2" value="checkbox4">CB 1</input>

Everything works as expected and but as var2 allows for multiple checkboxes to be selected I would like to save multiple values. I realise I need to do this as an array but I cant work it out.

Can anyone help or have a solution or example they can point me to?

share|improve this question
feedback

4 Answers

Change the name of var1 to "var1[]", and change the name of var2 to "var2[]"...

 $var1 = $_GET['var1'];
 $var2 = $_GET['var2'];
 echo count($var1);
 echo count($var2);

Gl mf

share|improve this answer
the radio buttons (var1) dont need to have the [] as its only a single value – Ronnie 11 hours ago
feedback

it would be var2[] as the name and view the results as

foreach($_POST['var2'] as $checkedItem)
{
    echo $checkedItem;
}
share|improve this answer
feedback
<input type="checkbox" name="var2" value="checkbox1">CB 1</input>
<input type="checkbox" name="var2" value="checkbox2">CB 1</input>
<input type="checkbox" name="var2" value="checkbox3">CB 1</input>
<input type="checkbox" name="var2" value="checkbox4">CB 1</input>

should be

<input type="checkbox" name="var2[]" value="checkbox1">CB 1</input>
<input type="checkbox" name="var2[]" value="checkbox2">CB 1</input>
<input type="checkbox" name="var2[]" value="checkbox3">CB 1</input>
<input type="checkbox" name="var2[]" value="checkbox4">CB 1</input>

UPDATE

You are incorrectly handling the submission. var2 is an array. Try:

$checkboxes = $_GET['var2'];
foreach ($checkboxes as $var2) {
    $insert_query = "INSERT INTO $db_tablename (var1, var2) ";
    $insert_query .= sprintf( "VALUES('%s', '%s')", $var1, $var2);
}
share|improve this answer
I have tried that but it doesn't save anything at all in the SQl database – fightstarr20 11 hours ago
@fightstarr20 That is because you are incorrectly handling $_GET['var2']. See my updated answer. – czarpino 10 hours ago
feedback

The html: `

<input type="radio" name="var1" value="value1">Test 1</input>
<input type="radio" name="var1" value="value2">Test 2</input>

<input type="checkbox" name="var2[]" value="check" /> CB 1
<input type="checkbox" name="var2[]" value="check" /> CB 2

`

and the SQL :

Array $_POST['var2'] contains the checkboxes.

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.