0

How do you pass multiple value to a checkbox input's value attribute which already contains a value. I want to pass $row['ProductStartDate'] and $row['ProductEndDate'] to checkbox "check[]" along with it's own value $row['ProductID']. This is so that a calculation between ProductEndDate and ProductStartDate can occur for every ProductID checked from the checkbox.

$sql = 'SELECT * FROM product ORDER BY ProductID ASC';
$result_select = mysql_query($sql);
$rows = array();
while($row = mysql_fetch_array($result_select))
    $rows[] = $row;
foreach ($rows as $row) {
    echo '<tr>';
    echo '<td>'. $row['ProductID'] . '</td>';
    echo '<td>'. $row['ProductName'] . '</td>';
    echo '<td>'. $row['ProductPrice'] . '</td>';
    echo '<td>'. $row['ProductStartDate'] . '</td>';
    echo '<td>'. $row['ProductEndDate'] . '</td>';
    echo '<td><a class="btn" href="productupdate.php?id='.$row['ProductID'].'">Update</a></td>';
    echo ' ';
    echo '<td><a class="btn" href="productdelete.php?id='.$row['ProductID'].'">Delete</a></td>';
    echo ' ';
    echo '<td><input type="checkbox" name="check[]" value="'.$row['ProductID'] .'" ></td>';
    echo '</tr>';
}

$checkbox1 = $_POST['check'];
for ($i=0; $i<sizeof($checkbox1); $i++) {

    $date1 = strtotime($row['ProductStartDate']);
    $date2 = strtotime($row['ProductEndDate']);
    // Adding current month + all months in each passed year
    $diff = 1 + (date("Y",$date2)-date("Y",$date1))*12;
    // Add/subtract month difference
    $diff += date("m",$date2)-date("m",$date1);

    $sql3="INSERT into enrollment (StudentID, ProductID, EnrollmentMonth) VALUES($StudentID, $checkbox1[$i], $diff)";
    mysql_query($sql3);
}
  • This is not an answer but...stop using mysql extension https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – Sir_Faenor Jul 12 '17 at 08:36

2 Answers2

1

You can try below method

1) Just concat values which you need with any separater

echo '<td><input type="checkbox" value="'.$row['ProductStartDate'].':'.$row['ProductID'].':'.$row['ProductEndDate'].'" name="chk_export" /></td>';

2) on posting data segregate your values

$values = explode(":", $_POST['chk_export']);
$values[0],$values[1],$values[2]

will have desired values

or you can set Start Date and End date in hidden fields and get them when form posted

Passionate Coder
  • 7,154
  • 2
  • 19
  • 44
0
$sql = 'SELECT * FROM product ORDER BY ProductID ASC';
$result_select = mysql_query($sql);
$rows = array();
while($row = mysql_fetch_array($result_select))
    $rows[] = $row;
foreach ($rows as $row) {
    echo '<tr>';
    echo '<td>'. $row['ProductID'] . '</td>';
    echo '<td>'. $row['ProductName'] . '</td>';
    echo '<td>'. $row['ProductPrice'] . '</td>';
    echo '<td>'. $row['ProductStartDate'] . '</td>';
    echo '<td>'. $row['ProductEndDate'] . '</td>';
    echo '<td><a class="btn" href="productupdate.php?id='.$row['ProductID'].'">Update</a></td>';
    echo ' ';
    echo '<td><a class="btn" href="productdelete.php?id='.$row['ProductID'].'">Delete</a></td>';
    echo ' ';
    echo '<td><input type="checkbox" name="check[]" value="'.$row['ProductID'] .'" ></td>';
    echo '<td><input type="checkbox" name="startDates[]" value="'.$row['ProductStartDate'] .'" ></td>';
    echo '<td><input type="checkbox" name="endDates[]" value="'.$row['ProductEndDate'] .'" ></td>';
    echo '</tr>';
}


$checkbox1 = $_POST['check'];
for ($i=0; $i<sizeof($checkbox1); $i++) {

    $date1 = strtotime($_POST['startDates'][$i]);
    $date2 = strtotime($_POST['endDates'][$i]);
    // Adding current month + all months in each passed year
    $diff = 1 + (date("Y",$date2)-date("Y",$date1))*12;
    // Add/subtract month difference
    $diff += date("m",$date2)-date("m",$date1);

    $sql3="INSERT into enrollment (StudentID, ProductID, EnrollmentMonth) VALUES($StudentID, $checkbox1[$i], $diff)";
    mysql_query($sql3);
}

And stop using "mysql_*" functions, see my comment above to your answer.

Sir_Faenor
  • 878
  • 6
  • 13