Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-03-2011, 02:05 PM   PM User | #1
thoford75
Regular Coder

 
Join Date: Jan 2007
Posts: 154
Thanks: 52
Thanked 0 Times in 0 Posts
thoford75 is an unknown quantity at this point
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?
thoford75 is offline   Reply With Quote
Old 05-03-2011, 03:19 PM   PM User | #2
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
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.
__________________
Fumigator is offline   Reply With Quote
Users who have thanked Fumigator for this post:
thoford75 (05-03-2011)
Old 05-03-2011, 03:32 PM   PM User | #3
thoford75
Regular Coder

 
Join Date: Jan 2007
Posts: 154
Thanks: 52
Thanked 0 Times in 0 Posts
thoford75 is an unknown quantity at this point
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?
thoford75 is offline   Reply With Quote
Old 05-03-2011, 03:34 PM   PM User | #4
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,046
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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.



.
mlseim is offline   Reply With Quote
Users who have thanked mlseim for this post:
thoford75 (05-03-2011)
Old 05-03-2011, 03:35 PM   PM User | #5
thoford75
Regular Coder

 
Join Date: Jan 2007
Posts: 154
Thanks: 52
Thanked 0 Times in 0 Posts
thoford75 is an unknown quantity at this point
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] =>
)
thoford75 is offline   Reply With Quote
Old 05-03-2011, 03:53 PM   PM User | #6
thoford75
Regular Coder

 
Join Date: Jan 2007
Posts: 154
Thanks: 52
Thanked 0 Times in 0 Posts
thoford75 is an unknown quantity at this point
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?
thoford75 is offline   Reply With Quote
Old 05-03-2011, 06:21 PM   PM User | #7
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,046
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
complete='$eidtext'

should be:

complete='$eidtext[$item]'



.
mlseim is offline   Reply With Quote
Reply

Bookmarks

Tags
array, mysql, php, query, select

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:14 AM.


Advertisement
Log in to turn off these ads.