Jump to content

[SOLVED] Checkbox Form - Inserting row data from one SQL Table to Another SQL Table


julzk

Recommended Posts

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 ->

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%'> $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%'> $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:

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 :*(

Link to comment
Share on other sites

for the insert have

$jobs = $_POST['checkbox'];

$jobIDs = array();

foreach ($job in $jobs) 
{
$jobIDs[] = "('$job')";
}

//implode with "," for sql
$jobIDS = implode(", ", $jobIDs);


$sql = "INSERT INTO tbl_jobs_done(jobs_id) VALUES $jobIDS;";

 

Link to comment
Share on other sites

I tried your method but it returns "Parse error: syntax error, unexpected T_STRING in /var/www/html/tsdportal/dailyjobs_index.php on line 121".. Line 121 is the foreach ($job in $jobs)  line

 

My Insert Code here:

// ##### Complete Job and Insert into DB ##### Start ->
if ($_POST[submit] == "Complete")
{

$jobs = $_POST['checkbox'];

$jobIDs = array();

foreach ($job in $jobs) 
{
$jobIDs[] = "('$job')";
}

//implode with "," for sql
$jobIDS = implode(", ", $jobIDs);

$sql = "INSERT INTO tbl_jobs_done(jobs_id) VALUES $jobIDS;";

$result = mysql_query($sql);
}
// ##### Complete Job and Insert into DB ##### End <- 

Link to comment
Share on other sites

haha sorry! i wrote that foreach loop as if it were in C#,

// ##### Complete Job and Insert into DB ##### Start ->
if ($_POST[submit] == "Complete")
{

$jobs = $_POST['checkbox'];

$jobIDs = array();

foreach ($jobs as $job)
{
$jobIDs[] = "('$job')";
}

//implode with "," for sql
$jobIDS = implode(", ", $jobIDs);

$sql = "INSERT INTO tbl_jobs_done(jobs_id) VALUES $jobIDS;";

$result = mysql_query($sql);
}
// ##### Complete Job and Insert into DB ##### End <- 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.