Hi I know that multiple checkbox values has been discussed earlier but I have't got any wiser starring at them.

This is how I want the database entries to look like:

id ... drillID ...kommentar
 ====================
1559...1...........NULL
 -------------------------------
1559...2...........NULL
 -------------------------------
1559...3...........NULL

With the code below I can print out both id and drillID on the screen but it will only insert the last checked box value along with the id to the DB table why? The "kommentar" column is for later use.

The echo:
==========
1559    1
1559    2
1559    3

The insert:
===========
1559    3    NULL



<?php


$id = $_POST["spelarID"];
?>
<?php //echo $spelarID ?>

<?php
// loopa igenom din array 
foreach ($_POST['drillSelect'] as $value) 
{ 
     // skriv ut innehållet 
echo $id;  ?> &nbsp;&nbsp;  <?php   echo $value . "<br />"; 
} 


?>




<?php 
$insertSQL = sprintf("INSERT INTO exercise_spelare_ref (id, drillID) VALUES ('$id', '$value')") ;

 mysql_select_db($database_connect, $connect);
  $Result1 = mysql_query($insertSQL, $connect) or die(mysql_error());


 ?>

i would like to help you but the code looks ...

your talking about checkbox problem, but you didnt put any code involving the checkboxes.

Dark

You must be finding the way to insert multiple rows at a time. Check this post

Zero13

Thank You for the link I couldn't get it to work though.

Dark
Thanks for Your help I will try to be moore precise.

The page with the checkboxes here

The code

<form id="form1" name="form1" method="POST" action="untitled3.php">
  <table width="200">
    <tr>
      <td><label>
        <input type="checkbox" name="drillSelect[]" value="1" id="drillSelect" />
        Checkbox</label></td>
    </tr>
    <tr>
      <td><label>
        <input type="checkbox" name="drillSelect[]" value="2" id="drillSelect" />
        Checkbox</label></td>
    </tr>
    <tr>
      <td><label>
        <input type="checkbox" name="drillSelect[]" value="3" id="drillSelect" />
        Checkbox</label></td>
    </tr>
  </table>
  <input name="spelarID" type="hidden" id="spelarID" value="1559" />
  <input type="submit" name="submit" id="submit" value="Submit" />
  <input type="hidden" name="MM_insert" value="form1" />

</form>

The insert page code (actually I would prefere to have it all in one page). The problem is that it only inserts the value from the last checked checkbox. But if it can echo all 3 values (123) if checked along with the id (1559 in this case) it must be able to insert them aswell I think.

1559 1
1559 2
1559 3

