Checkbox Form - Inserting row data from one Table to Another

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
julzk
Forum Newbie
Posts: 23
Joined: Fri Oct 30, 2009 4:42 am

Checkbox Form - Inserting row data from one Table to Another

Post by julzk »

This is driving me crazy. I have been trying to work this out for weeks and cannot get my head around it. any help would be greatly appreciated!! :)

I have two tables "tbl_jobs" and "tbl_jobs_done". I have an SQL query which displays and formats my daily job list from tbl_jobs SQL database, and has a checkbox at the end of each row. I can't get the stupid checkboxs to work :*(

I want to be able to select 1, 2, 3 or more checkboxs and then click my submit button which will then insert the data into my second MySQL database named "tbl_jobs_done". i have spent hours and hours looking at tutorials and other peoples forum posts but can't seem to find something that hits home and works for me :(

I only need the jobs_id field data from the tbl_jobs to be inserted into tbl_jobs_done table and into jobs_id field. I have other data on the form that will also be submitted. But in general, that's all I need :*(

Here is my SQL Query ->

Code: Select all

if(!isset($cmd))
    {     
      echo "<form method='post' action=''><table width='98%' cellspacing='1' cellpadding='1' border='0'>\n";
      echo "<tr>\n";
      echo "<td class='sectiontitle' valign='top' align='left' width='100%'>Today's Jobs!</td>";
      echo "<td class='txt' colspan='2' valign='top' align='right'><input name='submit' type='submit' value='Complete'></td>";
      echo "</tr>\n";
 
 
$everyday=EVERYDAY;
$dayname=date('l');
if($wtype=date('l'))
{
    if ($wtype=="Saturday" or $wtype=="Sunday")
    {
    $wtype=WEEKEND;
    }
    elseif ($wtype=="Monday" or $wtype=="Tuesday" or $wtype=="Wednesday" or $wtype=="Thursday" or $wtype=="Friday")
    {
    $wtype=WEEKDAY;
    }
}
 
$result = mysql_query("SELECT * FROM tbl_jobs WHERE jobs_datetype IN('$everyday', '$dayname', '$wtype') ORDER BY jobs_id ASC");
 
$count=mysql_num_rows($result);
 
$i = 0;
while($r=mysql_fetch_array($result))
{
      $jobs_id=$r["jobs_id"];
      $jobs_description=str_replace("\r\n","<br>",$r["jobs_description"]);
      $jobs_datestart=$r["jobs_datestart"];
      $jobs_dateend=$r["jobs_dateend"];
      $jobs_datetype=$r["jobs_datetype"];
      $jobs_user=$r["jobs_user"];
      $jobs_updateuser=$r["jobs_updateuser"];
 
      if($i%2 == 0){
      echo "<tr class='rowresult1'>\n";
      echo "<td class='txt' align='left' width='100%'>&nbsp;$jobs_description</td>";
      echo "<td class='txt' align='right'><input name='jobs_comment' style='width: 50px;'></td>";
      echo "<td class='txt' valign='top' align='center'><input name='checkbox[]' type='checkbox' id='checkbox[]' value='$jobs_id'></td>";
      echo "</tr>\n";
      $i++;
      }else{
      echo "<tr class='rowresult2'>\n";
      echo "<td class='txt' align='left' width='100%'>&nbsp;$jobs_description</td>";
      echo "<td class='txt' align='right'><input name='jobs_comment' style='width: 50px;'></td>";
      echo "<td class='txt' valign='top' align='center'><input name='checkbox[]' type='checkbox' id='checkbox[]' value='$jobs_id'></td>";
      echo "</tr>\n";
      $i++;
      }
    
      
    
}
echo "</table></form>\n";
    }
And here is my Insert code:

Code: Select all

Well... i have no real code here at the moment as it's chopped up in 100 different things in attempts to get this to work :*(
 
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Checkbox Form - Inserting row data from one Table to Another

Post by josh »

You're never inserting the row. You'll need an insert statement, and probably a delete statement. You should consider just having a filed called 'done' that you set to 1 or 0. For instance what if in addition to 'done' you wanted to have an attribute 'published', what are you going to have tables

job_published_done
job_published
job_unpublished_done
job_unpublished
multiplied how many ever fields you have?
No I don't think so...
julzk
Forum Newbie
Posts: 23
Joined: Fri Oct 30, 2009 4:42 am

Re: Checkbox Form - Inserting row data from one Table to Another

Post by julzk »

it's a daily job list and re-occurring on set days/weekends/months/everyday for example.. I figure it's going to be easy to have two tables.. one table to store and I can manage/edit/delete jobs. And the second table is to store the completion of jobs. Then I can use the MySQL function to JOIN or INNER JOIN the two tables by the referenced ID to tell if the job has been completed or not on X date.
julzk
Forum Newbie
Posts: 23
Joined: Fri Oct 30, 2009 4:42 am

Re: Checkbox Form - Inserting row data from one Table to Another

Post by julzk »

Nevermind, all resolved! :)

I worked out my solution :)

form checkbox field for each row result from my sql query.

Code: Select all

<input name='checkbox[]' type='checkbox' value='$jobs_id'>
And here's my insert code

Code: Select all

// ##### Manage Jobs Edit Update DB ##### Start ->
if ($_POST[submit] == "Complete")
{
    foreach($_POST[checkbox] as $jobs_id) {
        $sql="INSERT INTO tbl_jobs_done (jobs_id, jobs_date, jobs_comment, jobs_datedone, jobs_timedone, jobs_user) VALUES ('$jobs_id', '$currdate', '$comments', '$currdate', '$currtime', '$_SESSION[username]')";
        $result = mysql_query($sql);
    }
    //var_dump($comments);
    print_r($_POST);
}
// ##### Manage Jobs Edit Update DB ##### End <-
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Checkbox Form - Inserting row data from one Table to Another

Post by josh »

Cool, I've been working on rewriting a very similar system for the last 6 months.
Post Reply