CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   Insert into Dynamic DB (http://www.codingforums.com/showthread.php?t=225854)

thoford75 05-03-2011 02:05 PM

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?

Fumigator 05-03-2011 03:19 PM

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.

thoford75 05-03-2011 03:32 PM

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?

mlseim 05-03-2011 03:34 PM

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.



.

thoford75 05-03-2011 03:35 PM

Quote:

Originally Posted by Fumigator (Post 1086036)
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 05-03-2011 03:53 PM

Quote:

Originally Posted by mlseim (Post 1086043)
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?

mlseim 05-03-2011 06:21 PM

complete='$eidtext'

should be:

complete='$eidtext[$item]'



.


All times are GMT +1. The time now is 06:44 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.