1

I'm using a table to update a database, the table has 264 checkboxes which can be checked and unchecked then updated on the database.

I'm doing this by posting data on the form, using a while loop to set all fields to blank ( the value of the checkbox and the textarea value) for each respective field, then using a foreach loop to update each row in the database with the value of the checkbox.

Now, what I want to do is add the textarea value for each ticked checkbox into the database as well, what i cant figure out is how to do this?

This is my update code:

if (isset($_POST["update"])) {
$seolistRes2 = mysql_query($seolistQ) or die(mysql_error());
while ($seolistRow2 = mysql_fetch_array($seolistRes2)) {
    $wsID1 = $seolistRow2["worksheetID"];
    $updateWSQ2 = "UPDATE seo_work SET taskValue=0, taskInfo='' WHERE worksheetID=$wsID1 AND userID=$userID";
    mysql_query($updateWSQ2) or die(mysql_error());
}

$item = $_POST;
foreach($item as $key => $value) {
    $wsID = str_replace("checkbox","",$key);

    if (is_numeric($wsID)) {
        $updateWSQ = "UPDATE seo_work SET taskValue=$value taskInfo=$value WHERE worksheetID=$wsID AND userID=$userID";
        mysql_query($updateWSQ) or die(mysql_error());
        header("Location: worksheet.php?y=".$seoworkyear."&userID=$userID&action=success");
    }
}

}

This is checkbox and textarea code: (please note this is within a form)

$currentTask = '';
echo "<tr class='tr'>";
while ($seolistRow = mysql_fetch_array($seolistRes)) {
    $taskValue = $seolistRow["taskValue"];
    $worksheetID = $seolistRow["worksheetID"];
    $taskName = $seolistRow["taskName"];
    $taskInfo = $seolistRow["taskInfo"];

    if ($taskValue == 1) {
        $taskDone = "<input type='checkbox' value='1' class='checkbox' name='checkbox".$worksheetID."' id=checkbox'".$worksheetID."' checked='checked' />".
        "<textarea class='textarea' name='textarea".$worksheetID."' id=textarea'".$worksheetID."'>" . $taskInfo . "</textarea>";
    }
    else {
        $taskDone = "<input type='checkbox' value='1' class='checkbox' name='checkbox".$worksheetID."' id='checkbox".$worksheetID."' />".
        "<textarea class='textarea' name='textarea".$worksheetID."' id=textarea'".$worksheetID."'>" . $taskInfo . "</textarea>";
    }
    if ($currentTask != $taskName) {
        echo "</tr>";
        echo "<tr class='tr'>";
        echo "<td class='task'>".$taskName."</td>";
    }
    echo "<td class='tick'>".$taskDone."</td>";
        $currentTask = $taskName;
}
echo "</tr>";
1
  • ... in other news, look up 'SQL Injection' Commented Jan 5, 2011 at 10:09

3 Answers 3

1

Use your HTML form like shown below.

$currentTask = '';
echo "<tr class='tr'>";
while ($seolistRow = mysql_fetch_array($seolistRes)) {
    $taskValue = $seolistRow["taskValue"];
    $worksheetID = $seolistRow["worksheetID"];
    $taskName = $seolistRow["taskName"];
    $taskInfo = $seolistRow["taskInfo"];

    if ($taskValue == 1) {
        $taskDone = "<input type='checkbox' value='1' class='checkbox' name='checkbox[".$worksheetID."]' id='checkbox[".$worksheetID."]' checked='checked' />".
        "<textarea class='textarea' name='textarea[".$worksheetID."]' id='textarea[".$worksheetID."]'>" . $taskInfo . "</textarea>";
    }
    else {
        $taskDone = "<input type='checkbox' value='1' class='checkbox' name='checkbox[".$worksheetID."]' id='checkbox[".$worksheetID."]' />".
        "<textarea class='textarea' name='textarea[".$worksheetID."]' id='textarea[".$worksheetID."]'>" . $taskInfo . "</textarea>";
    }
    if ($currentTask != $taskName) {
        echo "</tr>";
        echo "<tr class='tr'>";
        echo "<td class='task'>".$taskName."</td>";
    }
    echo "<td class='tick'>".$taskDone."</td>";
        $currentTask = $taskName;
}
echo "</tr>";

See the modified name and id of your textarea and checkbox. I have modified it to textarea[SOME_WORKSHEET_ID] and checkbox[SOME_WORKSHEET_ID] respectively.

So, when you submit the form, you will receive those checkbox and textarea value as an array, with worksheetId as an index. You can use this [] technique to retrieve the values of the textarea and checkbox, or as many field you want to add in form.

Use above HTML structure and check the $_POST array.

Hope this will help..

Thanks!

Hussain.

Sign up to request clarification or add additional context in comments.

Comments

0

You should be able to get the value in the textarea using $_POST['textarea' . $wsID] after your call to is_numeric.

1 Comment

That worked, thanks :) now I'm just having a problem with displaying the textarea field. Can only get the first field to show up, as in if I tick six boxes and give each one a different note, it displays note 1 on all 6 boxes.
0

Here is another approach, not completely tested, but hopefully you get the idea...

if (isset($_POST["update"])) 
{
    // All to blank
    $seolistRes2 = mysql_query($seolistQ) or die(mysql_error());
    while ($seolistRow2 = mysql_fetch_array($seolistRes2)) 
    {
        $wsID1 = $seolistRow2["worksheetID"];
        $updateWSQ2 = "UPDATE seo_work SET taskValue=0, taskInfo='' WHERE worksheetID=$wsID1 AND userID=$userID";
        mysql_query($updateWSQ2) or die(mysql_error());
    }

    // Re use your result from the select to go through all the known IDs
    foreach($seolistRow2 as $i => $data) 
    {
        // Obtain the ID
        $id = $data['worksheetID'];

        // Get the checkbox value for current ID
        $checkbox_wsID = $_POST['checkbox' . $id];

        // Get the textarea value for current ID
        $textarea_wsID = $_POST['textarea' . $id];

        // Update the Row using mysql_escape
        $updateWSQ = "UPDATE seo_work " . 
        "SET taskValue = '" . mysql_escape_string($checkbox_wsID) ."' taskInfo = '" . mysql_escape_string($textarea_wsID) ."'" .
        "WHERE worksheetID=$id AND userID=$userID";

        mysql_query($updateWSQ) 
            or die(mysql_error());

        header("Location: worksheet.php?y=".$seoworkyear."&userID=$userID&action=success");         
    }
}

Comments

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.