<?php require_once('fileName.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_connect, $connect);
$query_Recordset1 = "SELECT analys_kund.id, analys_kund.user_login, analys_kund.first_name, analys_kund.last_name, exercise.drillID, exercise.categori, exercise.rubrik FROM analys_kund, exercise";
$Recordset1 = mysql_query($query_Recordset1, $connect) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

$colname_rsLookup = "-1";
if (isset($_GET['id'])) {
  $colname_rsLookup = $_GET['id'];
}
mysql_select_db($database_connect, $connect);
$query_rsLookup = sprintf("SELECT * FROM exercise_spelare_ref WHERE id = %s", GetSQLValueString($colname_rsLookup, "int"));
$rsLookup = mysql_query($query_rsLookup, $connect) or die(mysql_error());
$row_rsLookup = mysql_fetch_assoc($rsLookup);
$totalRows_rsLookup = mysql_num_rows($rsLookup);
$query_Recordset1 = "SELECT analys_kund.id, analys_kund.user_login, analys_kund.first_name, analys_kund.last_name, exercise.drillID, exercise.categori, exercise.rubrik FROM analys_kund, exercise WHERE analys_kund.id = analys_kund.id";
$Recordset1 = mysql_query($query_Recordset1, $connect) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>






<?php


$id = $_POST["spelarID"];
?>
<?php //echo $spelarID ?>

<?php
// loopa igenom din array 
foreach ($_POST['drillSelect'] as $value) 
{ 
     // skriv ut innehållet 
echo $id;  ?> &nbsp;&nbsp;  <?php   echo $value . "<br />"; 
} 


?>




<?php 
$insertSQL = sprintf("INSERT INTO exercise_spelare_ref (id, drillID) VALUES ('$id', '$value')") ;

 mysql_select_db($database_connect, $connect);
  $Result1 = mysql_query($insertSQL, $connect) or die(mysql_error());


 ?>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

</body>
</html>
<?php
mysql_free_result($Recordset1);

mysql_free_result($rsLookup);
?>

This seems to work:

<?php
$id = $_POST['spelarID']; 
    $drills = $_POST['drillSelect']; 

$inserts = array(); 
foreach ($drills as $drills)
    $inserts[] = "('$id','$drills','')"; 

$query = "INSERT INTO exercise_spelare_ref VALUES ". implode(", ", $inserts); 

//echo "query = $query"; // for debugging purposes, remove this once it is working 
mysql_query($query) or die(mysql_error()); 
?>

Got it from here

Any thoughts?

Then ofcourse a new question appears. When I want to make a update page where the inserted records shows as Checked I cant manage to pull that out of the table.

In a non dynamic checkbox I would do something like:

<input <?php if (!(strcmp($row_rsRef['drillID'],1))) {echo "checked=\"checked\"";} ?> type="checkbox" name="CheckboxGroup1" value="1" id="CheckboxGroup1_0" />

        chkbox1</label>

Any idea anyone?

I really need help on this!

I can insert values in the lookup table like this

id   drillID   kommentar
=======================
1559    1        NULL
1559    2        NULL
1559    3        NULL
1560    1        NULL

To get the checkboxes checked I think it woulkd be something like:
if row exixts echo checked, but cant get it to work!!!!

The form is a loop like this:

<form id="form1" name="form1" method="POST" action="insertDrills.php">
  <?php 
// Horizontal Looper start code for Recordset1

do {
ob_start();
?>


<input type="checkbox" name="drillSelect[]" value="<?php echo $row_Recordset1['drillID']; ?>" id="drillSelect" />
    <?php echo $row_Recordset1['rubrik']; ?>
    </label>



    <?php
// HLooper manage the arrays for Recordset1
$HLoop_Recordset1_temp = ob_get_contents(); //dump buffer to variable
ob_end_clean(); //clear buffer
$HLoop_Recordset1[$HLoop_Recordset1_i][$HLoop_Recordset1_ii] = $HLoop_Recordset1_temp;
$HLoop_Recordset1_ii++;
if($HLoop_Recordset1_ii >= $HLoop_Recordset1_loopTo){
    $HLoop_Recordset1_i++;
    $HLoop_Recordset1_ii = 0;
    $HLoop_Recordset1_actualrows++;
};
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
if($HLoop_Recordset1_actualrows < $HLoop_Recordset1_rows && $HLoop_Recordset1_vertical == "false") $HLoop_Recordset1_rows = $HLoop_Recordset1_actualrows;
if($HLoop_Recordset1_actualrows < $HLoop_Recordset1_columns && $HLoop_Recordset1_vertical == "true") $HLoop_Recordset1_columns = $HLoop_Recordset1_actualrows;

?>
  <table class="hloop" align="center" width="100%">

    <?php for($i = 0; $i < $HLoop_Recordset1_rows; $i++) { ?>
    <tr>
      <?php for($ii = 0; $ii < $HLoop_Recordset1_columns; $ii++) { ?>
      <td width="25%"><?php
            if($HLoop_Recordset1_vertical == "true") {
              echo(isset($HLoop_Recordset1[$ii][$i]) ? $HLoop_Recordset1[$ii][$i] : "&nbsp;");
             }else{
              echo(isset($HLoop_Recordset1[$i][$ii]) ? $HLoop_Recordset1[$i][$ii] : "&nbsp;");
            } ?></td>
      <?php } ?>
    </tr>
    <?php } // End Horizontal/Vertical Looper code for Recordset1 ?>
  </table>
  <input name="spelarID" type="hidden" id="spelarID" value="<?php echo $row_rsSpelare['id']; ?>" />
  <input type="hidden" name="MM_insert" value="form1" />
  <input type="submit" name="submit" id="submit" value="Välj övningar" />
  <input type="button" value="Avbryt" onclick="window.close();"></button>
</form>

Am I anywhere close to it when trying:

<?php 
$id = $row_rsRef['id']; 
$drills = $row_Recordset1['drillID'];
//echo $id;
//echo $drills;
$arr = array($id,$drills);
$selected = implode(", ",$arr);


?>


<input <?php if(isset($row_rsRef['$selected']))  {echo "checked=\"checked\"";} ?> type="checkbox" name="drillSelect[]" value="<?php echo $row_Recordset1['drillID']; ?>" id="drillSelect" />
    <?php echo $row_Recordset1['rubrik']; ?>
    </label>

I can echo the two first variables besides the checkboxes with correct values, then I think I should be able to use it for "echo checked" to.

diafol!

Thank you so much for your reply to my earlier thread! I tried it but couldn't get it to rock! In the same time while I was struggling with that I got another input that I could use, a dreamweaver extention from Tom Muck that would loop my checkboxes. I got extatic over it and forgot to reply to your answer at that time, really sorry for that!

I have read many of your postings people has allways got the help they needed and of course you have spent a lot of time and effort helping people out once again thank you and a reply is the least yuo should expect from us!

Member Avatar for diafol

No prob. Just didn't want to rehash the technique to fit your new question if you didn't want to go down that route. Happy trails. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.