SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict kirikintha's Avatar
    Join Date
    Nov 2004
    Location
    Boulder, CO
    Posts
    290
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multiple Record Update with Checkbox control

    Hi everyone, this is my first post here. Sorry if there is a lot here, better safe than uninformed.
    I use the current versions of php, and Mysql make my pages dynamic. I use Dreamweaver MX 6.1 to build my pages

    I have been looking for a number of days on updating my database with multiple checkboxes through an array. The user selects a row with the checkbox to designate which record is updated. No matter what I do, either my loop does not work correctly, or I get no result at all with these notice errors.

    "Notice: Undefined index: txtstudent0 in E:\www\intranet\new\admin_today.php on line 11

    Notice: Undefined index: chkstudentdel0 in E:\www\intranet\new\admin_today.php on line 11"



    I am using radio boxes to "choose" a php script to execute. Currently, I have a delete script that works fine, and an update that does not.
    Here's my form code:

    Code:
    <form action="admin_today.php" method="post" name="formstudent" id="formstudent">
              <table width="100%" border="0">
                <tr align="center" valign="middle"> 
                  <td colspan="3" bgcolor="#FFFFCC">This is the information already 
                    stored into the database</td>
                </tr>
                <?php do { ?>
                <tr bgcolor="#FFFFCC"> 
                  <td align="center" valign="middle"> 
                    <input name="chkstudentdel[]" type="checkbox" id="chkstudentdel[]" value="<?php echo $row_rsstudentaffairs['TodayID']; ?>"> 
                  </td>
                  <td colspan="2" align="center" valign="middle"> 
                    <textarea name="txtstudent[]" cols="60" rows="3" id="txtstudent[]"><?php echo $row_rsstudentaffairs['TodayInfo']; ?></textarea>
                  </td>
                </tr>
                <?php } while ($row_rsstudentaffairs = mysql_fetch_assoc($rsstudentaffairs)); ?>
                <tr align="center" valign="middle" bgcolor="#FFFFCC"> 
                  <td>Select your function</td>
                  <td width="44%"><input type="radio" name="goal1" value="1">
                    Delete Record 
                    <input name="goal2" type="radio" value="2">
                    Update Record</td>
                  <td width="21%"><input type="submit" name="Submit" value="Submit"></td>
                </tr>
              </table>
            </form>
    Here's my Delete and Update Code So far
    Code:
    <?php
    if (isset($_POST['goal1'])) //select the radio button delete, then submit
    {
          $ids_to_delete = join(',' , $_POST['chkstudentdel']); //sql for deleting follows
           mysql_query("DELETE FROM tblbcmttoday WHERE TodayID IN ($ids_to_delete)");
    }
    elseif (isset($_POST['goal2'])) //select the radio button update, then submit
    {
    		for ($i = 0; $i < count($_POST['chkstudentdel']); $i++) // loop that doesn't work
    	  	mysql_query("UPDATE tblbcmttoday Set TodayInfo = '".$_POST['txtstudent'.$i]."' Where TodayID IN ".$_POST['chkstudentdel'.$i]."");
    }
    ?>
    Right now I'm getting no changes in my database, can anyone help? I'm not terribly new, or super code guru - I'm right in the middle of everything making sense.

    Thanks so much

  2. #2
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    New Jersey, USA
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If it's a radio button, shouldn't they all be named the same?
    Also, shouldn't you be checking the VALUE of goal1 == 1, or == 2, instead of just isset?

    You might try calling phpinfo at the bottom of your script to show what variables had which values when your script was run.

    =Austin

  3. #3
    SitePoint Addict kirikintha's Avatar
    Join Date
    Nov 2004
    Location
    Boulder, CO
    Posts
    290
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    You're right

    The radio buttons work as intentded - I changed the value to blank, since php doesn't have a problem with the declaration of variables like in asp. (Originally I was thinking Boolean, like a 1,0 then I changed my mind - I found the value had nothing to do with whether or not the script ran. I'm just checking to see if the radio button is in the post)

    I actually have some new code to post, my current working code is using an array - I'm almost there figuring ou thow to make this work.

    I can get the checkbox to update the proper columns in the record, but now I get 'array' instead of the value I want.

    I found another post that helps out - will update here shortly. Something to do with implode...

    Thanks a lot Big-A - you're right, phpinfo helps out a lot. Also, I'm posting to a different page to echo the values in my array. LMK if yo think of something else.

    PAul

  4. #4
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    New Jersey, USA
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahh, the array is probably caused by the fact that you're trying to reference xxx0 instead of xxx[0].

    Change $_POST["chkstudentdel" . $i] to $_POST["chkstudentdel"][$i] and see if that works.

    =Austin

  5. #5
    SitePoint Addict kirikintha's Avatar
    Join Date
    Nov 2004
    Location
    Boulder, CO
    Posts
    290
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Awesome

    Thanks for the tip, I have the pages at work, so as soon as I'm there tomorrow, I'll post again and let you know -

    Thanks bro

    Paul

  6. #6
    SitePoint Addict kirikintha's Avatar
    Join Date
    Nov 2004
    Location
    Boulder, CO
    Posts
    290
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Here's The new code

    This is what I have now - the code puts the information into the right place into the database, but only the last key in the $array2 value

    Code:
    <?php
    if (isset($_POST['goal1'])) //select the radio button delete, then submit
    {
          $ids_to_delete = join(',' , $_POST['chkstudentdel']); //sql for deleting follows
           mysql_query("DELETE FROM tblbcmttoday WHERE TodayID IN ($ids_to_delete)");
    }
    elseif (isset($_POST['goal2'])) //select the radio button update, then submit
    {
    		$array2 = ($_POST['txtstudent']);
    	foreach($array2 as $studenttxt=>$studenttxtvalue)
    		
    		$array = ($_POST['chkstudentdel']);
    	foreach($array as $student=>$studentvalue)
    	
    {
    	  	mysql_query("UPDATE tblbcmttoday Set TodayInfo = '$studenttxtvalue' Where TodayID = $studentvalue");
    }
    }
    ?>
    For example: If you check a checkbox in row 2 the database updates the record with the primary key "85" - but the array updates the last key [2] in this case "changing to something new 5" in stead of the intended key [1] "fenbjfdsfdjgsdfg" (hope that makes sense)

    _POST["txtstudent"]
    Array
    (
    [0] => changing information to something new 2
    [1] => fenbjfdsfdjgsdfg
    [2] => changing to something new 5
    )


    _POST["chkstudentdel"]
    Array
    (
    [0] => 85
    )


    Obiviously I'm missing something important. I presume I need to get these key s to line up somehow - but I am clueless as to how.

    My best guess so far is to use an 'if' or 'for' instead of 'foreach' - am I on the right track?
    Thanks

    PAul

  7. #7
    SitePoint Addict kirikintha's Avatar
    Join Date
    Nov 2004
    Location
    Boulder, CO
    Posts
    290
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    By the way, I resolved this problem differently than I though.

    Search for "Multiple Update with PHP" - I'm putting a post up there. It was relatively simple when I started to think about what I was doing.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •