Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jan 2007
    Posts
    154
    Thanks
    52
    Thanked 0 Times in 0 Posts

    Insert into Dynamic DB

    Hi all. Have been trying to get my head around this simple problem.

    My webpage displays from my database a table of
    1) ID's,
    2) a checkbox which toggles the visibility of
    3) a textbox

    So my output looks like this:

    1 [✔] _________
    2 []
    3 [✔] _________
    5 []
    8 []


    The form has a submit button which on click I want to update a column called 'complete' with the text from the textbox.

    So far my code is:

    PHP Code:
    <table width="100%" class="samples">
                <tr>
                
               
              
                
                 <td width="648" valign="top" style="border-right:1pt solid #4F5258;border-top:1pt solid #4F5258;border-left:1pt solid #4F5258; color:#000;">Task / Deadline</td>
                 
                 <td width="107" valign="top" style="border-right:1pt solid #4F5258;border-top:1pt solid #4F5258; color:#000;">Complete</td>
                
                    </tr>                

    <?PHP
       $query 
    "SELECT event.eventcustomer,event.description,event.date_event,event.event_id FROM event,dbACT WHERE event.eventcustomer=dbACT.Contact AND dbACT.ID='$user_id2' ORDER BY event.date_event ASC";
    $result mysql_query($query);
    while(list(
    $eventcustomer,$description,$deadline,$eid) = mysql_fetch_row($result))
    {
                    
    ?>
                    <tr align="left">
                     
                 
                  
               
                      <td width="648" valign="top" style="border-right:1pt solid #4F5258;border-top:1pt solid #4F5258;border-left:1pt solid #4F5258;"><p align=center> <?PHP echo $description;  ?> <br /><small><i>by <?PHP echo $deadline;  ?></i></small></p></td>
                     
                      <td width="107" valign="top" style="border-right:1pt solid #4F5258;border-top:1pt solid #4F5258;"><p align=center><?PHP echo "<input type=\"text\" id=\"eid\" name=\"eid[event_id]\" value=\"$eid\" />"?>
                        <input name="CB<?php echo $eid?>" type="checkbox" onclick="showHide(this);" > <div id="Text<?php echo $eid?>" style="display: none;"><input name="eidtext[<?php echo $eid?>]" type="text" /></div></p></td>
                      
                <?php ?>
              








                </tr>
              </table>
    </form>

    This works fine and produces the result I need.

    When I want to post this information the code I have so far is:

    PHP Code:
    $eid $_POST['eid'];
    $eidtext $_POST['eidtext'];


    $query mysql_query("UPDATE event SET complete='$eidtext' WHERE event_id='$eid'"); 
    I'm sure it's something to do with arrays, but I cant seem to grasp how they would be used in this instance?

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    First of all, you didn't say what the problem you're having actually is. "It doesn't work" isn't very helpful; you really should be catching query errors so you know exactly what's going on with your queries.

    PHP Code:
    $queryString "UPDATE event SET complete='$eidtext' WHERE event_id='$eid'";
    $query mysql_query($queryString);
    if (!
    $query) {
        die(
    "Query Error! Query string: $queryString<br />Error: ".mysql_error());

    Now then, to the actual problem: I see you are creating this HTML inside a loop:

    PHP Code:
    <?PHP echo "<input type=\"text\" id=\"eid\" name=\"eid[event_id]\" value=\"$eid\" />"?>
    There are two problems with this. First, each element in your DOM needs to have a unique ID value. You are assigning multiple input elements the id "eid". That's going to cause issues.

    Second, the name "eid[event_id]" is not going to change on each iteration in your loop, so you're going to end up with only one entry in your $_POST array.

    To see how PHP is interpreting your form elements over to the $_POST array, use this:
    PHP Code:
    echo "<pre>".print_r($_POST,true)."</pre>"
    That'll be helpful in figuring out how to process the data and eventually get to updating the database.

  • Users who have thanked Fumigator for this post:

    thoford75 (05-03-2011)

  • #3
    Regular Coder
    Join Date
    Jan 2007
    Posts
    154
    Thanks
    52
    Thanked 0 Times in 0 Posts
    Thanks for the reply Fumigator, by 'not working' I should have explained that the UPDATE event query updates the complete field with a blank value.

    Removing the [event_id] and setting the complete field to equal $eid returns correctly the $eid value of the highest id. In my case 4. So the last row of the db is updated with the complete value as 4 (which is correct as the ID of this row is 4)

    From this I presume I need a way to make each textbox a unique ID that I can then refer to when I post?

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,471
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    This is my idea ... everyone else beat me to the post.
    Examine all of the ideas and put together a plan.



    You'll want to use an array for both the checkboxes and textboxes.

    The value of the checkbox will be an index number ...
    With PHP, when you process the checkbox array, the array will only
    contain the boxes that were checked. So in your example where you
    are showing box 1 and 3 checked ... the checkbox array will only have
    two items in it. You won't know which ones were checked, so you need
    to example the values of them ... and then you know.

    Here's an example:
    PHP Code:

    $index=0;
    while(list($eventcustomer,$description,$deadline,$eid) = mysql_fetch_row($result))
    {
                    ?>
                    <tr align="left">
                     
                 
                  
               
                      <td width="648" valign="top" style="border-right:1pt solid #4F5258;border-top:1pt solid #4F5258;border-left:1pt solid #4F5258;"><p align=center> <?PHP echo $description;  ?> <br /><small><i>by <?PHP echo $deadline;  ?></i></small></p></td>
                     
                      <td width="107" valign="top" style="border-right:1pt solid #4F5258;border-top:1pt solid #4F5258;"><p align=center><?PHP echo "<input type=\"text\" id=\"eid\" name=\"eid[]\" value=\"$eid\" />"?>
                        <input name="CB[]" value="<?php echo $index?>" type="checkbox" onclick="showHide(this);" > <div id="Text<?php echo $eid?>" style="display: none;"><input name="eidtext[]" type="text" /></div></p></td>
                      
                <?php 
    $index
    ++;
    ?>

    Now, when you process the form .... use this to test it out ...
    PHP Code:

    // get the eid array
    $eid $_POST['eid'];

    // get the textbox array
    $eidtext $_POST['eidtext'];

    // loop through the checkbox array
    foreach($_POST['CB'] as $item){

    echo 
    "A checked box is showing: ".$eidtext[$item]."<br>";

    // $query = mysql_query("UPDATE event SET complete='$eidtext' WHERE event_id='$eid[$item]'");


    Untested ... but you can sort of get the idea.



    .

  • Users who have thanked mlseim for this post:

    thoford75 (05-03-2011)

  • #5
    Regular Coder
    Join Date
    Jan 2007
    Posts
    154
    Thanks
    52
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fumigator View Post
    To see how PHP is interpreting your form elements over to the $_POST array, use this:
    PHP Code:
    echo "<pre>".print_r($_POST,true)."</pre>"
    That'll be helpful in figuring out how to process the data and eventually get to updating the database.
    Returns

    [eid] => Array
    (
    [event_id] => 4
    )

    [eidtext] => Array
    (
    [1] =>
    [2] =>
    [3] => three
    [4] =>
    )

  • #6
    Regular Coder
    Join Date
    Jan 2007
    Posts
    154
    Thanks
    52
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    This is my idea ... everyone else beat me to the post.
    Examine all of the ideas and put together a plan.



    You'll want to use an array for both the checkboxes and textboxes.

    The value of the checkbox will be an index number ...
    With PHP, when you process the checkbox array, the array will only
    contain the boxes that were checked. So in your example where you
    are showing box 1 and 3 checked ... the checkbox array will only have
    two items in it. You won't know which ones were checked, so you need
    to example the values of them ... and then you know.

    Here's an example:
    PHP Code:

    $index=0;
    while(list($eventcustomer,$description,$deadline,$eid) = mysql_fetch_row($result))
    {
                    ?>
                    <tr align="left">
                     
                 
                  
               
                      <td width="648" valign="top" style="border-right:1pt solid #4F5258;border-top:1pt solid #4F5258;border-left:1pt solid #4F5258;"><p align=center> <?PHP echo $description;  ?> <br /><small><i>by <?PHP echo $deadline;  ?></i></small></p></td>
                     
                      <td width="107" valign="top" style="border-right:1pt solid #4F5258;border-top:1pt solid #4F5258;"><p align=center><?PHP echo "<input type=\"text\" id=\"eid\" name=\"eid[]\" value=\"$eid\" />"?>
                        <input name="CB[]" value="<?php echo $index?>" type="checkbox" onclick="showHide(this);" > <div id="Text<?php echo $eid?>" style="display: none;"><input name="eidtext[]" type="text" /></div></p></td>
                      
                <?php 
    $index
    ++;
    ?>

    Now, when you process the form .... use this to test it out ...
    PHP Code:

    // get the eid array
    $eid $_POST['eid'];

    // get the textbox array
    $eidtext $_POST['eidtext'];

    // loop through the checkbox array
    foreach($_POST['CB'] as $item){

    echo 
    "A checked box is showing: ".$eidtext[$item]."<br>";

    // $query = mysql_query("UPDATE event SET complete='$eidtext' WHERE event_id='$eid[$item]'");


    Untested ... but you can sort of get the idea.



    .

    Great work! Post to the correct ID in the database now.

    Only thing is that it updates complete as 'Array'. Do I need to change the column type in mysql?

  • #7
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,471
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    complete='$eidtext'

    should be:

    complete='$eidtext[$item]'



    .


  •  

    Tags for this Thread

    Posting Permissions

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